简体   繁体   中英

Initiating a view properly with navigation bar

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?

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.

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.

It looks like you have your rootViewController set to a custom controller of type SWRevealViewController. If you want to call pushViewController:animated: on rootViewController, it must be a UINavigation controller. Currently you are calling it on a controller that does not respond to that message.

In your Storyboard make sure you drag a UINavigationController onto the canvas and move the root-view-controller arrow in InterfaceBuilder to point to it. Then load whatever ViewController you want into the navigationController. (ie your code at the top of your question should work).

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