Objective C 获取 pickerview 值

发布于 2024-11-14 05:53:56 字数 2737 浏览 5 评论 0原文

另一个给你的 Objective-C 的,可能非常明显,但我已经研究了几天了,在解决了这里和其他地方的类似问题后,无法让它工作!

我几乎有一个 5 段选择器,当单击按钮时,会显示一个警报表,一旦接受,就会抓取选择器轮的值。很简单啊,但我无法让它工作,我不确定我哪里出错了...下面的一些代码...它可能很糟糕...但这对我来说都是新的,所以任何帮助都是赞赏。顺便说一下,这一切都是在不使用 InterfaceBuilder 的情况下完成的。

操作表之后循环之前的一切都工作正常......我只是无法获取值!

(ps,我希望格式有效!我已经更改了一些命名,但一切都应该是正确的)

谢谢!

我的文件.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

我的文件.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];
        }
    }
}

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!

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.

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)

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];
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

神也荒唐 2024-11-21 05:53:56

另一种答案:thePickerView 为零。我没有看到你给它赋值,对吧?

An alternative answer: thePickerView is nil. I don't see you assigning a value to it, right?

木森分化 2024-11-21 05:53:56

在这一行中:

NSInteger *selectedValue = [thePickerView selectedRowInComponent:i];

您应该知道 NSInteger 只是 int 的名称(实际上,底层类型取决于操作系统,但它始终是具体类型,而不是Objective C 对象)。或者换句话说,它不是指针。尝试:

NSInteger selectedValue = [thePickerView selectedRowInComponent:i];

我很惊讶你没有收到关于此的编译器警告。

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:

NSInteger selectedValue = [thePickerView selectedRowInComponent:i];

I'm surprised you didn't get a compiler warning about this.

若言繁花未落 2024-11-21 05:53:56

NSInteger 不是一个对象。这是普通的旧数据。现在查一下它的定义。继续,按住命令双击源文件中的符号。或者按住 Option 键双击以在参考手册中查找。

这应该可以做到。

NSInteger selectedValue = [thePickerView selectedRowInComponent:i];
NSLog(@"Wheel : %i Value : %i", i, selectedValue);
// [selectedValue release];

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.

And this should do it.

NSInteger selectedValue = [thePickerView selectedRowInComponent:i];
NSLog(@"Wheel : %i Value : %i", i, selectedValue);
// [selectedValue release];
你曾走过我的故事 2024-11-21 05:53:56

正如其他人指出的那样, NSInteger 不是一个对象。

NSInteger *selectedValue = [thePickerView selectedRowInComponent:i];

此行中发生的情况是 selectedRowInComponent: 返回一个数字,指示所选行的索引。因为您将其分配给 NSInteger 指针,所以计算机正在使用该值作为内存地址。 (当您声明 Whatever *foo 时,这意味着 foo 包含一个内存地址,而 *foo 是该地址处的值。)

如果您尝试取消引用(获取所指向的值)selectedValue,您的应用程序可能会崩溃。或者更糟糕的是,不崩溃,并返回该任意地址中的任何垃圾。

更重要的是,您将方法返回的内容(索引)与您想要的内容(值)混淆了。

这就是您所需要的:

NSInteger selectedRow = [thePickerView selectedRowInComponent:i];
NSString *selectedValue = [thePickerValues objectAtIndex:selectedRow];

现在您已经在 selectedValue 中获得了所需的内容,并且可以将其放入操作表中。

As others have pointed out, NSInteger is not an object.

NSInteger *selectedValue = [thePickerView selectedRowInComponent:i];

What is happening in this line is that selectedRowInComponent: is returning a number indicating the index of the selected row. Because you are assigning it to an NSInteger pointer instead, 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.)

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.

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.

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