现在的位置: 首页 > 综合 > 正文

storyboard presentViewController pushViewController 跳转后黑屏 NavigationBar按钮push

2018年05月28日 ⁄ 综合 ⁄ 共 1218字 ⁄ 字号 评论关闭

环境:XCode5.1,iOS6样式,Storeboard

先在主页面建一个navigation view,然后在导航栏放了一个button,也称之为bar button item.

新建一个view controller(简称viewB),结果按住ctrl,拖动button到viewB可以建立Modal或者Push,运行就是没反应!

好吧,那就创建一个button的action,我习惯用VC++的命名:

- (IBAction)onBtnShowModal2:(id)sender {

}

这样仿真时可以发现按钮事件是调用的,那就可以用代码来跳转到ViewB啦。实现之:

//导航栏按钮弹出模态窗体
- (IBAction)onBtnShowModal2:(id)sender {
    //用pushviewcontroller可以出现导航栏,自动有返回按钮,黑屏
    RightBtnViewController *view = [[RightBtnViewController alloc] init];
    [self.navigationController pushViewController:view animated:YES];
    
    
}

结果发现,导航栏出现了,但是界面黑屏了!使用

[self presentViewController:view animated:YES completion:nil];

结果全部黑屏

好了,换一种方法:

点选ViewB的ViewController,然后右边identity inspector设置Storyboard ID,例如“RightWin”

//导航栏按钮弹出模态窗体
- (IBAction)onBtnShowModal2:(id)sender {
    RightBtnViewController *view = [self.storyboard instantiateViewControllerWithIdentifier:@"RightWin"];
    [self.navigationController pushViewController:view animated:YES];
    
}

这下OK了,就是创建view时的不一样!

使用[self presentViewController:view animated:YES completion:nil];也OK了,不过是全屏不带navigation bar的。可以使用

[self dismissViewControllerAnimated:YES completion:nil];返回!!!

//============华丽的分割线==============

其实上面说了那么多,无非证明一件事:拖动过去的button是可以用的,如果想实现导航栏按钮的导航功能,那这个做法就很傻逼了!正确做法如下:

把button换成bar button item(在控件的最下面),这样按住ctrl拖动,选择push就OK啦,一句话也不用写,是不是很傻逼?哈哈

抱歉!评论已关闭.