UIAlertController 如何在 obj c 中添加标签值

UIAlertController how add the tag value in obj c

将其用于 UIAlertView

- (void) alertStatus:(NSString *)msg :(NSString *)title :(int) tag
{
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title
                                                        message:msg
                                                       delegate:self
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil, nil];
    alertView.tag = tag;
    [alertView show];
}

但现在 UIAlertView 已被弃用。更改我的代码

 UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:msg preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction* MyAlert = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
    [alertController addAction:MyAlert];
   [self presentViewController:alertController animated:YES completion:nil];

这里如何传递这个标签值

alertView.tag = tag;

帮助如何在 UIAlertController 中传递标签值。提前致谢。

UIAlertControllerUIViewController ,所以我们需要为视图分配标签,所以使用 alertController.view.tag.

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"sds" message:@"sdf" preferredStyle:UIAlertControllerStyleAlert];
alertController.view.tag = tag;
UIAlertAction* MyAlert = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
[alertController addAction:MyAlert];
[self presentViewController:alertController animated:YES completion:nil];

更新

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"sds" message:@"sdf" preferredStyle:UIAlertControllerStyleAlert];
alertController.view.tag = 3;
UIAlertAction* MyAlert = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action)
                          {
                              // OK button tappped.
                              [self dismissViewControllerAnimated:YES completion:^{

                              }];

                          }];
[alertController addAction:MyAlert];
[self presentViewController:alertController animated:YES completion:nil];

创建 UIAlertController *alertController; 的 属性,然后在任何需要的地方使用此 alertController。像这样设置标签

alertController.view.tag = <YOUR TAG VALUE>;

获取那个 alertController 的标签,当你点击 alertController

上的 YES
//OK button tapped.
[self dismissViewControllerAnimated:YES completion:^{
    NSInteger *tag = alertController.view.tag;
}];

UIAlertController 中没有任何标签属性。您可以使用块来获取按钮操作。

UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Alert"
                                                               message:[NSString stringWithFormat:@"Your message"]
                                                        preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            //NSLog(@"OK");
        }]];
[self presentViewController:alert animated:YES completion:nil];

但是你可以这样使用tag-

alert.view.tag = 1;