制作按钮UIAlertView执行Segue [英] Make button it UIAlertView perform Segue

查看:126
本文介绍了制作按钮UIAlertView执行Segue的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have created a UIAlertView for an action which gives me 2 options. I want the user to be able to click on a button and have it perform a Segue.

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

Here's the code I have so far:

- (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"]) {



    }
}


推荐答案

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

Yeah, it's not very obvious at first, you need to create a manual segue.

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

Select the ViewController that will do the pushing (I'm the one who pushes), and connect manual to the pushed view controller (The Pushed View controller).

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

Select the newly created segue, and give it a name (in my case is "segue.push.alert", long name for logging), and call the perform segue in the action of the alert, like:

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)

[无主自我] 如果视图应小心处理控制器可以在动作发生时解除分配,你最好用 [弱自我] 然后做 self?.performSegue ... 如果可以解除分配。

[unowned self] should be handled with care, if the view controller can deallocate while the action is happening, you're better off with [weak self] and then doing self?.performSegue... if deallocation can occur.

现在,从视图控制器中你可以只需调用 performSegueWithIdentifier:sender:,在您的情况下

Now, from a view controller you can simply call performSegueWithIdentifier:sender:, in your case

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

以这种方式拥有segues的优势(相反直接编码),是你仍然可以对你的应用程序流有一个很好的概述,混合编码segues和故事板加载的segues有点失败的目的。

The advantage of having the segues in this way (instead of being directly coded), is that you can still have that nice overview of your application flow, having intermixed coded segues and storyboard-loaded segues kinda defeats the purpose.

这篇关于制作按钮UIAlertView执行Segue的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆