开发者

iphone not displaying array variables correctly

开发者 https://www.devze.com 2022-12-30 04:45 出处:网络
I was reading a tutorial on how to do a UIPicker and found this code: - (IBAction)buttonPressed { 开发者_高级运维NSInteger row = [userPicker selectedRowInComponent:0];

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

0

精彩评论

暂无评论...
验证码 换一张
取 消