How do I present a modal view upon an option in UI

2019-08-19 05:14发布

I have my main view controller, and tapping a button brings up an action sheet. I want one of the options tapped there to bring up an action sheet. I can't seem to figure it out, however, despite all my searching.

I have the following code:

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    NSString *buttonTitle = [actionSheet buttonTitleAtIndex:buttonIndex];

    if ([buttonTitle isEqualToString:@"Text"]) {
        AddTextViewController *addTextViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"addTextViewController"];        
    }

Now that I instantiated the view controller, I don't know what to do.

1条回答
劫难
2楼-- · 2019-08-19 05:40

Add this line right after you instantiate it.

if ([buttonTitle isEqualToString:@"Text"]) {
    AddTextViewController *addTextViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"addTextViewController"];        
    [self presentViewController:addTextViewController animated:YES completion:nil];
}

Note

You can also create a custom segue in the Storyboard that presents your view, then instead of instantiating and presenting you can just perform your custom segue. Both work the same.

Storyboard Method

First click your segue in Storyboards, then give it an identifier in the info panel.

Storyboard Segue Settings

Then you can replace your code with this and it should trigger the segue from your Storyboard.

if ([buttonTitle isEqualToString:@"Text"]) {
    [self performSegueWithIdentifier:@"infoSegue" sender:self];
}
查看更多
登录 后发表回答