iOS 6 Preservation and Restoration without Storyboards - objective-c

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.

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.

how I use MFSideMenu?

I'm trying to create a MFSideMenu however I never used it and do not know what it missing. Can anyone write a short tutorial on how to finalize it?
-(void)viewDidLoad{
MFSideMenuContainerViewController *container = [MFSideMenuContainerViewController
containerWithCenterViewController:centerViewController
leftMenuViewController:leftMenuViewController
rightMenuViewController:rightMenuViewController];
self.window.rootViewController = container;
[self.window makeKeyAndVisible];
}
In AppDelegate.h
#import "MFSideMenuContainerViewController.h"
In AppDelegate.m
MFSideMenuContainerViewController *container = (MFSideMenuContainerViewController *)self.window.rootViewController;
UINavigationController *navigationController = [self.storyboard instantiateViewControllerWithIdentifier:#"navigationController"]; //I have instantiated using storyboard id.
UIViewController *leftSideMenuViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"sideMenuViewController"]; //I have instantiated using storyboard id.
[container setLeftMenuViewController:leftSideMenuViewController];
[container setCenterViewController:navigationController];
In Your controller.m add:
- (IBAction)menuPressed:(id)sender
{
[self.menuContainerViewController toggleLeftSideMenuCompletion:nil];
}
- (MFSideMenuContainerViewController *)menuContainerViewController
{
return (MFSideMenuContainerViewController*)self.navigationController.parentViewController;
}
Put this code into your AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// init center, left, and right view controllers
MFSideMenuContainerViewController *container = [MFSideMenuContainerViewController
containerWithCenterViewController:centerViewController]
leftMenuViewController:leftMenuViewController
rightMenuViewController:rightMenuViewController];
self.window.rootViewController = container;
[self.window makeKeyAndVisible];
return YES;
}

Loaded Xib file but nothing appears

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:

didSelectRowAtIndexPath when select can't turn to other pages

if(!_personViewController)
{
_personViewController=[[PersonViewController alloc]initWithNibName:#"PersonViewController" bundle:nil];
}
_personViewController.user=_user;
[self.navigationController pushViewController:_personViewController animated:YES];
[PersonViewController release];
This is my code. I just want to when select the row can jump to personViewController page but it seems bad.
Just check it whether you write navigation controller in - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions and it is AppDelegate.m file.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
self.viewController = [[[ViewController alloc] initWithNibName:#"ViewController_iPhone" bundle:nil] autorelease];
UINavigationController *navControl = [[UINavigationController alloc] initWithRootViewController:self.viewController];
[self.window addSubview:[navControl view]];
[self.window makeKeyAndVisible];
return YES;
}
try this..
//_personViewController.user=_user;
or
_personViewController.user=[_user retain];

iOS 5 Upgrade Needing rootViewController

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.