I was reading a tutorial on how to do a UIPicker and found this code:
- (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];
}
The UIPicker displays correctly and the variables in the picker also display correctly. When the button is pressed then the alert message does come up but it says, "You selected.." and then it's a bunch of gibberish. Why aren't the array variables being passed correctly?
You need to use %@ in your initWithFormat string, rather than %a
See here for more info: http://developer.apple.com/mac/library/documentation/cocoa/conceptual/strings/Articles/FormatStrings.html
精彩评论