推送问题 UINavigationController
Push Issue UINavigationController
我 运行 在尝试从一个 UINavigationController 推送到另一个时遇到了问题。
我到达我的 CameraViewController - 它通过在不同的视图控制器中选择一个按钮嵌入了 UINavigationController,如下所示:
- (void)goToCamera {
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
CameraViewController *camera = [storyboard instantiateViewControllerWithIdentifier:@"CameraView"];
[self presentViewController:camera animated:YES completion:^{
}];
}
从那里我尝试通过推送到我的 PublishViewController - 它没有嵌入 UINavigationController 因为我认为如果来自另一个 UINavigationController 你不需要一个。
我试着这样做:
- (void)goToPublish {
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
PublishViewController *publishView = [storyboard instantiateViewControllerWithIdentifier:@"PublishView"];
[self.navigationController pushViewController:publishView animated:YES];
}
知道为什么这不起作用吗?控制器从不推动。从我最初使用相机时嵌入是否仍然有效,还是我也需要初始化 UINavigationController?
这是我的故事板:
我觉得这很好,所以我怀疑你那里有一个 nil 对象。添加一些断言以确保您在预期的地方处理对象:
- (void)goToPublish {
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
assert(storyboard);
PublishViewController *publishView = [storyboard instantiateViewControllerWithIdentifier:@"PublishView"];
assert(publishView);
assert(self.navigationController); // this is probably the cause
[self.navigationController pushViewController:publishView animated:YES];
}
我认为如果给它一个 nil 视图参数,pushViewController 消息会呕吐,所以我认为主要嫌疑人是 self.navigationController.
我认为这里的问题是,当您通过标识初始化 UIViewController 时,您只会获得该视图控制器的实例,而不是它嵌入的 UINavigationController。
要解决您的问题,您需要实例化并呈现导航控制器(其中嵌入了 CameraViewController)。
虽然有很多方法可以做到这一点,但我建议您考虑放弃显式调用 [storyboard instantiateViewControllerWithIdentifier:],而是执行以下操作:
- 将您的导航控制器(或初始应用视图控制器)设置为 'Initial View Controller'。例如。
在 CameraViewController 和 PublishViewController 之间添加一个 segue(有关如何执行此操作的详细信息,请参见此处 https://developer.apple.com/library/ios/recipes/xcode_help-IB_storyboard/chapters/StoryboardSegue.html)。
如果您没有将 segue 直接连接到您在 Interface Builder 中设置 'Storyboard segue identify' 的按钮,可以通过调用 performSegueWithIdentifer 手动触发它:例如
- (void)goToPublish {
[self performSegueWithIdentifier:@"Go To Publish" sender:self];
}
这种方法将确保 UINavigationController 正确初始化,并且还可以减少代码,这通常是一个胜利:)。
当您呈现一个名为 VC1 的 viewController 时,除非您呈现一个 navigationController,否则 VC1 的 navigationController 必须为 nil。
你应该知道vc.navigationController不是有人(UINavigationController)推送的nil,或者是UINavigationController的rootViewController(其实nav的rootViewController也是被推送到nav的stack)。
您应该创建一个新的 navigationController。
- (void)goToCamera {
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
CameraViewController *camera = [storyboard instantiateViewControllerWithIdentifier:@"CameraView"];
UINav *nav = UINav alloc]initWithRootVC:camera];
[self presentViewController:nav animated:YES completion:^{
}];
}
我 运行 在尝试从一个 UINavigationController 推送到另一个时遇到了问题。
我到达我的 CameraViewController - 它通过在不同的视图控制器中选择一个按钮嵌入了 UINavigationController,如下所示:
- (void)goToCamera {
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
CameraViewController *camera = [storyboard instantiateViewControllerWithIdentifier:@"CameraView"];
[self presentViewController:camera animated:YES completion:^{
}];
}
从那里我尝试通过推送到我的 PublishViewController - 它没有嵌入 UINavigationController 因为我认为如果来自另一个 UINavigationController 你不需要一个。
我试着这样做:
- (void)goToPublish {
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
PublishViewController *publishView = [storyboard instantiateViewControllerWithIdentifier:@"PublishView"];
[self.navigationController pushViewController:publishView animated:YES];
}
知道为什么这不起作用吗?控制器从不推动。从我最初使用相机时嵌入是否仍然有效,还是我也需要初始化 UINavigationController?
这是我的故事板:
我觉得这很好,所以我怀疑你那里有一个 nil 对象。添加一些断言以确保您在预期的地方处理对象:
- (void)goToPublish {
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
assert(storyboard);
PublishViewController *publishView = [storyboard instantiateViewControllerWithIdentifier:@"PublishView"];
assert(publishView);
assert(self.navigationController); // this is probably the cause
[self.navigationController pushViewController:publishView animated:YES];
}
我认为如果给它一个 nil 视图参数,pushViewController 消息会呕吐,所以我认为主要嫌疑人是 self.navigationController.
我认为这里的问题是,当您通过标识初始化 UIViewController 时,您只会获得该视图控制器的实例,而不是它嵌入的 UINavigationController。
要解决您的问题,您需要实例化并呈现导航控制器(其中嵌入了 CameraViewController)。
虽然有很多方法可以做到这一点,但我建议您考虑放弃显式调用 [storyboard instantiateViewControllerWithIdentifier:],而是执行以下操作:
- 将您的导航控制器(或初始应用视图控制器)设置为 'Initial View Controller'。例如。
在 CameraViewController 和 PublishViewController 之间添加一个 segue(有关如何执行此操作的详细信息,请参见此处 https://developer.apple.com/library/ios/recipes/xcode_help-IB_storyboard/chapters/StoryboardSegue.html)。
如果您没有将 segue 直接连接到您在 Interface Builder 中设置 'Storyboard segue identify' 的按钮,可以通过调用 performSegueWithIdentifer 手动触发它:例如
- (void)goToPublish { [self performSegueWithIdentifier:@"Go To Publish" sender:self]; }
这种方法将确保 UINavigationController 正确初始化,并且还可以减少代码,这通常是一个胜利:)。
当您呈现一个名为 VC1 的 viewController 时,除非您呈现一个 navigationController,否则 VC1 的 navigationController 必须为 nil。
你应该知道vc.navigationController不是有人(UINavigationController)推送的nil,或者是UINavigationController的rootViewController(其实nav的rootViewController也是被推送到nav的stack)。
您应该创建一个新的 navigationController。
- (void)goToCamera {
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
CameraViewController *camera = [storyboard instantiateViewControllerWithIdentifier:@"CameraView"];
UINav *nav = UINav alloc]initWithRootVC:camera];
[self presentViewController:nav animated:YES completion:^{
}];
}