我希望通过单个 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"];
}
}
最佳答案
不要使用 segue
和 performSegueWithIdentifier
考虑将 StoryboardID
与 instantiateViewControllerWithIdentifier 一起使用:
为此,在 Storyboard 中,只需创建一个 View Controller ,并且不要将任何 Segue 连接到它。在属性检查器的第三个选项卡中,为其分配一个 Storyboard ID
:
然后,在您的代码中,您可以创建一个如下所示的实例:
[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/