以编程方式通过情节提要推送视图控制器的替代方法 [英] Alternative ways to push view controllers with storyboard programmatically

查看:67
本文介绍了以编程方式通过情节提要推送视图控制器的替代方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找其他方式以编程方式推送在情节提要上实例化的视图控制器. 实际上,我发现了两种方法,可以用来进入下一个视图,还可以回到上一个视图:

I'm looking for alternative ways to push view controllers instantiated on storyboard programmatically. I've actually found two ways, that I use to go to the next view, but also to go back to the previous:

  1. 使用pushViewController:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle: nil]; 
LocationWebView *lvc = [storyboard instantiateViewControllerWithIdentifier:@"LocationWebView"];
[self.navigationController pushViewController:lvc animated:YES];

  • 以编程方式执行Segue:

    [self performSegueWithIdentifier: @"SongSegue" sender: self];
    

  • 您对替代方法以及应该执行此操作的最佳方式有一些提示吗?请注意,我指的不是模式视图.

    Do you have some hints on alternatives and on the best way this operation should be performed? Note that I'm not referring to modal views.

    推荐答案

    Apple建议使用performSegueWithIdentifier:sender:someObject以编程方式执行segue.这样做至少有两个优点:

    Apple recommends using performSegueWithIdentifier:sender:someObject to programmatically perform segues. There are at least a couple of advantages to doing it this way:

    • 缺少自己的代码意味着您要让框架完成更多工作.如果Apple提供了一些超酷的新视觉效果来进行推送,或者提高了性能,或者修复了将来的某些iOS版本中的错误,则您的应用可以免费获得.而且您编写完成该框架工作所需的每一行代码都会为您增加编写错误的机会.

    • Less of your own code means you're letting the framework do more of the work. If Apple comes up with some super-cool new visual effect for push segues, or improves performance, or fixes a bug in some future iOS release, your app can get it for free. And every line of code you write to do work the framework could be doing for you increases your chance of writing bugs.

    IB中的更多内容可能意味着更容易更改.如果您决定要将所有推式按钮更改为模态按钮,或者将某些自定义按钮类型更改为自己的超酷视觉效果,则可以在IB中选择"em all",然后单击一下即可更改按钮类型,而不是搜寻在您的代码中.

    More in IB can mean easier to change. If you decide you want to change all your push segues to modal segues, or some custom segue type where you do your own super-cool visual effect, you can select 'em all in IB and change the segue type with one click instead of hunting around in your code.

    此外,无论您使用第一种方法还是第二种方法,推动视图控制器以返回"到先前的视图控制器都不会像您的用户期望的那样工作.按下SongViewController时,它会添加到导航堆栈的末尾:

    In addition, whether you use the first or second method, pushing a view controller in order to go "back" to a previous view controller won't work the way your users expect. When you push a SongViewController, it's added to the end of the navigation stack:

    LocationViewController -> SongViewController
    

    如果您随后再次按LocationViewController键以返回":

    If you then push LocationViewController again to go "back":

    LocationViewController -> SongViewController -> LocationViewController
    

    如果用户点击导航栏中的后退按钮,他们将再次从位置视图返回到歌曲视图,再回到位置视图. (此外,如果继续这样做,则每个后退"和前进"都会增加视图控制器的链条,这可能会导致其他麻烦.)

    If the user taps the back button in the navigation bar, they'll go back from the location view to the song view to the location view again. (Also, if you keep doing this, every "back" and "forward" will add to an ever-increasing chain of view controllers, which might lead to other trouble.)

    相反,您应该让导航控制器处理回溯.为此,它在导航栏中放置了一个按钮,该按钮应该可以处理大多数用例.如果您需要使自己的控件返回,则无法在iOS 5的IB中执行此操作,但可以使用导航控制器的popViewControllerAnimated:方法以编程方式进行操作.

    Instead, you should let the navigation controller handle going back. It puts a button in the navigation bar for that purpose, which should handle most use cases. If you need to make your own control to go back, you can't do this in IB with iOS 5, but you can do it programmatically with the navigation controller's popViewControllerAnimated: method.

    这篇关于以编程方式通过情节提要推送视图控制器的替代方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆