ios 从一个故事板转移到另一个故事板?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18777627/
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
Segue from one storyboard to a different storyboard?
提问by BrownEye
I have too many views on one storyboard which is causing it to run really slow. I have been told that a solution to this issue would be to split the one storyboard into multiple storyboards. Could anyone tell me how I can segue from a view on storyboard 1 to a view in storyboard 2 via a button?
我对一个故事板有太多的看法,这导致它运行得很慢。有人告诉我,此问题的解决方案是将一个故事板拆分为多个故事板。谁能告诉我如何通过按钮从故事板 1 上的视图切换到故事板 2 中的视图?
采纳答案by BrownEye
I tried everything I had read but still had no success. I've managed to get it working using Rob Browns Storyboard LinkIt's easy to implement and works really fast
我尝试了我读过的所有内容,但仍然没有成功。我已经使用Rob Browns Storyboard Link设法让它工作了 它很容易实现并且工作得非常快
回答by Nycen
回答by metamorph2
In Swift (iOS 8.1) this is pretty easy:
在 Swift (iOS 8.1) 中,这很容易:
var storyboard: UIStoryboard = UIStoryboard(name: "Another", bundle: nil)
var vc = storyboard.instantiateViewControllerWithIdentifier("NextViewController") as AnotherViewController
self.showViewController(vc, sender: self)
Update for Swift 3:
Swift 3 更新:
let storyboard: UIStoryboard = UIStoryboard(name: "Another", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "NextViewController") as! AnotherViewController
self.show(vc, sender: self)
回答by Inder Kumar Rathore
回答by Nocross
Another solution using segues(iOS SDK 6.0+), which keeps code separated by purpose and leaves room for customisation:
另一种使用segues(iOS SDK 6.0+) 的解决方案,它使代码按目的分开并为自定义留出空间:
- (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender {
//check/validate/abort segue
return YES;
}//optional
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
//sender - segue/destination related preparations/data transfer
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UIViewController *destination = [[UIStoryboard storyboardWithName:@"SomeStoryboard" bundle:nil] instantiateInitialViewController];
UIStoryboardSegue *segue = [UIStoryboardSegue segueWithIdentifier:@"identifier" source:self destination:destination performHandler:^(void) {
//view transition/animation
[self.navigationController pushViewController:destination animated:YES];
}];
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
[self shouldPerformSegueWithIdentifier:segue.identifier sender:cell];//optional
[self prepareForSegue:segue sender:cell];
[segue perform];
}
Note:UITableViewCell *cell
is used as sender
to keep defaultTableViewController
response behaviour.
注意:UITableViewCell *cell
用于sender
保持默认TableViewController
响应行为。
回答by Udit Agarwal
Finally XCode 7 has added this feature in which you can segue between view controllers in two different storyboards using interface builder. Till now, we had to do this programatically.
最后,XCode 7 添加了此功能,您可以使用界面构建器在两个不同故事板中的视图控制器之间进行切换。到目前为止,我们必须以编程方式执行此操作。
回答by Dan Fairaizl
First of all, breaking up a storyboard into multiple separate ones is a great idea, saves a lot of headache (especially if you are on a team and dealing with lots of merge conflicts in the storyboard file).
首先,将故事板分解成多个单独的故事板是一个好主意,可以省去很多麻烦(特别是如果您在一个团队中并处理故事板文件中的大量合并冲突)。
Now to answer your question - you cannot perform a segue between two storyboards necessarily, but one solution I've had great success with is to do something like this:
现在来回答你的问题——你不一定能在两个故事板之间执行转场,但我已经取得了巨大成功的一个解决方案是做这样的事情:
- (IBAction)buttonPressed:(id)sender {
UIViewController *otherVC = [[UIStoryboard storyboardWithName:@"SecondStoryboard" bundle:nil] instantiateInitialViewController]; //Or get a VC by its identifier
[self.navigationController pushViewController:otherVC animated:YES];
}
Just load up the other storyboard and either call instantiateInitialViewController
or instantiateViewControllerWithIdentifier:
then do whatever transition you would like.
只需加载另一个故事板,然后调用instantiateInitialViewController
或instantiateViewControllerWithIdentifier:
执行您想要的任何转换。
Hope this helps.
希望这可以帮助。
回答by Mingebag
Here is a simple Swift solution:
这是一个简单的 Swift 解决方案:
let viewController:UIViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("ViewController") as UIViewController
// .instantiatViewControllerWithIdentifier() returns AnyObject! this must be downcast to utilize it
self.presentViewController(viewController, animated: false, completion: nil)
回答by al_mota
Swift 3
斯威夫特 3
let vc = UIStoryboard(name: "StoryboardName", bundle: nil).instantiateViewController(withIdentifier: "ViewControllerIdentifier") as? ExpectedViewControllerClass
self.show(vc, sender: self)
Where the "StroboardName" is the name of your .storyboardfile. The "ViewControllerIdentifier" is the id of the View in the story board. And "self" is any UIViewController
其中“StroboardName”是您的 .storyboardfile 的名称。“ViewControllerIdentifier”是故事板中视图的ID。而“self”是任何 UIViewController
In my case, the identifier was "chooseCountryViewController"