Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

starting ios project without storyboard

Im having some troubles to start an iOS app using xibs instead of storyboard. The problem is that im getting a black screen and the first view controller is not being called (added break point to viewDidLoad method).

In the app delegate header i have declared this:

@property (strong, nonatomic) UIWindow window;
@property (strong, nonatomic) ViewController *viewController;

And in the didFinishLaunchingWithOptions method i have this implementation:

self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];

UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
navController.navigationBarHidden = YES;

self.window.rootViewController = navController;
[self.window makeKeyAndVisible];

Looking over some forums i found that i should be allocing the window so i added this as the first line of the function

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

The problem is that, when i do this, the app crashes after returning from didFinishLaunchingWithOptions method (SIGABRT without any trace).

I also tried to make the navController a property and also instantiating a default UIViewController class initing the same xib

What am i doing wrong?

Thanks and regards

like image 996
cobolero Avatar asked Aug 04 '15 08:08

cobolero


2 Answers

Hope this helps you:

Delete the view controller and storyboard file and new viewController.h,viewController.h.m ,viewController.xib file.

 #import "AppDelegate.h"

 @interface AppDelegate ()

 @end

 @implementation AppDelegate
 @synthesize viewCOntrollerobj;

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
     self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
     self.viewCOntrollerobj = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
     UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:self.viewCOntrollerobj];
     //navController.navigationBarHidden = YES;

     self.window.rootViewController = navController;
     [self.window makeKeyAndVisible];
     return YES;

}  

enter image description here

like image 64
soumya Avatar answered Sep 29 '22 01:09

soumya


To change your project to use xibs instead of storyboards start by creating a xib for each view controller. You will need to change the File's Owner to class to the view controller you are creating the xib for. Then link the File's Owner view outlet to the view in the xib.

After that, select your app target and change the Main Interface drop down to be empty. Now you can delete the storyboard file.

Finally, initialize your window in the app delegate's application:didFinishLaunchingWithOptions: method and set your initial view controller as the root view controller of the window. Then call makeKeyAndVisible on your app delegate's window and you should be good to go.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    self.window = [UIWindow new];
    self.window.rootViewController = [ViewController new];
    [self.window makeKeyAndVisible];
    return YES;
}
like image 31
jhildensperger Avatar answered Sep 29 '22 02:09

jhildensperger