iPhone 无法正确显示数组变量
我正在阅读有关如何制作 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 显示正确,选择器中的变量也显示正确。当按下按钮时,确实会出现警报消息,但它会显示“您选择了..”,然后是一堆乱码。为什么数组变量没有正确传递?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要在 initWithFormat 字符串中使用 %@,而不是 %a
请参阅此处了解更多信息:http://developer.apple.com/mac/library/documentation/cocoa/conceptual/strings/Articles/FormatStrings.html
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