Loaded Xib file but nothing appears - objective-c

I just started to learn objective-C and am having issues with transitioning between loading a view (directly) to using a XIB as rootViewController. Currently I am trying to load my xib file into a view controller object. While it compiles without any problem, my simulator comes up blank (except for basic interface, time and battery fuel gauge). I also made sure to set my XIB into class BNRReminderViewController and set the view and respective button/objects. (I also imported my class BNRReminderViewController.h to my .m file)
Below is my code:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen] bounds]];
NSBundle *appBundle = [NSBundle mainBundle];
BNRReminderViewController *rvc = [[BNRReminderViewController alloc] initWithNibName:#"BNRReminderViewController" bundle:appBundle];
self.window.rootViewController = rvc;
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
I think what is happening is that my BNRReminderViewController.xib file is not within the mainBundle so when I init with the NSBundle object nothing loads which is why I am getting blank screen. I am new to objective-C so I do not really know how to deal with this as many other languages just import a .h or directly read the file. please help.

You can check if your hypothesis about the NSBundle is correct using rjstellings code here.
However, if you're using at least Xcode 5.0.1, you should be able to get away with not specifying the bundle name. Additionally, since your ViewController class appears to have the same name as the xib, Xcode is smart enough to let you get away with not needing to specify that either.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
BNRReminderViewController *rvc = [[BNRReminderViewController alloc] init];
[self.window setRootViewController:rvc];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}

You don't have to call mainBundle. It might be nil. This code might be solution:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.dashboardVC = [[DashboardViewController alloc] initWithNibName:#"DashboardViewController" bundle:nil];
UINavigationController * nc = [[UINavigationController alloc] initWithRootViewController:self.dashboardVC];
self.window.rootViewController = nc;
[self.window makeKeyAndVisible];
return YES;
}
Otherwise, did you create app with Storyboard? If you start with Storyboard, you have to remove the line below:

Related

Error: Manually adding the rootViewController's view to the view hierarchy is no longer supported

I upgraded to Xcode 11.1 and trying to compile an older Objective C app that programmatically sets the rootViewController.
How do I work around this error?
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
lvc = [[LoginViewController alloc] initWithNibName:nil bundle:nil];
nav = [[UINavigationController alloc] initWithRootViewController:lvc];
nav.navigationBar.barTintColor = [UIColor whiteColor];
self.window.rootViewController = nav;
[self.window addSubview:nav.view];
return YES;
}
See the line [self.window addSubview:nav.view];? Delete it. It was always wrong.

Navigation Controller not working when enabling ARC in xcode 5 and using ios 7

I enabled ARC and using ios 7 for my app.With out using xib i am doing programming.But i am unable to navigate from one view controller to another view controller.Created obj in .h file for a class.
In .h file
#property(nonatomic,strong)CountriesViewController *countryViewController;
In .m file in a button action.
countryViewController = [[CountriesViewController alloc] init];
[self.navigationController pushViewController:countryViewController animated:YES];
You need to add navigation Controller in AppDelegate like this,
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.viewController = [[YourViewController alloc] initWithNibName:#"YourViewController" bundle:nil];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
return YES;
}
In your ViewController, navigate like this,
self.countryViewController = [[CountriesViewController
alloc]initWithNibName:#"CountriesViewController" bundle:nil];
[self.navigationController pushViewController:self.countryViewController animated:YES];

Sigabrt error in [self.window makeKeyAndVisible] while running the app on ipad?

I am having an error in the following lines if code. This error is being generated when I run the app on ipad its working fine on the simulator.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
self.viewController = [[[ViewController alloc] initWithNibName:#"ViewController" bundle:nil] autorelease];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
Please elaborate the reasons why this error is being generated.
Kindly check the Target... if you have set or not, and also the proper one.
And one suggestion, try to adopt ARC. That will make you feel ReeeelaaaaXXXX!!!

Open other app using url schemes

I just created test app and wanted to lunch it from other app.
In both apps added Url type and schemes. Then from one app called:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"Test://test_page/"]];
This worked but i got a warning:
Application windows are expected to have a root view controller at the end of application launch
What i am missing here ?
Thanks
EDIT: Added test app lunch method:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
MyViewController *mainView = [[MyViewController alloc] init];
UINavigationController *navControl = [[UINavigationController alloc] initWithRootViewController: mainView];
[self.window setRootViewController: navControl];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
You should register URL schemes in .plsit file. Please, see this link

Application windows are expected to have a root view controller at the end of application launch error

I keep getting this error, and I cannot figure out why. Everything was working fine, and all of the sudden I got this error. I had not changed the code, and I set the rootViewController in the delegate, so I don't see why I am getting this error. Here is the code from my appDelegate.m file.
#import "WhereAmIAppDelegate.h"
#import "WhereAmIViewController.h"
#implementation WhereAmIAppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
WhereAmIViewController *wvc = [[WhereAmIViewController alloc] initWithNibName:#"WhereAmIViewController" bundle:nil];
[[self window]setRootViewController:wvc];
[self.window makeKeyAndVisible];
return YES;
}
I clearly set the rootViewController, so I don't know why this is happening.