我有一个通用的UIViewController所有的uiviewscontroller都扩展了它来重用一些通用的操作。

我想在这个Common UIViewController上建立一个segue这样所有其他的UIViewController都会继承。

我想知道如何通过编程来实现。

我想这个问题也可能是我如何为我所有的uiviewcontroller设置segue而不需要进入故事板手工操作。


当前回答

我一直在使用这段代码来实例化我的自定义segue子类并以编程方式运行它。这似乎很有效。这有什么问题吗?我很困惑,看到所有其他的答案都说这是不可能的。

UIViewController *toViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"OtherViewControllerId"];
MyCustomSegue *segue = [[MyCustomSegue alloc] initWithIdentifier:@"" source:self destination:toViewController];
[self prepareForSegue:segue sender:sender];
[segue perform];

其他回答

我一直在使用这段代码来实例化我的自定义segue子类并以编程方式运行它。这似乎很有效。这有什么问题吗?我很困惑,看到所有其他的答案都说这是不可能的。

UIViewController *toViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"OtherViewControllerId"];
MyCustomSegue *segue = [[MyCustomSegue alloc] initWithIdentifier:@"" source:self destination:toViewController];
[self prepareForSegue:segue sender:sender];
[segue perform];

你必须将你的代码链接到你正在使用的UIStoryboard。确保你进入UIStoryboard中的YourViewController,点击它周围的边框,然后设置它的标识符字段为你在代码中调用的NSString。

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" 
                                                     bundle:nil];
YourViewController *yourViewController = 
 (YourViewController *)
  [storyboard instantiateViewControllerWithIdentifier:@"yourViewControllerID"];
[self.navigationController pushViewController:yourViewController animated:YES];

实际上有几个问题:

首先,在你为我们上传的那个项目中,segue没有“segue1”标识符:

没有标识符

如果您还没有填写标识符,您应该填写。

第二,当你从表视图推到表视图时,你在调用initWithNibName来创建一个视图控制器。你需要使用instantiateViewControllerWithIdentifier。

你可以创建UIStoryBoardSegue的子类。子类化主要用于提供自定义过渡动画。

你可以看到wwdc 2011介绍StoryBoard的视频。YouTube上也有。

http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIStoryboardSegue_Class/Reference/Reference.html#//apple_ref/occ/cl/UIStoryboardSegue

我猜这个问题已经得到了回答和接受,但我只是想再补充一些细节。

我解决了一个问题,我将登录视图作为第一个屏幕,然后想要segue到应用程序,如果登录是正确的。我创建了从登录视图控制器到根视图控制器的segue并给它一个像myidentifier这样的标识符。

然后检查所有登录代码后,如果登录是正确的,我会打电话

[self performSegueWithIdentifier: @"myidentifier" sender: self];

我最大的误解是,我试图把segue放到一个按钮上,一旦找到segue就会中断。