#define kCustomAlert @"UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Alert Back" message:msg delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: nil];[alert show];"
答案 0 :(得分: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);
答案 1 :(得分: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");
注意:我并不是说只使用这样做就好了。
答案 2 :(得分: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];
}
答案 3 :(得分: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]; }
请注意,这个只能在视图控制器中使用。
<小时/> FROM ANYWHERE
如果您希望能够在任何地方显示提醒,那么您需要这个提醒:
#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;
}