在UIAlertView中按下按钮后发出警告

时间:2013-07-22 12:52:33

标签: ios uialertview segue

我正在开发一个应用,当我按下ViewController中的“确定”按钮时,我需要显示UIAlertView。我怎么能这样做?

我在这里找到了这个解决方案:iOS Dev: Segue by pressing a button in an Alert?,但它没有用。

xCode说:

the method: presentModalViewController is deprecated

我在网上发现我应该使用方法presentViewController

我试图这样做,但它显示我没有任何互动的黑屏,有什么不对? 我在这里粘贴我的代码,也许你可以帮助我。

-(void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex != alertView.cancelButtonIndex)
    {
        PromoViewController *pvc = [[PromoViewController alloc] init];
        [self presentViewController:pvc animated:YES completion:nil];
    }
}

谢谢

3 个答案:

答案 0 :(得分:2)

为了以编程方式使用segue,您需要调用performSegueWithIdentifier

首先确保你的segue在故事板中有一个标识符。

然后使用此代码......

-(void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex != alertView.cancelButtonIndex)
    {
        [self performSegueWithIdentifier:@"YourSegueIdentifier"];
    }
}

这将做你想要的。

答案 1 :(得分:0)

另一种方法是获取实例并以编程方式执行

UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard"
                                                         bundle: nil];
PromoViewController *controller = (PromoViewController*)[mainStoryboard 
                    instantiateViewControllerWithIdentifier: @"<Controller ID>"];
[self presentViewController:controller animated:YES completion:nil];

注意控制器ID是您通过属性检查器

为场景设置的故事板ID

答案 2 :(得分:0)

请参阅:Make button it UIAlertView perform Segue

它将向您展示如何创建手动segue并调用它。

相关问题