我在一个Storyboard上有太多的视图,导致其运行非常缓慢。我被告知解决此问题的方法是将一个Storyboard拆分为多个Storyboard。请问如何通过按钮从Storyboard 1上的一个视图切换到Storyboard 2上的一个视图?
我在一个Storyboard上有太多的视图,导致其运行非常缓慢。我被告知解决此问题的方法是将一个Storyboard拆分为多个Storyboard。请问如何通过按钮从Storyboard 1上的一个视图切换到Storyboard 2上的一个视图?
在 Swift(iOS 8.1)中,这非常容易:
var storyboard: UIStoryboard = UIStoryboard(name: "Another", bundle: nil)
var vc = storyboard.instantiateViewControllerWithIdentifier("NextViewController") as AnotherViewController
self.showViewController(vc, sender: self)
Swift 3更新:
let storyboard: UIStoryboard = UIStoryboard(name: "Another", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "NextViewController") as! AnotherViewController
self.show(vc, sender: self)
另一种解决方案是使用 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];
}
注意: UITableViewCell *cell
被用作 sender
,以保持 默认的 TableViewController
响应行为。
(void)setViewControllers:(NSArray *)viewControllers animated:(BOOL)animated
- jbbenni我尝试了一切我读到的但仍然没有成功。 使用Rob Browns Storyboard Link, 我成功地使它工作起来了。 它易于实现并且非常快速。
- (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];
}
只需加载另一个故事板,然后调用instantiateInitialViewController
或instantiateViewControllerWithIdentifier:
,然后执行所需的任何转换。
希望这可以帮助您。
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)
Swift 3
let vc = UIStoryboard(name: "StoryboardName", bundle: nil).instantiateViewController(withIdentifier: "ViewControllerIdentifier") as? ExpectedViewControllerClass
self.show(vc, sender: self)
"StroboardName"是你的.storyboard文件的名称。 "ViewControllerIdentifier"是故事板中视图的ID。 而"self"则是任何UIViewController。