我正在阅读有关如何使用UIPicker的教程,并找到了这段代码:
- (IBAction)buttonPressed {
NSInteger row = [userPicker selectedRowInComponent:0];
NSString *selected = [userPickerData objectAtIndex:row];
NSString *title = [[NSString alloc] initWithFormat:@"You selected %a", selected];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:@"Thank you for Choosing." delegate:nil cancelButtonTitle:@"You're Welcome" otherButtonTitles:nil];
[alert show];
[alert release];
[title release];
}
- (void)viewDidLoad {
// [super viewDidLoad];
NSArray *array = [[NSArray alloc] initWithObjects:@"New", @"Frank", @"Bob", @"Thor", nil];
self.userPickerData = array;
[array release];
}
UIPicker正确显示,选择器中的变量也能正确显示。当按下按钮时,警报消息确实出现,但它说“你选择了......”然后它是一堆乱码。为什么数组变量没有正确传递?
答案 0 :(得分:0)
您需要在initWithFormat字符串中使用%@,而不是%a
有关详细信息,请参阅此处:http://developer.apple.com/mac/library/documentation/cocoa/conceptual/strings/Articles/FormatStrings.html