ios 以编程方式使用故事板推送视图控制器的替代方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9047146/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Alternative ways to push view controllers with storyboard programmatically
提问by yassassin
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:
我正在寻找以编程方式推送在情节提要上实例化的视图控制器的替代方法。我实际上找到了两种方法,用于转到下一个视图,但也用于返回到前一个视图:
using
pushViewController
:UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle: nil]; LocationWebView *lvc = [storyboard instantiateViewControllerWithIdentifier:@"LocationWebView"]; [self.navigationController pushViewController:lvc animated:YES];
performing
Segue
programmatically:[self performSegueWithIdentifier: @"SongSegue" sender: self];
使用
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.
您是否有一些关于替代方案和执行此操作的最佳方式的提示?请注意,我不是指模态视图。
回答by rickster
Apple recommends using performSegueWithIdentifier:sender:someObject
to programmatically perform segues. There are at least a couple of advantages to doing it this way:
Apple 建议使用performSegueWithIdentifier:sender:someObject
以编程方式执行转场。这样做至少有几个好处:
Less of your own codemeans 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.
More in IBcan 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.
减少自己的代码意味着让框架做更多的工作。如果 Apple 为推送转场提供了一些超酷的新视觉效果,或者提高了性能,或者修复了未来某个 iOS 版本中的错误,您的应用程序可以免费获得。你编写的每一行代码来完成框架可以为你做的工作,都会增加你编写错误的机会。
更多的IB意味着更容易改变。如果您决定要将所有推送转场更改为模态转场,或某些自定义转场类型,您可以在其中制作自己的超酷视觉效果,您可以在 IB 中全部选择它们,然后一键更改转场类型而不是寻找在你的代码中。
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:
此外,无论您使用第一种方法还是第二种方法,推送视图控制器以“返回”到以前的视图控制器都不会按照用户期望的方式工作。当您推送 a 时SongViewController
,它会被添加到导航堆栈的末尾:
LocationViewController -> SongViewController
If you then push LocationViewController
again to go "back":
如果您再次按下LocationViewController
以“返回”:
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.)
如果用户点击导航栏中的后退按钮,他们将再次从位置视图返回到歌曲视图再到位置视图。(另外,如果你继续这样做,每一个“后退”和“前进”都会增加一个不断增加的视图控制器链,这可能会导致其他问题。)
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.
相反,您应该让导航控制器处理返回。为此,它在导航栏中放置了一个按钮,该按钮应该可以处理大多数用例。如果您需要让自己的控件返回,则无法在带有 iOS 5 的 IB 中执行此操作,但您可以使用导航控制器的popViewControllerAnimated:
方法以编程方式执行此操作。
回答by gfrigon
Personally, I use the #2 when I am in a UIViewController that is under a UINavigationController. I don't think it will work it there is no UINavigationController.
就个人而言,当我在 UINavigationController 下的 UIViewController 中时,我使用 #2。我不认为它会起作用,因为没有 UINavigationController。
When I am using a view that is under a UITabBarController, I use the following code to transition to another tab:
当我使用位于 UITabBarController 下的视图时,我使用以下代码转换到另一个选项卡:
NSUInteger tabIndex = 2; // Index of the tab I want to select
UIViewController * viewCtrl = [_tabController.viewControllers objectAtIndex:tabIndex];
_tabController.selectedViewController = viewCtrl;