2

所以我有一个弹出的选择器而不是键盘,但是当您滚动到选择器上的不同点时它不会改变值。我通过让警报显示选择了哪个年龄来测试这一点,并且它永远不会从第一个选项改变。我认为警报已损坏,但选择器中当前选定选项的蓝色选择器轮廓也没有显示。这是选择器和警报的代码:

- (void)viewDidLoad
{
    [super viewDidLoad];


    NSArray *array = [[NSArray alloc]        initWithObjects:@"18-29",@"30-39",@"40-49",@"50-59",@"60+",nil];
    self.pickerData = array;
}

-(IBAction)UITextFieldEditingDidBegin
{

    UIPickerView *picker = [[UIPickerView alloc] 
                            initWithFrame:CGRectMake(0, 244, 320, 270)];
    picker.delegate = self;
    picker.dataSource = self;
    [self.view addSubview:picker];
    [picker release];

}

-(IBAction)donePressed
{

NSInteger row = [agePicker selectedRowInComponent:0];
NSString *selected = [pickerData objectAtIndex:row];
NSString *title = [[NSString alloc] initWithFormat:
                   @"You selected %@!", selected];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
                                               message : @"Thank you for choosing."
                                               delegate:nil
                                     cancelButtonTitle :@"Okay!"             
                                     otherButtonTitles :nil];
[alert show];
[agePicker resignFirstResponder];
}

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

-(NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component
{
    return [pickerData count];
}

-(NSString *)pickerView:(UIPickerView *)pickerView
        titleForRow:(NSInteger)row
       forComponent:(NSInteger)component 
{
    return[pickerData objectAtIndex:row];
}
4

1 回答 1

2

编辑,以帮助关闭您的选择器,使其成为实例变量,在界面中声明您的选择器,

我的类.h

@interface MyClass:UIViewController <UIPickerViewDelegate, UIPickerViewDataSource>{
UIPickerView *picker
}

我的班级.m

@interface MyClass()
property (nonatomic, retain) *selectedVal; //make it a NSString
@end



-(IBAction)UITextFieldEditingDidBegin
{

    picker = [[UIPickerView alloc] 
                            initWithFrame:CGRectMake(0, 244, 320, 270)];
    picker.delegate = self;
    picker.dataSource = self;
    [self.view addSubview:picker];
    //[picker release]; //Place your picker release in the dealloc method

}

您必须为选择器实现委托,

- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
 self.selectedVal = [pickerData objectAtIndex:row]
NSLog(@"Selected option: %@. ", self.selectedVal );
}

然后在您的完成按钮上,您在属性中选择了 val 以供使用

-(IBAction)donePressed
{


NSString *title = [[NSString alloc] initWithFormat:
                   @"You selected %@!", self.selectedVal];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
                                               message : @"Thank you for choosing."
                                               delegate:nil
                                     cancelButtonTitle :@"Okay!"             
                                     otherButtonTitles :nil];
[alert show];
[alert release]; //I noticed you release the picker, so not using ARC, so release             alert!!
[picker resignFirstResponder];

 //after done, remove the picker from the view
 [picker removeFromSuperview];

}
于 2012-11-22T22:27:43.523 回答