Main.storyboard does not load - ios

I'm new to iOS and I'm making some exercises I'm stuck at the begining. I'd like to use standard Main.storyBoard which looks like this:
And the AppDelegate.m file looks like this:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
// CGRect bounds = [[UIScreen mainScreen] bounds];
// self.window = [[UIWindow alloc] initWithFrame:bounds];
//
// self.controler = [[ViewController alloc] init];
//
// self.window.rootViewController = self.controler;
//
// [self.window makeKeyAndVisible];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:[NSBundle mainBundle]];
UIViewController *vc = [storyboard instantiateInitialViewController];
// Set root view controller and make windows visible
self.window.rootViewController = vc;
[self.window makeKeyAndVisible];
return YES;
}
And ViewControler if it will be usefull:
#interface ViewController ()
#property UIView *viewM;
#end
#implementation ViewController
-(void) loadView
{
CGRect bounds = [[UIScreen mainScreen] bounds];
self.viewM = [[UIView alloc] initWithFrame: bounds];
self.view = self.viewM;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(#"AHAHH DOTKNIETO EKRANU");
}
-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
self.view.backgroundColor = [UIColor blueColor];
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 200, 200, 50)];
//SET TEXT FOR THIS LABEL
label.text = #"TEXTTTTTT";
[self.view addSubview:label];
// Do any additional setup after loading the view, typically from a nib.
}
Everytime I run this sample it loads me View which is white instead of the image above. What's not right with it? I'm using Xcode 5.1.1 version.

In the project - General page you should see a setting for "Main Interface".
Remove all the code in the applicationDidFinishLaunching except return YES; and make sure this is set to Main.

Related

Adding a UISegmentedControl to UINavigationBar

I try to add a UISegmentedControl to the UINavigationBar but when running, the UISegmentedControl is not showing.
here is a code
Appdelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
MainViewController2 *mainVC = [[MainViewController2 alloc] init];
self.window.rootViewController = mainVC;
return YES;
}
MainViewController2.m
#import "MainViewController2.h"
#interface MainViewController2 ()
#end
#implementation MainViewController2
#synthesize firstVC;
#synthesize segment;
- (void)viewDidLoad {
[super viewDidLoad];
[self initSegment];
}
-(void)initSegment{
segment.frame =CGRectMake(10,100,199,100);
segment = [[UISegmentedControl alloc]initWithItems:#[#"seg1", #"seg2", #"seg3"]];
self.navigationItem.titleView = segment;
[self.view addSubview:firstVC];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
You can directly add segment in navigation bar as a title view, using storyboard.
Here is programatically using your code:
Note: Don't forget to embed your view controller with navigation controller. (Either programatically or using storyboard)
- (void)viewDidLoad {
[super viewDidLoad];
[self initSegment];
}
-(void)initSegment{
UISegmentedControl *segment = [[UISegmentedControl alloc]initWithItems:#[#"seg1", #"seg2", #"seg3"]];
self.navigationItem.titleView = segment;
}
Add following code to your AppDelegate.m
//
// AppDelegate.m
// OBJC_Test
//
// Created by Krunal on 10/10/17.
// Copyright © 2018 Krunal. All rights reserved.
//
#import "AppDelegate.h"
#interface AppDelegate ()
#end
#implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
/*
UIViewController *viewController = [[UIViewController alloc] init];
viewController.view.frame = self.window.bounds;
viewController.view.backgroundColor = [UIColor whiteColor];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
self.window.rootViewController = navigationController;
[self.window makeKeyAndVisible];
*/
//or try this according to your code
MainViewController2 *mainVC = [[MainViewController2 alloc] init];
mainVC.view.frame = self.window.bounds;
mainVC.view.backgroundColor = [UIColor whiteColor];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:mainVC];
self.window.rootViewController = navigationController;
[self.window makeKeyAndVisible];
return YES;
}
Add this code to your AppDelegate.m in the didFinishLaunchingWithOptions function to embed a navigation controller programmatically.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
MainViewController2 *mainVC = [[MainViewController2 alloc] init];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:mainVC];
self.window.rootViewController = navigationController;
[self.window makeKeyAndVisible];
return YES;
}
Below code might be useful to you
AppDelegate.h
//Declare UINavigationcontroller
#property (strong, nonatomic) UINavigationController *navigationController;
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
self.window.backgroundColor = [UIColor whiteColor];
MainViewController2 *mainVC = [[MainViewController2 alloc] init];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:mainVC];
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
return YES;
}
In MainViewController2.m use below code
#import "MainViewController2.h"
#interface MainViewController2 ()
#end
#implementation MainViewController2
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self setUpSegmentalController];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)setUpSegmentalController{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 250, 44)];
//view.backgroundColor = [UIColor redColor];
NSArray *itemArray = [NSArray arrayWithObjects: #"One", #"Two", #"Three", nil];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:itemArray];
segmentedControl.frame = CGRectMake(0, 0, 250, 44);
segmentedControl.segmentedControlStyle = UISegmentedControlStylePlain;
[segmentedControl addTarget:self action:#selector(segmentControlAction:) forControlEvents: UIControlEventValueChanged];
segmentedControl.selectedSegmentIndex = 1;
[view addSubview:segmentedControl];
self.navigationItem.titleView = view;
}
- (void)segmentControlAction:(UISegmentedControl *)segment
{
NSLog(#"selectedSegmentIndex-----%ld",segment.selectedSegmentIndex);
if(segment.selectedSegmentIndex == 0)
{
// code for the first button
}
}
Use this code.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
MainViewController2 *mainVC = [[MainViewController2 alloc] init];
self.window.rootViewController = mainVC;
[self.window makeKeyAndVisible];
return YES;
}

How to call touchesBegan in a sub-classed UIView in iOS 8?

I want touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event to be called in a sub-classed UIView.
AppDelegate.m:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self.window setRootViewController:[[UIViewController alloc] init]];
CGRect firstFrame = self.window.bounds;
HypnosisView *firstView = [[SubClassedView alloc] initWithFrame:firstFrame];
[self.window addSubview:firstView];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
SubClassedView.h:
#import <UIKit/UIKit.h>
#interface SubClassedView : UIView
#end
SubClassedView.m:
#import "SubClassedView.h"
#implementation SubClassedView
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
NSLog(#"Here!");
}
#end
When I touched the screen, the console didn't output "Here!" as I thought it should.
I use the newest Xcode 7 beta 5.
How can I get touchesBegan to be called in the right way?
Thank you very much.
You're adding your HypnosisView as the subview of the window, rather than as a subview of the root view controller's view. Your root view controller should be a UIViewController subclass so that you can modify its behaviour to build your app's navigation flow.
Subclass UIViewController, and add your HypnosisView as a subview in its view hierarchy:
#interface MyViewController : UIViewController
#property(nonatomic, strong) HypnosisView *hypnosisView;
#end
#implementation MyViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.hypnosisView = [[HypnosisView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:hypnosisView];
}
#end
Then in your app delegate, set your view controller subclass to be the root view controller:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
MyViewController *myVC = [[MyViewController alloc] init];
[self.window setRootViewController:myVC];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
This is quite an old-school method of doing things though. Is there any reason you're not using storyboards to build your interface?

Add Original Custom View to the Window

I added my custom view to the window but it doesn't work. I tried to figure it out but it doesn't work well. (I created this project with the empty project template without using storyboards). This screen is supposed to show the red rectangle towards the bottom right corner of the screen.
I wanted to add my custom view showing red screen to the window, but it just shows the white screen.
AppDelegate.m:
//
// HypnosisterAppDelegate.m
// Hypnosister
//
// Created by TSH on 12/1/13.
// Copyright (c) 2013 TSH. All rights reserved.
//
#import "HypnosisterAppDelegate.h"
#import "HypnosisterViewController.h"
#import "HypnosisView.h"
#implementation HypnosisterAppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
HypnosisterViewController *test = [[HypnosisterViewController alloc] initWithNibName:#"HypnosisterViewController" bundle:nil];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:test];
self.window.rootViewController = nav;
CGRect viewFrame = CGRectMake(16, 24, 10, 15);
HypnosisView *view = [[HypnosisView alloc] initWithFrame:viewFrame];
[view setBackgroundColor:[UIColor redColor]];
[[self window] addSubview:view];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
#end
[edit]
I just messed up the ordering.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
HypnosisterViewController *test = [[HypnosisterViewController alloc] initWithNibName:#"HypnosisterViewController" bundle:nil];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:test];
self.window.rootViewController = nav;
CGRect viewFrame = CGRectMake(160, 240, 100, 150);
HypnosisView *view = [[HypnosisView alloc] initWithFrame:viewFrame];
[view setBackgroundColor:[UIColor redColor]];
[[self window] addSubview:view];
return YES;
}
As you set navigationcontroller as rootviewcontroller so HypnosisterViewController will appear every time no doubts. If you want to add a custom view to have to add as a subview on the HypnosisterViewController.
HypnosisView *view = [[HypnosisView alloc] initWithFrame:viewFrame];
[view setBackgroundColor:[UIColor redColor]];
[test.view addSubview:view];
Hopefully it will work
#toshi You code is correct just you missed the ordering .Use this code it will work.Let me know if it doesn't work.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
HypnosisterViewController *test = [[HypnosisterViewController alloc] initWithNibName:#"HypnosisterViewController" bundle:nil];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:test];
self.window.rootViewController = nav;
[self.window makeKeyAndVisible];
CGRect viewFrame = CGRectMake(16, self.window.frame.size.height - 24, 10, 15);
HypnosisView *view = [[HypnosisView alloc] initWithFrame:viewFrame];
[view setBackgroundColor:[UIColor redColor]];
[[self window] addSubview:view];
self.window.backgroundColor = [UIColor whiteColor];
return YES;
}
There is two points
1- The coordinate of the view in iOS begin from top-left so this frame (16, 24, 10, 15) will in the top-left of the view as frame calculated as (x,y,width,height)
2- Window does not responds to device Orientation. It will be always Portrait. If you rotate your device you will need to re-calculate the view Coordinate.

Black empty area below table in iOS7 with nested UIViewControllers

I get some black empty area below a UITableViewController when nested in a certain way (iOS7). Would anybody know why that happens?
(obviously the code is a 100% stripped down version of the actual app's code)
- (BOOL) application:(UIApplication *) application didFinishLaunchingWithOptions:(NSDictionary *) launchOptions {
UITabBarController *tabBarController = [UITabBarController new];
UITableViewController *demoViewController = [UITableViewController new];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:demoViewController];
tabBarController.tabBar.translucent = NO;
navigationController.navigationBar.translucent = NO;
// THESE LINES INTRODUCE A BLACK AREA BELOW THE TABLE
InBetweenViewController *inBetweenViewController = [InBetweenViewController new];
[inBetweenViewController addChildViewController:navigationController];
[inBetweenViewController.view addSubview:navigationController.view];
tabBarController.viewControllers = #[ inBetweenViewController ];
// INSTEAD, THIS LINE WORKS CORRECTLY
/* tabBarController.viewControllers = #[ navigationController ]; */
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
#implementation InBetweenViewController
- (void) viewWillLayoutSubviews {
[super viewWillLayoutSubviews];
for (UIView *subview in self.view.subviews) {
subview.frame = self.view.bounds;
}
}
#end
It's because your inBetweenViewController doesn't know how to render its childViewController. You just add the the view of that controller without any further instructions. It should be possible to solve this by using a simple autoresizingMask. You then also need to make sure that the subview's size is the same as the superview's size when you add it. If you need more details on how to do that, let me know.

Adding UIImageView to rootViewController

All I am simply trying to do is display an image on the screen as I am just starting out iOS
development. I figured since UIImageView is a subclass of UIView, I would add it in a similar way but I am not having any luck. I understand this is an easy question but any help would be appreciated.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.rootViewController = [UIViewController new];
UIImage* stallion = [UIImage imageNamed:#"stallion1.png"];
UIImageView* iv = [UIImageView alloc];
iv.image = stallion;
[self.window.rootViewController.view addSubview:iv];
[self.window makeKeyAndVisible];
return YES;
}
Try something like this:
App delegate:
#import "RootViewController.h"
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
//allocate and initialize the root view controller
RootViewController *rootViewController = [[RootViewController alloc] init];
//set the root view controller
self.window.rootViewController = rootViewController;
[self.window makeKeyAndVisible];
return YES;
}
Create a subclass of UIViewController called RootViewController
.h
#import <UIKit/UIKit.h>
//set the class RootViewController as a subcalss of UIViewController
#interface RootViewController : UIViewController
#end
.m
//this is one of the life cycle methods of a UIViewController and should already be in the code when the class is created
- (void)viewDidLoad
{
//execute the viewDidLoad method of the superclass (UIViewController)
[super viewDidLoad];
//allocate and initialize the image view
//assign the image view a frame
//x offset from the left = 0.0
//y offset from the top = 0.0
//width = the view controller's view's width (should be the whole screen)
//height = the view controller's view's height(should be the whole screen)
UIImageView *iv = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, self.view.frame.size.width, self.view.frame.size.height)];
[iv setImage:[UIImage imageNamed:#"myImageName.png"]];
//the background will be red if everything is setup correctly, but the image isn't found
[iv setBackgroundColor:[UIColor redColor]];
//add the image view to the view controller's view
[self.view addSubview:iv];
}
//EDIT
It would be very helpful for you if u will read book in which there are information about View Controllers. This is very essential information. I recommend you to read "The Core IOS 6 Cookbook" by Erica Sadum. It really helped me.
You can also just look at the apple documentation http://developer.apple.com/library/ios/#documentation/WindowsViews/Conceptual/ViewControllerCatalog/
Write something like this in AppDelegate.m:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
MyViewController *viewController = [[MyViewController alloc] init];
self.window.rootViewController = viewController;
[self.window makeKeyAndVisible];
return YES;
}
Next you must add the UIImageView but only when the view of the created view controller is loaded. The best method for this is viewDidLoad. You can also use viewDidAppear: or viewWillAppear:.
You must override this methods in MyViewController class (which should inherit from the UIViewController class).
- (void)viewDidLoad
{
[super viewDidLoad];
UIImageView *imageView = [UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, self.frame.size.width, self.frame.size.heigth);
imageView.image = [UIImage imageNamed#"stallion1.png"];
[self.view addSubview:imageView];
}

Resources