UIAlertView添加NSString

时间:2010-07-15 09:51:28

标签: objective-c nsstring uialertview

我想在我的UIAlertView消息中添加字符串


-(void) errorState:(NSException *) exp
{
    NSLog(@"Error State Blood Level3 %@",exp);
    NSString * myString = (NSString*)exp;
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:myString delegate:self cancelButtonTitle:@"Go to the main menu" otherButtonTitles:nil];
    [alert show];
    [alert release];

}

但它会抛出


2010-07-15 12:48:34.721 kndFrameV02[7955:207] -[NSException isEqualToString:]: unrecognized selector sent to instance 0x707b2e0
2010-07-15 12:48:34.724 kndFrameV02[7955:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSException isEqualToString:]: unrecognized selector sent to instance 0x707b2e0'

那么如何将字符串变量添加到UIAlertView的消息中呢?

1 个答案:

答案 0 :(得分:7)

当您将NSException投射为NSString时,NSException不会突然变成reasonNSString *reason = exp.reason;有一个名为{{1}}的属性,这是您需要的属性。像这样使用它:{{1}}