[英]Moving between UIViewControllers without Segue
I have a storyboard that has 8 UIViewController
s on it (and counting). 我有一个故事板,上面有8个
UIViewController
(并且正在计数)。 I need to be able to move between any pair of these on demand. 我需要能够按需在任何一对之间移动。 Essentially, what I am trying to do is to mimic the functionality of a
UITabBarController
. 基本上,我想要做的是模仿
UITabBarController
的功能。 Since there are so many of these, I can't just use the UITabBarController
(that would solve my problem immediately), so I am using a UIPopoverController
with an embedded UITableView
to allow the user to choose their destination. 由于有这么多,我不能只使用
UITabBarController
(这将立即解决我的问题),所以我使用UIPopoverController
与嵌入式UITableView
允许用户选择他们的目的地。
Of course, I could create segues on the storyboard, but to set up all of the segues that I need currently, I would have to create 56 such segues just for the view controllers that I have right now, and potentially many more as things grow. 当然,我可以在故事板上创建segue,但是为了设置我目前需要的所有segue,我将不得不为我现在拥有的视图控制器创建56个这样的segue,并且随着事物的增长可能会有更多。
One idea that I had was to create a 'hub' view controller that doesn't really do anything other than to hold the segues to each of the 'real' VCs and be the delegate of each of those VCs. 我的一个想法是创建一个“中心”视图控制器,除了将每个“实际”VC保持segue并成为每个VC的代表之外,它实际上并没有做任何事情。 Then, when I want to switch between VCs, I first dismiss the view controller and pass the segue name for the destination back to the hub.
然后,当我想在VC之间切换时,我首先关闭视图控制器并将目标的segue名称传递回集线器。 The hub can then immediately perform that segue, which gets me where I want.
然后集线器可以立即执行该segue,这可以让我到达我想要的位置。 I think that this would work, but is seems sort of hackish.
我认为这会起作用,但似乎有点像黑客。
Does anyone know how if there is a better way to accomplish this? 有谁知道如果有更好的方法来实现这一目标? Thank you!
谢谢!
Why don't you give each one a storyboard identifier, then grab anyone you need with its identifier using this: 为什么不为每个人提供一个故事板标识符,然后使用以下标识获取您需要的任何人的标识符:
UIViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"identifier"];
Then present this view controller via push or present modally 然后通过push或present模式呈现此视图控制器
You can easily do this with performSegueWithIdentifier:
您可以使用
performSegueWithIdentifier:
轻松完成此操作performSegueWithIdentifier:
[self performSegueWithIdentifier:@"myCustomSegue" sender:self];
And then in the prepareForSegue:
method, configure what you need to: 然后在
prepareForSegue:
方法中,配置您需要的内容:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"myCustomSegue"]) {
MyCustomViewController *customViewController = segue.destinationViewController;
customViewController.customAttribute = @"something";
}
}
You can also use shouldPerformSegueWithIdentifier:
to control navigation. 您还可以使用
shouldPerformSegueWithIdentifier:
来控制导航。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.