iPhone 无法正确显示数组变量

发布于 2024-08-31 12:19:29 字数 835 浏览 0 评论 0原文

我正在阅读有关如何制作 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你如我软肋 2024-09-07 12:19:33

您需要在 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文