【问题标题】:UIAlertView button action?UIAlertView 按钮操作?
【发布时间】:2012-04-21 01:36:49
【问题描述】:

我有一个UIAlertView 显示此代码,要求您对应用商店中的应用程序进行评分。

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Rate on the Appstore!" 
                                                message:@"" 
                                               delegate:self 
                                      cancelButtonTitle:@"Later" 
                                      otherButtonTitles:@"OK", nil];
[alert show];
[alert release];

但我不知道如何向“确定”按钮添加操作,以将您带到 AppStore 中的应用程序。

【问题讨论】:

    标签: ios objective-c delegates uialertview


    【解决方案1】:

    这个怎么样?

    - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
        if (buttonIndex != [alertView cancelButtonIndex]) {
            NSLog(@"Launching the store");
            //replace appname with any specific name you want
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"itms://itunes.com/apps/appname"]];
        } 
    }
    

    【讨论】:

    • 请将方法名称更改为 clickedButtonAtIndex ...。人们可能会复制上述相同的方法名称,并且永远不会调用该方法...
    • 只是一点,在 ViewController.h 中,在 @interface ViewController : UIViewController 之后添加: 所以它看起来像这样:“@interface ViewController : UIViewController ”
    • 那么我在哪里调用这个方法呢?由于我们在头文件中添加了 UIViewController ,它是否会被自己调用,或者我们需要在某处执行类似 [self alertView...] 的操作?
    【解决方案2】:

    你想要类似下面的东西:

    - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
        if (buttonIndex == 0) {
            NSLog(@"Clicked button index 0");
            // Add the action here
        } else {
            NSLog(@"Clicked button index other than 0");
            // Add another action here
        }
    }
    

    当您按下按钮时,NSLog 会出现在控制台中,并在您想要调试/测试任何内容时提供帮助。

    然后对于您想要的操作,您可以编写如下内容:

    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"url_to_app_store"]];
    

    【讨论】:

    • 对于一个 2 按钮警报,而不是检查按钮索引是否为 0,您需要检查是否按下了取消按钮 if (buttonIndex != [alertView cancelButtonIndex])。
    • 我只是添加了额外的帮助,以便将来检查是否要按下任何其他按钮,然后他可以轻松看到并继续。
    • 请将方法名称更改为 clickedButtonAtIndex ...。人们可能会复制上述相同的方法名称,并且永远不会调用该方法...
    【解决方案3】:

    在 swift 中:使用此代码块显示警报消息。

    let alert = UIAlertController(title: "Alert", message: "This is an alert message", preferredStyle: UIAlertControllerStyle.Alert)
    
    let action = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: {(action:UIAlertAction) in print("This is in alert block")
    })
    
    alert.addAction(action)
    self.presentViewController(alert, animated: true, completion: nil)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多