How do I present a modal view upon an option in UIActionSheet being tapped?

不羁岁月 提交于 2019-12-11 22:58:56

问题


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:


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.

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];
}


来源:https://stackoverflow.com/questions/15478786/how-do-i-present-a-modal-view-upon-an-option-in-uiactionsheet-being-tapped

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!