ios - 使用 UIActionSheetDelegate 执行多个 Segue

标签 ios objective-c segue

我希望通过单个 UIBarButtonItem 按钮实现多个 Segue,并根据通过 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 将同一个按钮链接到多个 View 。我想知道是否有解决方法。

编辑

这就是我的代码现在的样子:(减去 Storyboard)

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

最佳答案

不要使用 segueperformSegueWithIdentifier

考虑将 StoryboardIDinstantiateViewControllerWithIdentifier 一起使用:

为此,在 Storyboard 中,只需创建一个 View Controller ,并且不要将任何 Segue 连接到它。在属性检查器的第三个选项卡中,为其分配一个 Storyboard ID:

Example

然后,在您的代码中,您可以创建一个如下所示的实例:

[self.storyboard instantiateViewControllerWithIdentifier:@"ImagePicker"]

这每次都会创建一个新实例,因此您仍然应该保存它并尽可能重复使用它。

编辑:获得 View Controller 后,您需要自己呈现它。

如果您使用 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];
    }
}

关于ios - 使用 UIActionSheetDelegate 执行多个 Segue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24128201/

相关文章:

ios - 在 Objective-C 中传回 NSManagedObject

ios - Storyboard segues 和接收内存警告

ios - 在最后一行向其添加数据之前,我的 segue 正在加载我的 UIViewController

ios - 如何在 tableView :heightForRowAtIndexPath: be implicitly recursive? 中初始化 NSAttributedString

ios - inputAccessoryViewController高度修改

objective-c - 在 Objective-C 中,如何对除子类之外的所有人隐藏方法(而不是属性)

ios - UINavigationBar barButtonItem 更改返回按钮图像和标题

ios - SKPaymentTransactionObserver 的 updatedTransactions 委托(delegate)方法中什么时候设置交易状态恢复?

ios - 如何在 objc 中以重复间隔每 15 天触发一次 UILocalNotification

ios - UIView animateWithDuration 完成已完成 = YES 尽管已被取消?