Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UIAlertView按钮操作?_Ios_Objective C_Delegates_Uialertview - Fatal编程技术网

Ios UIAlertView按钮操作?

Ios UIAlertView按钮操作?,ios,objective-c,delegates,uialertview,Ios,Objective C,Delegates,Uialertview,我有一个UIAlertView,它显示了一段代码,要求您对appstore中的应用程序进行评分 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Rate on the Appstore!" message:@"" delegate:sel

我有一个
UIAlertView
,它显示了一段代码,要求您对appstore中的应用程序进行评分

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

但是我不知道如何向OK按钮添加操作,该按钮将引导您进入AppStore中的应用程序

- (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"]];
这个怎么样

- (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"]];
    } 
}

在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)

对于双按钮警报,与其检查按钮索引是否为
0
,不如检查是否按下了取消按钮
if(buttonIndex!=[alertView cancelButtonIndex])
。我只是添加了额外的帮助,以便在以后按下任何其他按钮时进行检查,然后他可以很容易地看到并继续。请将方法名称更改为ClickedButtonIndex。。。。可能有人会复制上面提到的相同方法名称,并且该方法永远不会被调用…请将方法名称更改为ClickedButtonNatindex。。。。有可能人们会复制上面提到的相同方法名称,并且该方法永远不会被调用…只是一点,在ViewController.h中,在@interface ViewController:UIViewController之后添加:“@interface ViewController:UIViewController”那么我在哪里调用该方法?由于我们在头文件中添加了UIViewController,它是否将自己调用,或者我们需要在某个地方执行类似于[self-alertView…]的操作?