iOS 5 Upgrade Needing rootViewController - objective-c

The App I created for iOS 4 does not work once run on iOS 5. I am receiving the following error:
"Applications are expected to have a root view controller at the end of application launch"
How can I update the following code to fix this error?
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UINavigationController *navcon = [[UINavigationController alloc] init]
TSViewController *pvc = [[TSViewController alloc] init];
[navcon pushViewController:pvc animated:NO];
[navcon setNavigationBarHidden:YES animated:NO];
[pvc release];
[window addSubview:navcon.view];
return YES;
}

[window setRootViewController:navcon]; instead of addSubview.

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];

Adding a UINavigationController

I'm trying to add a UINavigationController to my ViewController. And when I launch the app, it gives me just a black screen and dont init the app.
This is my AppDelegate.m :
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIViewController *rootView = [[ViewController alloc]
initWithNibName:#"ViewController"
bundle:nil];
self.navController = [[UINavigationController alloc] initWithRootViewController:rootView];
[[self window] setRootViewController:self.navController];
//template code
[self.window makeKeyAndVisible];
[rootView release];
return YES;
}
I'm following this article: http://simplecode.me/2011/09/04/an-introduction-to-uinavigationcontroller/
What's wrong? Thanks!
Try to use ARC adn storyboard If you are targeting IOS 5+, it would be a lot easier and you wouldn even have to code, just drag and drop the navigation controller to storyboard.
havent test but try this:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIViewController *rootView = [[ViewController alloc]
initWithNibName:#"ViewController"
bundle:nil];
UINavigationController *navigationController=[[UINavigationController alloc] initWithRootViewController:rootView];
self.window.rootViewController =nil;
self.window.rootViewController = navigationController;
[self.window makeKeyAndVisible];
return YES;
}
Just testing with a project and used this code...
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.viewController = [[CCViewController alloc] initWithNibName:#"CCViewController" bundle:nil];
UINavigationController *navControl = [[UINavigationController alloc] initWithRootViewController:self.viewController];
self.window.rootViewController = navControl;
[self.window makeKeyAndVisible];
return YES;
}
Worked fine.

iOS 6 Preservation and Restoration without Storyboards

I've been trying without any luck to implement state restoration and preservation.
I'm not using storyboards. I have to be doing something wrong.
I set up the restoration identifier for every Nib file.
I know the state is being stored, but never restored properly. The last state shows for half a second and then it goes back to the main view. I've tried many things with no luck. Please help!
-(BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSLog(#"will finish");
return YES;
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
self.viewController = [[[ViewController alloc] initWithNibName:#"ViewController" bundle:nil] autorelease];
navigator = [[UINavigationController alloc] initWithRootViewController:self.viewController];
self.window.rootViewController = navigator;
[self.window makeKeyAndVisible];
[navigator release];
NSLog(#"did finish");
return YES;
}
// As you can see, I started the process of saving and restoring application state.
// Also, I added the restoration identifier for every class that should be restored.
-(BOOL)application:(UIApplication *)application shouldRestoreApplicationState:(NSCoder *)coder
{
NSLog(#"restoring");
return YES;
}
-(BOOL)application:(UIApplication *)application shouldSaveApplicationState:(NSCoder *)coder
{
return YES;
}
-(void)application:(UIApplication *)application didDecodeRestorableStateWithCoder:(NSCoder *)coder
{
NSLog(#"restored");
//navigator = [[UINavigationController alloc] initWithRootViewController:];
isRestored = YES;
}
You should do your controllers initialization in application:willFinishLaunchingWithOptions: because restoration is done before application:didFinishLaunchingWithOptions: is called.
Check this answer.
Also you should programmatically assign restorationIdentifier to all controllers.

Can I use pushViewController with UISegmentedControl?

I want to use pushViewController with UIsegmentedController to push to other view and xib.
Can I do that in xcode 4 ??? If I can , Could you guide me please ?
Here is some code that I try.But won't work.
-(IBAction) segmentedControlIndexChanged:(id)sender
{
if ([sender selectedSegmentIndex] == 0)
{
BDshelveController *shelve_page = [[BDshelveController alloc] initWithNibName: #"BDshelveController" bundle:nil];
}
else if([sender selectedSegmentIndex] == 2)
{
BDlibraryController *lib_page = [[BDlibraryController alloc] initWithNibName:#"BDlibraryController" bundle:nil];
[self.navigationController pushViewController:lib_page animated:YES];
}
}
yes you can, these are the things you could check
you missed segmentedIndex=1 in your code.
you missed to call pushViewController when selected segment=0
Or it is more likely that self.NavigatonController is nil
Your segmented control has not been connected valuechange to your action
If self.navigationController is nil, it depends on how you initialize your app, if your app is navigation bar driven, then this code in your AppDelegate should resolve the issue:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:yourRootViewController]; // <-- depends if you have storyboards, xib etc...you should this already initialized
[self.window addSubview:navController.view];
[navController release]; // <-- depends if you have ARC
[self.window makeKeyAndVisible];
return YES;
}
Otherwise I suppose you can initialize it where you are, by giving self as root controller.
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:self];