以编程方式使用故事板推送视图控制器的替代方法 [英] Alternative ways to push view controllers with storyboard programmatically

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

问题描述

我正在寻找以编程方式推送在情节提要上实例化的视图控制器的替代方法.我实际上找到了两种方法,用于转到下一个视图,也用于返回到上一个视图:

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 以编程方式执行转场.这样做至少有几个好处:

    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 中全部选择它们,然后一键更改转场类型而不是寻找在您的代码中.

    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天全站免登陆