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

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

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

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


当前回答

对于故事板中的控制器。

Jhilgert00是你要找的东西吗?

-(IBAction)nav_goHome:(id)sender {
UIViewController *myController = [self.storyboard instantiateViewControllerWithIdentifier:@"HomeController"];
[self.navigationController pushViewController: myController animated:YES];

}

还是……

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

其他回答

根据定义,segue不能独立于故事板而存在。它甚至存在于类名中:UIStoryboardSegue。你不能通过编程方式创建segue——是故事板运行时为你创建它们。你通常可以在你的视图控制器代码中调用performSegueWithIdentifier:,但这依赖于在故事板中已经设置了一个segue来引用。

我认为你问的是如何在你的公共视图控制器(基类)中创建一个方法,它将转换到一个新的视图控制器,并将被所有派生类继承。你可以为你的基类视图控制器创建一个这样的方法:

- (IBAction)pushMyNewViewController
{
    MyNewViewController *myNewVC = [[MyNewViewController alloc] init];

    // do any setup you need for myNewVC

    [self presentModalViewController:myNewVC 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

我对UIStoryboard的segue进行了逆向工程,并做了一个开源(重新)实现:https://github.com/acoomans/Segway

有了这个库,就可以编程地定义segue(不需要任何故事板)。

希望能有所帮助。

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

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