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

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

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

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


当前回答

我想我可以加上另一种可能性。你能做的一件事是你能用一个没有附加到动作的segue连接故事板中的两个场景,然后在你的视图控制器中以编程方式触发这个segue。你这样做的方法是,你必须从故事板场景底部的文件所有者图标拖拽到segue场景,然后右拖拽到目标场景。我将附上一张图片来帮助解释。

弹出窗口将显示“手动Segue”。我选择Push作为类型。点击小方块,确保你在属性检查器中。给它一个标识符,用于在代码中引用它。

接下来我要用一个程序化的栏按钮项进行segue。在viewDidLoad或其他地方,我将用下面的代码在导航栏上创建一个按钮项:

UIBarButtonItem *buttonizeButton = [[UIBarButtonItem alloc] initWithTitle:@"Buttonize"
                                                                    style:UIBarButtonItemStyleDone
                                                                   target:self
                                                                   action:@selector(buttonizeButtonTap:)];
self.navigationItem.rightBarButtonItems = @[buttonizeButton];

注意到选择器是buttonizeButtonTap:。所以为那个按钮写一个void方法在这个方法中你会像这样调用segue:

-(void)buttonizeButtonTap:(id)sender{
    [self performSegueWithIdentifier:@"Associate" sender:sender];
    }

当调用prepareForSegue时,需要sender参数来标识按钮。prepareForSegue是框架方法,你将实例化你的场景,并传递给它任何值,它将需要做它的工作。下面是我的方法:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"Associate"])
    {
        TranslationQuizAssociateVC *translationQuizAssociateVC = [segue destinationViewController];
        translationQuizAssociateVC.nodeID = self.nodeID; //--pass nodeID from ViewNodeViewController
        translationQuizAssociateVC.contentID = self.contentID;
        translationQuizAssociateVC.index = self.index;
        translationQuizAssociateVC.content = self.content;
    }
}

我测试过了,效果很好。

其他回答

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

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

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

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

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

我一直在使用这段代码来实例化我的自定义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不能在故事板之外创建。尽管有一些缺点,但您需要将其连接起来。

UIStoryboardSegue Reference明确指出:

你不能直接创建segue对象。相反,是故事板 运行时必须在两个视图之间执行segue时创建它们 控制器。类型仍然可以以编程方式启动segue performSegueWithIdentifier:sender: UIViewController的方法 想要的东西。您可以这样做,以从添加的源启动一个segue 因此在接口生成器中不可用。

你仍然可以通过编程的方式告诉故事板使用presentModalViewController:或pushViewController:animated:调用的segue来呈现一个视图控制器,但是你需要一个故事板实例。

你可以调用UIStoryboards类方法来获得一个命名的故事板,它带有主bundle的nil。

情节提要名称:捆绑包:

对于故事板中的控制器。

Jhilgert00是你要找的东西吗?

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

}

还是……

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