2012-03-24 26 views
1

我不明白爲什麼這不起作用,我試過100種方法。 AlertView顯示一條空白消息。這裏是我的代碼:使用NSString作爲UIAlertView消息:

eventChoiceNow = [[UIAlertView alloc] initWithTitle:@"Hurry Up!" message:timeTillRest delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; 

timeTillRest是一個NSString。並在調用alertview之前NSLog(@"%@",timeTillRest);顯示字符串沒有麻煩。

+0

您是否100%確定'timeTillRest'是一個'NSString'?只是想知道爲什麼這不起作用,因爲這是'UIAlertView'最簡單的用例。僅供參考,'%@'也可以與除NSString之外的Foundation類一起使用。怎麼樣'NSLog(@「%@」,NSStringFromClass([timeTillRest class]));'? – ayoy 2012-03-24 15:22:38

+0

我確信'@property(nonatomic,strong)NSString * timeTillRest;'在我的.h中,它也是在我的.m中合成的。 – 2012-03-24 15:29:17

+0

你在哪裏定義你的「UIAlertView * eventChoiceNow」?嘗試在這個代碼中定義自己寫的地方。 – 2012-03-24 15:42:22

回答

2

爲什麼要使用alertview作爲實例變量?這是沒有必要的。它就像這樣簡單:

UIAlertView *eventChoiceNow = [[UIAlertView alloc] initWithTitle:@"Hurry Up!" message:timeTillRest delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
[eventChoiceNow show]; 
[eventChoiceNow release]; 
+0

謝謝,就是這樣。你說得對,將UIAlertView作爲一個實例變量聲明是沒有意義的。 – 2012-03-25 07:32:42

2

這應該工作得很好。使用此代碼測試:

NSString *timeTillRest = @"Testing"; 

    UIAlertView *eventChoiceNow = [[UIAlertView alloc] initWithTitle:@"Hurry Up!" message:timeTillRest delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
    [eventChoiceNow show]; 

它工作正常。另外測試使用:

#import "ViewController.h" 

@interface ViewController() 
@property(nonatomic,strong) NSString *timeTillRest; 
@end 

@implementation ViewController 
@synthesize timeTillRest; 
- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    timeTillRest = @"Testing"; 

    UIAlertView *eventChoiceNow = [[UIAlertView alloc] initWithTitle:@"Hurry Up!" message:timeTillRest delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
    [eventChoiceNow show]; 
    // Do any additional setup after loading the view, typically from a nib. 
} 

而且,這工作也完美無瑕。確保你沒有將該屬性設置爲零。

+0

這個答案也是正確的。 – 2012-03-25 07:34:50