#define kCustomAlert @"UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Alert Back" message:msg delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: nil];[alert show];"
- 如何在视图控制器类中调用此警报?
#define kCustomAlert @"UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Alert Back" message:msg delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: nil];[alert show];"
在你的 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);
你需要做一个宏函数你不能像这样定义它。您的语法错误。这样做:
#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");
注意:我并不是说这很好用,只是告诉方法这样做。
我不知道如何实现这一点,或者是否有可能,但替代方法是在 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];
}
我知道这是一个老问题,但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;
}