使用UIActionSheet委托执行多个分段

时间:2014-06-09 20:13:16

标签: ios objective-c segue

我希望通过一个UIBarButtonItem按钮获得多个细分,并且根据UIActionSheetDelegate的响应,正确的UIViewController会加载push Segue公司。这是我目前的代码UIActionSheetDelegate

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0)
    {
        // rate this app
    }

    else if (buttonIndex == 1)
    {
        [self performSegueWithIdentifier:@"bugReportSegue" sender:self];
    }

    else if (buttonIndex == 2)
    {
        [self performSegueWithIdentifier:@"featureRequestSegue" sender:self];
    }
}

这个问题是我无法通过Storyboard segues将同一个按钮链接到多个视图。我想知道是否有解决方法。

修改

这就是我现在的代码:(减去故事板)

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0)
    {
        // rate this app
    }

    else if (buttonIndex == 1)
    {
        [self.storyboard instantiateViewControllerWithIdentifier:@"bugReportIdentifier"];
    }

    else if (buttonIndex == 2)
    {
        [self.storyboard instantiateViewControllerWithIdentifier:@"featureRequestIdentifier"];
    }
}

1 个答案:

答案 0 :(得分:3)

而不是segue使用performSegueWithIdentifier

考虑使用StoryboardIDinstantiateViewControllerWithIdentifier:

为此,在Storyboard中,只需创建一个视图控制器,不要将任何segue连接到它。在属性检查器的第三个选项卡中,为其指定Storyboard ID

Example

然后,在您的代码中,您可以创建一个这样的实例:

[self.storyboard instantiateViewControllerWithIdentifier:@"ImagePicker"]

每次都会创建一个新实例,所以你应该保存它并尽可能重复使用它。

编辑:获得视图控制器后,您需要自己呈现。

如果您正在使用NavigationViewController电话:

UIViewController * newController = [self.storyboard instantiateViewControllerWithIdentifier:@"ImagePicker"];
[self.navigationController pushViewController:newController];

如果没有,你可以使用:

UIViewController * newController = [self.storyboard instantiateViewControllerWithIdentifier:@"ImagePicker"];
[self presentViewController:newController animated:YES completion:nil];

编辑2 : 以下是您的最终代码:

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0)
    {
        // rate this app
    }

    else if (buttonIndex == 1)
    {
        UIViewController * controller = [self.storyboard instantiateViewControllerWithIdentifier:@"bugReportIdentifier"];
        [self presentViewController:controller animated:YES completion:nil];
    }

    else if (buttonIndex == 2)
    {
        UIViewController * controller = [self.storyboard instantiateViewControllerWithIdentifier:@"bugReportIdentifier"];
        [self presentViewController:controller animated:YES completion:nil];
    }
}