[英]Initiating a view properly with navigation bar
我的应用程序由导航控制器和视图控制器组成。 我的 UI 的某些部分是在故事板上完成的,有些是从代码(在 viewDidLoad 中)启动的。 我的目标是在通过 appDelegate 中的此方法从推送通知“正确”启动应用程序时加载 childViewController X(由后退按钮、导航栏、标签和表格组成):
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
这对你们来说可能是一个简单的问题,但几天前我已经问了几个问题(你可以看看我的问题历史)。 尝试了几种方法后,我的应用程序要么:
崩溃加载没有导航栏加载导航栏但没有标签(后退按钮不起作用)加载导航栏但下面有黑屏只有tableView被拖到故事板上。 导航栏是推断出来的,但我有指示其后退按钮和标题的代码。 其余的都是通过 .m 文件中的代码完成的。
我知道以前有人问过这个问题,但没有一种方法有效。 如何通过推送通知正确加载这个 childViewController?
到目前为止我的代码:
- (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);
}}
错误代码:
'-[SWRevealViewController pushViewController:animated:]: unrecognized selector sent to instance 0x14575f20'
SWRevealViewController 是我的侧边栏菜单视图控制器库。
我也试过这个方法:
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];
这会加载正确的 viewController 但没有导航栏。
看起来您已将 rootViewController 设置为 SWRevealViewController 类型的自定义控制器。 如果你想在 rootViewController 上调用 pushViewController:animated:,它必须是一个 UINavigation 控制器。 当前,您在不响应该消息的控制器上调用它。
在 Storyboard 中,确保将 UINavigationController 拖到画布上,并在 InterfaceBuilder 中移动 root-view-controller 箭头以指向它。 然后将您想要的任何 ViewController 加载到 navigationController 中。 (即您问题顶部的代码应该可以工作)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.