尝试将新视图控制器推送到现有导航控制器时出现问题。
我正在尝试的是在按下导航UIPopoverController
时显示UIBarButtonItem
,然后从该“下拉列表”中选择一个菜单点,将相关的视图控制器推送到“主” “导航控制器。
我尝试了以下内容,它提供了一个模态。但我希望推动观点。 如果选择push而不是modal,结果如下:
我也尝试过制作一个自定义UITableViewController
(在popover上),我尝试过以下代码:
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UINavigationController *dash = [storyboard instantiateViewControllerWithIdentifier:@"dash_nav"];
UIViewController *students = [storyboard instantiateViewControllerWithIdentifier:@"students"];
if (indexPath.row == 0) {
[dash pushViewController:students animated:YES];
// [[dash navigationController] presentViewController:students animated:YES completion:nil];
}
NSLog(@"%@", [dash title]);
NSLog(@"index = %i", indexPath.row);
}
有没有办法做我想要完成的事情?
答案 0 :(得分:0)
此代码:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UINavigationController *dash = [storyboard instantiateViewControllerWithIdentifier:@"dash_nav"];
UIViewController *students = [storyboard instantiateViewControllerWithIdentifier:@"students"];
正在创建太多新实例。您应该使用现有的故事板(self. storyboard
)和现有的导航控制器。导航控制器需要传递给表视图控制器(您应该使用它,因为故事板没有所需的信息)。我们将在表视图控制器上调用此originatingNavigationController
,一个新的@property
。
当segue触发显示弹出窗口时,将导航控制器引用设置为目标视图控制器(表视图)。
然后,在didSelectRowAtIndexPath:
方法中,您只需实例化students
VC并推送它:
UIViewController *students = [self.storyboard instantiateViewControllerWithIdentifier:@"students"];
[self.originatingNavigationController pushViewController:students animated:YES];
然后表视图控制器应该解除它自己(它的弹出)。