tags:

views:

16

answers:

1

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?

A: 

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

alku83