xcode 目标 C 获取选择器视图值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6253532/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Objective C getting pickerview values
提问by rich
another Objective-C one for you, probably pretty obvious but I have been at this for a few days now, and can't get it to work after trawling through similar problems on here and elsewhere!
另一个 Objective-C 给你,可能很明显,但我已经在这呆了几天了,在解决了这里和其他地方的类似问题后无法让它工作!
Pretty much I have a 5 segment picker which, when a button is clicked, an alert sheet is shown which once accepted grabs the values of the picker wheels. Easy huh, yet I cant get it to work and I'm not sure where I'm going wrong... Some code below... it's probably pretty bad... but it's all pretty new to me so any help would be appreciated. All this is done without using InterfaceBuilder by the way.
几乎我有一个 5 段选择器,当单击按钮时,会显示一个警报表,一旦接受它就会抓取选择器轮的值。简单吧,但我无法让它工作,我不确定我哪里出错了......下面的一些代码......它可能很糟糕......但这对我来说都很新,所以任何帮助都会赞赏。顺便说一下,所有这些都是在不使用 InterfaceBuilder 的情况下完成的。
Everything up to the loop after the action sheet works fine... I just can't get the values out!
动作表工作正常后循环的所有内容......我只是无法获取值!
(ps, i hope the formatting works! And I have changed some of my namings and that but all should be right)
(ps,我希望格式有效!我已经更改了一些命名,但一切都应该是正确的)
Thanks!
谢谢!
MyFile.h
我的文件
@interface MyViewController : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource, UIActionSheetDelegate>
{
UIPickerView * thePickerView;
NSArray *thePickerValues;
}
@property (nonatomic, retain) UIPickerView *thePickerView;
@property (nonatomic, retain) NSArray *thePickerValues;
- (void)buildPicker;
- (void)doAction;
@end
MyFile.m
我的文件
@implementation MyViewController
@synthesize thePickerView;
@synthesize thePickerValues;
- (void)viewDidLoad
{
NSArray *values = [[NSArray alloc] initWithObjects:@"0",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",nil];
thePickerValues = values;
[values release];
[self buildPicker];
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 5;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return [self.thePickerValues count];
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [self.thePickerValues objectAtIndex:row];
}
-(void)buildPicker
{
CGRect pickerFrame = CGRectMake(0.0f,130.0f,320.0,100.0f);
UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:pickerFrame];
pickerView.delegate = self;
pickerView.showsSelectionIndicator = YES;
[self.view addSubview:pickerView];
[pickerView release];
}
- (void)doAction
{
UIActionSheet *actionSheet = [[UIActionSheet alloc]
initWithTitle:[NSString stringWithFormat:@"Blah blah"]
delegate:self
cancelButtonTitle:@"No!"
destructiveButtonTitle:@"Yes!"
otherButtonTitles:nil];
[actionSheet showInView:self.parentViewController.tabBarController.view];
[actionSheet release];
}
-(void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if(!(buttonIndex == [actionSheet cancelButtonIndex]))
{
NSLog(@"Get the picker values");
for(int i = 0; i<5;i++)
{
NSInteger *selectedValue = [thePickerView selectedRowInComponent:i];
NSLog(@"Wheel : %i Value : %i", i, selectedValue);
[selectedValue release];
}
}
}
回答by Stephen Darlington
In this line:
在这一行:
NSInteger *selectedValue = [thePickerView selectedRowInComponent:i];
You should be aware that an NSInteger
is just a name for an int
(actually, the underlying type depends on the OS but it's always a concrete type and not an Objective C object). Or put another way, it's not a pointer. Try:
您应该知道 anNSInteger
只是an的名称int
(实际上,底层类型取决于操作系统,但它始终是具体类型而不是 Objective C 对象)。或者换句话说,它不是一个指针。尝试:
NSInteger selectedValue = [thePickerView selectedRowInComponent:i];
I'm surprised you didn't get a compiler warning about this.
我很惊讶你没有收到关于这个的编译器警告。
回答by Steven Kramer
An alternative answer: thePickerView is nil. I don't see you assigning a value to it, right?
另一个答案:thePickerView 为零。我没有看到你给它赋值,对吧?
回答by benzado
As others have pointed out, NSInteger is not an object.
正如其他人指出的那样, NSInteger 不是一个对象。
NSInteger *selectedValue = [thePickerView selectedRowInComponent:i];
What is happening in this line is that selectedRowInComponent:
is returning a number indicating the index ofthe selected row. Because you are assigning it to an NSInteger
pointerinstead, the computer is using the value as a memory address. (When you declare Whatever *foo
that means that foo
contains a memory address, and *foo
is the value at that address.)
这一行发生的事情selectedRowInComponent:
是返回一个数字,指示所选行的索引。因为您将它分配给一个NSInteger
指针,所以计算机将该值用作内存地址。(当你声明Whatever *foo
这意味着foo
包含一个内存地址,并且*foo
是该地址处的值。)
If you tried to dereference (get the value pointed to by) selectedValue
, your app will likely crash. Or worse, not crash, and return whatever junk is at that arbitrary address.
如果您尝试取消引用(获取指向的值)selectedValue
,您的应用可能会崩溃。或者更糟的是,不要崩溃,并返回该任意地址处的任何垃圾。
More importantly, you are mixing up what the method returns (the index) with what you want (the value).
更重要的是,您将方法返回的内容(索引)与您想要的内容(值)混在一起。
Here's what you need:
这是您需要的:
NSInteger selectedRow = [thePickerView selectedRowInComponent:i];
NSString *selectedValue = [thePickerValues objectAtIndex:selectedRow];
Now you've got what you want in selectedValue
, and can put it in your action sheet.
现在你已经得到了你想要的东西selectedValue
,并且可以把它放在你的行动表中。
回答by Steven Kramer
An NSInteger is not an object. It's plain old data. Look up its definition now. Go on, command-double click the symbol in your source file. Or option-double click to look it up in the reference manual.
NSInteger 不是对象。这是普通的旧数据。现在查一下它的定义。继续,命令并双击源文件中的符号。或选项-双击以在参考手册中查找。
And this should do it.
这应该可以做到。
NSInteger selectedValue = [thePickerView selectedRowInComponent:i];
NSLog(@"Wheel : %i Value : %i", i, selectedValue);
// [selectedValue release];