ios通过编程创建的按钮从segue打开modal



我正在创建一个按钮,并将其添加到工具栏中,如下所示:

UIButton *sendButtzon = [UIButton buttonWithType:UIButtonTypeRoundedRect];
sendButtzon.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
[sendButtzon setTitle:@"More" forState:UIControlStateNormal];
sendButtzon.frame = CGRectMake(toolBar.bounds.size.width - 18.0f,
                              6.0f,
                              58.0f,
                              29.0f);
[toolBar addSubview:sendButtzon];

如何打开一个新的viewController(我有一个名为"MoreView"的segue)?

您实现以下操作方法:

-(void)buttonPressed:(UIButton*)sender
{
    [self performSegueWithIdentifier:@"MoreView" sender:sender];
}

并像这样将其链接到您的按钮(将这一行添加到您的问题中的代码中):

[sendButtzon addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];

这会导致点击按钮调用buttobPressed:方法,该方法依次执行您在情节提要中定义的分段。

为此,您必须在故事板中定义一个名为"MoreView"的片段。

否则你必须创建UIViewControler按钮点击如下。。

-(void)buttonPressed:(UIButton*)sender
{
   UIViewController *destinationController = [[UIViewController alloc] init];
   [self presentModalViewContrller:destinationController animated:YES];
}

或创建视图控制器窗体故事板。

 -(void)buttonPressed:(UIButton*)sender
{
   UIViewController *destinationController = [self.storyboard instantiateViewContrllerWithIdentifier:@"DestinationViewController "];
   [self presentModalViewContrller:destinationController animated:YES];
}

每个UIViewController故事板都有属性。

最新更新