制作按钮UIAlertView执行Segue

时间:2013-07-18 17:42:22

标签: ios uialertview uistoryboard segue uistoryboardsegue

我为动作创建了一个UIAlertView,它给了我两个选项。我希望用户能够点击按钮并让它执行Segue。

这是我到目前为止的代码:

- (IBAction)switchView:(id)sender
{
    UIAlertView *myAlert = [[UIAlertView alloc]
                            initWithTitle:@"Please Note"
                            message:@"Hello this is my message"
                            delegate:self
                            cancelButtonTitle:@"OK"
                            otherButtonTitles:@"Option 1", @"Option 2", nil];
    [myAlert show];
}

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

    if ([buttonTitle isEqualToString:@"Option 1"]) {



    }
}

3 个答案:

答案 0 :(得分:9)

是的,一开始并不是很明显,你需要创建一个手动的segue。

enter image description here

选择将执行推送的ViewController(我是推送的人),并将手动连接到推送的视图控制器(推送的视图控制器)。

enter image description here

iOS 8+与Swift

选择新创建的segue,并为其命名(在我的情况下为"segue.push.alert",记录的长名称),并在警报的操作中调用perform segue,如:

let alert = UIAlertController(title: "My Alert", message: "Be Alerted. This will trigger a segue.", preferredStyle: .Alert)
alert.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: nil))
alert.addAction(UIAlertAction(title: "Segue", style: .Default, handler:
{
    [unowned self] (action) -> Void in

    self.performSegueWithIdentifier("segue.push.alert", sender: self)
}))

presentViewController(alert)

[unowned self]应该小心处理,如果视图控制器可以在动作发生时解除分配,那么最好使用[weak self],然后在发生重新分配时执行self?.performSegue...

旧答案

现在,您可以在视图控制器中轻松调用performSegueWithIdentifier:sender:

// Using enums is entirely optional, it just keeps the code neat.
enum AlertButtonIndex : NSInteger
{
    AlertButtonAccept,
    AlertButtonCancel
};

// The callback method for an alertView
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)index
{
    if (index == AlertButtonAccept)
    {
        [self performSegueWithIdentifier:@"segue.push.alert" sender:self];
    }
}

以这种方式使用segue(而不是直接编码)的优点是,您仍然可以对应用程序流进行良好的概述,混合编码的segue和加载故事板的segue有点失败了。

答案 1 :(得分:1)

如果您在故事板中为segue指定了标识符,则可以执行以下操作:

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

    if ([buttonTitle isEqualToString:@"Option 1"]) {

        [self performSegueWithIdentifier:@"foo" sender:nil];

    }
}

答案 2 :(得分:1)

这是加载ViewController的另一种方法。您可以使用Storyboard标识符。阅读:What is a StoryBoard ID and how can i use this?

首先在Identity Inspector中设置Storyboard ID,然后将以下代码添加到警报委托。

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

    if ([buttonTitle isEqualToString:@"Option 1"]) {

        // This will create a new ViewController and present it. 
        NewViewController *newViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"NewViewControllerID"];

        [NewViewController setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
        [self presentViewController:NewViewController animated:YES completion:nil];

    }
}

希望这有帮助! :)