从一个故事板到另一个故事板?
我对一个故事板有太多的看法,导致它运行得很慢。 我被告知,解决这个问题的办法是将一个故事板分成多个故事板。 任何人都可以告诉我,如何通过一个button从故事板1的视图继续到故事板2的视图?
使用Xcode 7,你可以selectStoryboard References
并设置目标故事板和控制器
在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)
将参考添加到另一个故事板 – Apple开发人员文档
另一个使用segues (iOS SDK 6.0+)的解决scheme,它保持代码分离的目的,留下空间进行自定义:
- (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
响应行为。
我尝试了所有我读过的东西,但仍然没有成功。 我已经设法使用罗布·布朗斯故事板链接它的工作很容易实现和工作速度非常快
最后,XCode 7添加了这个function,您可以使用界面构build器在两个不同的故事板中的视图控制器之间进行切换。 到目前为止,我们不得不通过编程来完成这个任务。
首先,将故事板分成多个单独的故事板是一个好主意,可以节省很多头痛(特别是如果你在一个团队中并处理故事板文件中的大量合并冲突)。
现在要回答你的问题 – 你不能在两个故事板之间进行一个循环,但是我已经取得了巨大成功的一个解决scheme就是做这样的事情:
- (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:
然后做任何你想要的过渡。
希望这可以帮助。
这是一个简单的Swift解决scheme:
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”是故事板中View的ID。 而“自我”是任何UIViewController
在我的情况下,标识符是“chooseCountryViewController”
- UIView上的UITapGestureRecognizer和它的子视图在子视图被抽头时一起响应
- iOS错误“无效部分请求”是什么意思?
- 带有SWRevealViewController的TabBarController
- 如何拦截button单击iOS内的UIWebview?
- IOS录音,如何在麦克风播放之前检查麦克风/播放是否忙?
- 为什么将GL_ELEMENT_ARRAY_BUFFER绑定到0会产生一个memmove错误?
- 如何在iTunesConnect中使用缺less构build的列表创build“Build”logging?
- 如何在游戏中存储用户数据? (swift,spritekit)
- UITableViewCell中的UIButton从UITableView中窃取触摸