簡體   English   中英

ios從程序創建的按鈕segue打開模態?

[英]ios open modal from segue from a programmatically created button?

我正在創建一個按鈕,並將其添加到我的工具欄中,如下所示:

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”的名稱)?

您實現以下操作方法:

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

然后像這樣將其鏈接到您的按鈕(將此行添加到您問題中的代碼中):

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

這將導致在按鈕上的點擊以調用buttobPressed:方法,該方法反過來執行您在情節buttobPressed:定義的segue。

為此,您必須在故事板上定義名稱為“ MoreView”的序列。

否則,您必須像這樣在按鈕單擊上創建UIViewControler。

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

或創建View Controller表單故事板。

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

每個UIViewController故事板都有屬性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM