以编程方式创建segue

以编程方式创建segue

我有个共同点UIViewController我所有的UIViewsControllers扩展到重用一些常见操作。

我想在这个“公共”上建立一个UIViewController所以其他人UIViewControllers继承。

我正在努力弄清楚如何通过编程来实现这个目标。

我想问题也可能是如何设置segue为了我UIViewControllers而不是走进故事板用手做的。


暮色呼如
浏览 535回答 3
3回答

慕工程0101907

我想再增加一种可能性。您可以做的一件事是,您可以使用未附加到操作的segue连接故事板中的两个场景,然后以编程方式在视图控制器内触发segue。您这样做的方式,是您必须从文件的所有者图标在故事板场景的底部,这是一个敏感的场景,并右拖动到目标场景。我再加一张图片来解释。弹出窗口将显示“手动段”。我选推作为类型。点击小方块,确保你在属性检查器中。给它一个标识符,您将使用它在代码中引用它。好的,接下来我将使用一个编程栏按钮项目进行转换。在viewDidLoad或其他地方,我将使用以下代码在导航栏上创建一个按钮项:UIBarButtonItem *buttonizeButton = [[UIBarButtonItem alloc] initWithTitle:@"Buttonize"                                          style:UIBarButtonItemStyleDone                                 target:self   action:@selector(buttonizeButtonTap:)];self.navigationItem.rightBarButtonItems = @[buttonizeButton];好的,注意选择器是BuonizeButtonTap:。因此,为该按钮编写一个void方法,在该方法中,您将调用segue,如下所示:-(void)buttonizeButtonTap:(id)sender{     [self performSegueWithIdentifier:@"Associate" sender:sender];     }在调用preareForSegue时,需要发送方参数来标识按钮。preareForSegue是一个框架方法,在这个方法中,您将实例化场景,并传递它执行其工作所需的任何值。下面是我的方法:- (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子类并以编程方式运行它。好像很管用。这个有什么问题吗?我很困惑,阅读了所有其他的答案,说这是做不到的。UIViewController *toViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"OtherViewControllerId"]; MyCustomSegue *segue = [[MyCustomSegue alloc] initWithIdentifier:@"" source:self destination:toViewController]; [self prepareForSegue:segue sender:sender];[segue perform];
打开App,查看更多内容
随时随地看视频慕课网APP