【问题标题】:Initiating a view properly with navigation bar使用导航栏正确启动视图
【发布时间】:2014-03-28 17:51:36
【问题描述】:

我的应用由导航控制器和视图控制器组成。我的 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 但没有导航栏。

【问题讨论】:

    标签: ios objective-c navigation initialization appdelegate


    【解决方案1】:

    看起来您已将 rootViewController 设置为 SWRevealViewController 类型的自定义控制器。如果要在 rootViewController 上调用 pushViewController:animated:,它必须是 UINavigation 控制器。目前,您正在一个不响应该消息的控制器上调用它。

    确保在您的 Storyboard 中将 UINavigationController 拖到画布上,并在 InterfaceBuilder 中移动 root-view-controller 箭头以指向它。然后将您想要的任何 ViewController 加载到 navigationController 中。 (即问题顶部的代码应该可以工作)。

    【讨论】:

    • 我刚刚尝试过,但由于应用程序的要求,我需要该应用程序的侧边栏菜单。为了使侧边栏菜单起作用,SWRevealViewController 必须设置为 rootViewController。
    • 那么 UINavigationController 在哪里?你必须调用 pushViewController:animated: on something....
    • 我处理类似设置的一种方法是让我的自定义 ViewController 成为 rootViewController,并在 IB 中将 ContainerView 放入其中,然后将其设置为嵌入 segue 以嵌入 NavigationController。在 prepareForSegue 上,我检查它是否是嵌入 segue,然后将对destinationViewController 的引用保存为navigationController,并将自己设置为它的委托,以便我知道所有其他控制器何时被推送和弹出。
    • 我对containerView不是很熟悉,能简单解释一下在里面放一个containerView是什么意思吗?
    • 这是IB中的特殊视图。在对象浏览器(拖出视图、标签、按钮等)中,在搜索中键入“container”,您将看到一个名为“Container View”的特殊对象。将其中一个拖出作为 RootViewController 中的子视图。然后从 Container View 按住 ctrl-drag 到你的 UINavigationController 并选择“embed”。这会自动将导航控制器添加为容器控制器的子视图控制器。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-27
    • 1970-01-01
    • 1970-01-01
    • 2011-07-26
    相关资源
    最近更新 更多