2013-10-07 27 views
2
#define kCustomAlert @"UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Alert Back" message:msg delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: nil];[alert show];" 
  1. 如何調用視圖控制器類此警報?
+1

這感覺不對。如果你想要一個自定義的UIAlertView方法,那麼我會在UIAlertView上創建一個類別,它有一個方法可以爲你做到這一點。 – Fogmeister

+0

這是不正確的方式,你如何設置警報代表,並且這種方法不會幫助你反正 –

+0

@ LithuT.V - 'delegate'方法將被調用:)如果你設置它。如果你願意,我可以解釋你。 – TheTiger

回答

0

我不知道如何實現這一目標還是可能的,即使或沒有,但這樣做的另一種方法是在你的AppDelegate.m使用下面的方法,並宣佈在AppDelegate.h文件的方法,然後你可以在任何類

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

你需要做一個功能無法定義它像這樣創建的AppDelegate實例調用它。您的語法錯誤。 做,在這種方式:

#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"); 

注意:我並不是說這是很好的使用,只是告訴方式這樣做。

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); 
+0

你在做什麼@PJR,只是在這裏複製我的答案? – TheTiger

+0

嘿@TheTiger,我對你的答案一無所知,我在閱讀一個問題後試圖在mac中看到宏,然後回答它。 – PJR

+0

太棒了!你已經在10分鐘前回答了,我的回答比你大7分鐘......你沒看見嗎?和dic2 ..反正.... NP – TheTiger