简体   繁体   中英

How can I load a view “properly” iOS

My app consists of a navigation controller and view controllers. Some parts of my UI is done on storyboard and some are initiated from code (in viewDidLoad). My goal is to load childViewController X (consists of a back button, nag bar, label and table) when the app is launched from a push notification "properly" through this method in appDelegate:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

This might be a simple question to you guys but I've asked several questions the pass few days (you can look at my questions history). After trying several methods, my app either:

  • crashes
  • loads without navigation bar
  • loads with navigation bar but no label (back button does not work)
  • loads with navigation bar but with black screen underneath

Only the tableView is dragged onto storyboard. The navigation bar is inferred but I have code that dictates its back button and title. The rest is all done through code in the .m file.

I know people have asked this question before but none of the methods worked. How can I load this childViewController properly through a push notification?

EDIT/UPDATE:

My code so far:

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSDictionary *dictionary = [launchOptions     objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];

if (dictionary != nil)
{

UIStoryboard *mainstoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
UIViewController* firstVC = [mainstoryboard instantiateViewControllerWithIdentifier:@"NotificationsViewController"];
[self.window.rootViewController addChildViewController:firstVC];
[(UINavigationController *)self.window.rootViewController pushViewController:firstVC animated:NO];
self.window.rootViewController.childViewControllers);
}}

Error Code:

'-[SWRevealViewController pushViewController:animated:]: unrecognized selector sent to instance 0x14575f20'

SWRevealViewController is my library for my sidebar menu view controller.

UPDATE2: I've also tried this method:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
UIViewController *initViewController = [storyboard instantiateViewControllerWithIdentifier:@"NotificationsViewController"];

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

This loads the correct viewController but without a navigation bar.

Try this:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    NSDictionary *dictionary = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];

    if(dictionary != nil) {
        UIStoryboard *mainstoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
        UIViewController *firstVC = [mainstoryboard instantiateViewControllerWithIdentifier:@"NotificationsViewController"];
        UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;

        // Navigation already created
        if(navigationController) {
            [navigationController pushViewController:firstVC animated:NO];

        // Navigation not created
        } else {
            UINavigationViewController *navigationController = [[UINavigationViewController alloc] initWithRootController:firstVC];

            [self.window setRootViewController:firstVC];
            [self.window makeKeyAndVisible];
        }
    }

    return YES;
}

You had some problem in your code:

  1. You need to have a UINavigationController in order to have a navigation bar and manage the push/pop actions, so your window root controller should be a UINavigationController .

  2. If you need to initialize a UINavigationController the best and simple way is to initialize it with a root controller directly, so leave the push/pop for user actions: an application has always a root controller, so why don't you create it immediately?

  3. The " addChildController " is another thing: it is used to create custom container controller, it means that you have to implement all the business logic manually. I suggest you to use it only if you are experienced enough, since it could be difficult - Cocoa has enough components to leave it for a small set of applications.

  4. You are using a third part controller, the " SWRevealViewController ". They should have an example project, you can try to "copy" it and them customize it for your own purpose.

Let me know if your code works well, otherwise post the new error and I'll try to help you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM