为了账号安全,请及时绑定邮箱和手机立即绑定

以编程方式创建segue

以编程方式创建segue

暮色呼如 2019-07-08 15:39:33
以编程方式创建segue我有个共同点UIViewController我所有的UIViewsControllers扩展到重用一些常见操作。我想在这个“公共”上建立一个UIViewController所以其他人UIViewControllers继承。我正在努力弄清楚如何通过编程来实现这个目标。我想问题也可能是如何设置segue为了我UIViewControllers而不是走进故事板用手做的。
查看完整描述

3 回答

?
慕工程0101907

TA贡献1887条经验 获得超5个赞

我想再增加一种可能性。您可以做的一件事是,您可以使用未附加到操作的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;
    }}

好吧,测试一下就行了。希望它能帮到你。


查看完整回答
反对 回复 2019-07-08
?
斯蒂芬大帝

TA贡献1827条经验 获得超8个赞

我一直在使用这段代码实例化我的自定义segue子类并以编程方式运行它。好像很管用。这个有什么问题吗?我很困惑,阅读了所有其他的答案,说这是做不到的。

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


查看完整回答
反对 回复 2019-07-08
  • 3 回答
  • 0 关注
  • 533 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信