2
#define kCustomAlert @"UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Alert Back" message:msg delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: nil];[alert show];"
  1. 如何在视图控制器类中调用此警报?
4

4 回答 4

7

在你的 pch 文件中声明这个宏:

 #define kCustomAlert() [[[UIAlertView alloc] initWithTitle:@"Alert Title" message:@"Alert Message" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil] show]

宏调用: kCustomAlert();

带参数的警报宏:

#define kCustomAlertWithParam(title,msg) [[[UIAlertView alloc] initWithTitle:title message:msg delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil] show]

kCustomAlertWithParam(@"This is Title",@"This is message");

带有参数和目标的警报宏(供使用:UIAlertView 委托方法)

Please set UIAlertViewDelegate for your Controller.

#define kCustomAlertWithParamAndTarget(title,msg,target) [[[UIAlertView alloc] initWithTitle:title message:msg delegate:target cancelButtonTitle:@"OK" otherButtonTitles:nil, nil] show]

kCustomAlertWithParamAndTarget(@"This is Title",@"This is message",self);
于 2013-10-07T10:22:02.627 回答
2

你需要做一个函数你不能像这样定义它。您的语法错误。这样做:

#define ShowAlert() [[[UIAlertView alloc] initWithTitle:@"Title" message:@"Message" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil] show]

并称它为:

ShowAlert();

您还可以传递参数:-

#define ShowAlert(myTitle, myMessage) [[[UIAlertView alloc] initWithTitle:myTitle message:myMessage delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil] show]

并称它为:

ShowAlert(@"YourTitle", @"YourMessage");

注意:我并不是说这很好用,只是告诉方法这样做。

于 2013-10-07T10:14:33.777 回答
0

我不知道如何实现这一点,或者是否有可能,但替代方法是在 AppDelegate.m 中使用以下方法并在 AppDelegate.h 文件中声明该方法,然后您可以通过创建 AppDelegate 实例来调用它在任何班级

-(void)showAlertWithTitle:(NSString *)title message:(NSString *)message
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:message delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
    [alert show];
    [alert release];
}
于 2013-10-07T10:12:27.693 回答
0

我知道这是一个老问题,但UIAlertView现在已弃用,提供的答案会产生警告。

以下是您可以通过以下方式实现此目的的方法UIAlertViewController

从视图控制器

#define ShowAlert(title, myMessage)  { UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:myMessage preferredStyle:UIAlertControllerStyleAlert]; [alertController addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil]]; [self presentViewController:alertController animated:YES completion:nil]; }

请注意,这个只能在视图控制器中使用。


从任何地方

如果您希望能够在任何地方显示警报,您将需要这个:

#define ShowAlertFromTopMostController(title, myMessage)  { UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:myMessage preferredStyle:UIAlertControllerStyleAlert]; [alertController addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil]]; [[Utils topMostController] presentViewController:alertController animated:YES completion:nil]; }

并且您需要将以下方法添加到Utils类(子类化NSObject):

+(UIViewController*) topMostController {
    UIViewController *topController = [UIApplication sharedApplication].keyWindow.rootViewController;
    while (topController.presentedViewController) {
        topController = topController.presentedViewController;
    }

    return topController;
}
于 2018-03-28T09:13:43.780 回答