在不同的操作表中使用多个选择器视图?

发布于 2024-12-23 04:28:38 字数 7845 浏览 2 评论 0原文

在我的应用程序中,我在操作表中使用 4 个选取器视图,其中我如何从特定选取器获取特定值? 其中我使用了一种方法来获取它。但是我需要一种更好的方法来获取它。请帮助我。,

-(NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component {
    NSLog(@"pickerView");
    return [pickerArray count];
}

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

- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {

    if (validateButton == 0){

        NSInteger temp = (row*5);
        minAsleepInt = temp;
        minAsleepLable.text = [NSString stringWithFormat:@"%d",temp];
    }
    else if(validateButton == 1){

        noOptInt = row+1;
        noOptLable.text = [pickerArray objectAtIndex:row];
    }
    else if(validateButton == 2){

        ageTypeLable.text = [pickerArray objectAtIndex:row];
    }
    else if(validateButton == 3){

        if([pickerArray objectAtIndex:row] == @"Yes"){
            sleepLessBool = YES;
        }else{
            sleepLessBool = NO;
        }
         sleepLessLable.text = [pickerArray objectAtIndex:row];
    }

}


- (IBAction)sleepLessAction:(id)sender {

    validateButton = 3;

    pickerArray = [[NSMutableArray alloc] init];
    [pickerArray addObject:@"Yes"];
    [pickerArray addObject:@"No"];


    actionSheet = [[UIActionSheet alloc] initWithTitle:nil 
                                                             delegate:self
                                                    cancelButtonTitle:nil
                                               destructiveButtonTitle:nil
                                                    otherButtonTitles:nil];

    [actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];

    CGRect pickerFrame = CGRectMake(0, 40, 0, 0);

    pickerView = [[UIPickerView alloc] initWithFrame:pickerFrame];
    pickerView.showsSelectionIndicator = YES;
    pickerView.delegate = self;

    [actionSheet addSubview:pickerView];

    UISegmentedControl *closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:@"Close"]];
    closeButton.momentary = YES; 
    closeButton.frame = CGRectMake(260, 7.0f, 50.0f, 30.0f);
    closeButton.segmentedControlStyle = UISegmentedControlStyleBar;
    closeButton.tintColor = [UIColor blackColor];
    [closeButton addTarget:self action:@selector(dismissActionSheet) forControlEvents:UIControlEventValueChanged];
    [actionSheet addSubview:closeButton];

    [actionSheet showInView:[[UIApplication sharedApplication] keyWindow]];

    [actionSheet setBounds:CGRectMake(0, 0, 320, 485)];


}

- (IBAction)ageTypeAction:(id)sender {
     validateButton = 2;

    pickerArray = [[NSMutableArray alloc] init];
    [pickerArray addObject:@"Child"];
    [pickerArray addObject:@"Teen"];
    [pickerArray addObject:@"Adult"];

    actionSheet = [[UIActionSheet alloc] initWithTitle:nil 
                                              delegate:nil
                                     cancelButtonTitle:nil
                                destructiveButtonTitle:nil
                                     otherButtonTitles:nil];

    [actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];

    CGRect pickerFrame = CGRectMake(0, 40, 0, 0);

    pickerView = [[UIPickerView alloc] initWithFrame:pickerFrame];
    pickerView.showsSelectionIndicator = YES;
    pickerView.delegate = self;

    [actionSheet addSubview:pickerView];


    UISegmentedControl *closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:@"Close"]];
    closeButton.momentary = YES; 
    closeButton.frame = CGRectMake(260, 7.0f, 50.0f, 30.0f);
    closeButton.segmentedControlStyle = UISegmentedControlStyleBar;
    closeButton.tintColor = [UIColor blackColor];
    [closeButton addTarget:self action:@selector(dismissActionSheet) forControlEvents:UIControlEventValueChanged];
    [actionSheet addSubview:closeButton];

    [actionSheet showInView:[[UIApplication sharedApplication] keyWindow]];

    [actionSheet setBounds:CGRectMake(0, 0, 320, 485)];
}

- (IBAction)noOptAction:(id)sender {

     validateButton = 1;
    pickerArray = [[NSMutableArray alloc] init];

    [pickerArray addObject:@"1"];
    [pickerArray addObject:@"2"];
    [pickerArray addObject:@"3"];
    [pickerArray addObject:@"4"];
    [pickerArray addObject:@"5"];
    [pickerArray addObject:@"6"];
    [pickerArray addObject:@"7"];
    [pickerArray addObject:@"8"];    
    [pickerArray addObject:@"9"];

    actionSheet = [[UIActionSheet alloc] initWithTitle:nil 
                                              delegate:nil
                                     cancelButtonTitle:nil
                                destructiveButtonTitle:nil
                                     otherButtonTitles:nil];

    [actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];

    CGRect pickerFrame = CGRectMake(0, 40, 0, 0);

    pickerView = [[UIPickerView alloc] initWithFrame:pickerFrame];
    pickerView.showsSelectionIndicator = YES;
    pickerView.delegate = self;


    [actionSheet addSubview:pickerView];


    UISegmentedControl *closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:@"Close"]];
    closeButton.momentary = YES; 
    closeButton.frame = CGRectMake(260, 7.0f, 50.0f, 30.0f);
    closeButton.segmentedControlStyle = UISegmentedControlStyleBar;
    closeButton.tintColor = [UIColor blackColor];
    [closeButton addTarget:self action:@selector(dismissActionSheet) forControlEvents:UIControlEventValueChanged];
    [actionSheet addSubview:closeButton];

    [actionSheet showInView:[[UIApplication sharedApplication] keyWindow]];

    [actionSheet setBounds:CGRectMake(0, 0, 320, 485)];

}

- (IBAction)minSleepAction:(id)sender {

         validateButton = 0;

    pickerArray = [[NSMutableArray alloc] init];
    [pickerArray addObject:@"0 Minutes"];
    [pickerArray addObject:@"5 Minutes"];
    [pickerArray addObject:@"10 Minutes"];
    [pickerArray addObject:@"15 Minutes"];
    [pickerArray addObject:@"20 Minutes"];
    [pickerArray addObject:@"25 Minutes"];
    [pickerArray addObject:@"30 Minutes"];
    [pickerArray addObject:@"35 Minutes"];
    [pickerArray addObject:@"40 Minutes"];
    [pickerArray addObject:@"45 Minutes"];
    [pickerArray addObject:@"50 Minutes"];
    [pickerArray addObject:@"55 Minutes"];
    [pickerArray addObject:@"60 Minutes"];

    actionSheet = [[UIActionSheet alloc] initWithTitle:nil 
                                              delegate:nil
                                     cancelButtonTitle:nil
                                destructiveButtonTitle:nil
                                     otherButtonTitles:nil];

    [actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];

    CGRect pickerFrame = CGRectMake(0, 40, 0, 0);

    pickerView = [[UIPickerView alloc] initWithFrame:pickerFrame];
    pickerView.showsSelectionIndicator = YES;
    pickerView.delegate = self;


    [actionSheet addSubview:pickerView];


    UISegmentedControl *closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:@"Close"]];
    closeButton.momentary = YES; 
    closeButton.frame = CGRectMake(260, 7.0f, 50.0f, 30.0f);
    closeButton.segmentedControlStyle = UISegmentedControlStyleBar;
    closeButton.tintColor = [UIColor blackColor];
    [closeButton addTarget:self action:@selector(dismissActionSheet) forControlEvents:UIControlEventValueChanged];
    [actionSheet addSubview:closeButton];

    [actionSheet showInView:[[UIApplication sharedApplication] keyWindow]];

    [actionSheet setBounds:CGRectMake(0, 0, 320, 485)];
}


-(void)dismissActionSheet{
    [actionSheet dismissWithClickedButtonIndex:0 animated:YES];

}

@end

In my app im using 4 pickerviews in action sheet in which how can i get the particular value from the particular picker ?
in which i have used a method to get it., but i need a better way to get it., plz help me .,

-(NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component {
    NSLog(@"pickerView");
    return [pickerArray count];
}

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

- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {

    if (validateButton == 0){

        NSInteger temp = (row*5);
        minAsleepInt = temp;
        minAsleepLable.text = [NSString stringWithFormat:@"%d",temp];
    }
    else if(validateButton == 1){

        noOptInt = row+1;
        noOptLable.text = [pickerArray objectAtIndex:row];
    }
    else if(validateButton == 2){

        ageTypeLable.text = [pickerArray objectAtIndex:row];
    }
    else if(validateButton == 3){

        if([pickerArray objectAtIndex:row] == @"Yes"){
            sleepLessBool = YES;
        }else{
            sleepLessBool = NO;
        }
         sleepLessLable.text = [pickerArray objectAtIndex:row];
    }

}


- (IBAction)sleepLessAction:(id)sender {

    validateButton = 3;

    pickerArray = [[NSMutableArray alloc] init];
    [pickerArray addObject:@"Yes"];
    [pickerArray addObject:@"No"];


    actionSheet = [[UIActionSheet alloc] initWithTitle:nil 
                                                             delegate:self
                                                    cancelButtonTitle:nil
                                               destructiveButtonTitle:nil
                                                    otherButtonTitles:nil];

    [actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];

    CGRect pickerFrame = CGRectMake(0, 40, 0, 0);

    pickerView = [[UIPickerView alloc] initWithFrame:pickerFrame];
    pickerView.showsSelectionIndicator = YES;
    pickerView.delegate = self;

    [actionSheet addSubview:pickerView];

    UISegmentedControl *closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:@"Close"]];
    closeButton.momentary = YES; 
    closeButton.frame = CGRectMake(260, 7.0f, 50.0f, 30.0f);
    closeButton.segmentedControlStyle = UISegmentedControlStyleBar;
    closeButton.tintColor = [UIColor blackColor];
    [closeButton addTarget:self action:@selector(dismissActionSheet) forControlEvents:UIControlEventValueChanged];
    [actionSheet addSubview:closeButton];

    [actionSheet showInView:[[UIApplication sharedApplication] keyWindow]];

    [actionSheet setBounds:CGRectMake(0, 0, 320, 485)];


}

- (IBAction)ageTypeAction:(id)sender {
     validateButton = 2;

    pickerArray = [[NSMutableArray alloc] init];
    [pickerArray addObject:@"Child"];
    [pickerArray addObject:@"Teen"];
    [pickerArray addObject:@"Adult"];

    actionSheet = [[UIActionSheet alloc] initWithTitle:nil 
                                              delegate:nil
                                     cancelButtonTitle:nil
                                destructiveButtonTitle:nil
                                     otherButtonTitles:nil];

    [actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];

    CGRect pickerFrame = CGRectMake(0, 40, 0, 0);

    pickerView = [[UIPickerView alloc] initWithFrame:pickerFrame];
    pickerView.showsSelectionIndicator = YES;
    pickerView.delegate = self;

    [actionSheet addSubview:pickerView];


    UISegmentedControl *closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:@"Close"]];
    closeButton.momentary = YES; 
    closeButton.frame = CGRectMake(260, 7.0f, 50.0f, 30.0f);
    closeButton.segmentedControlStyle = UISegmentedControlStyleBar;
    closeButton.tintColor = [UIColor blackColor];
    [closeButton addTarget:self action:@selector(dismissActionSheet) forControlEvents:UIControlEventValueChanged];
    [actionSheet addSubview:closeButton];

    [actionSheet showInView:[[UIApplication sharedApplication] keyWindow]];

    [actionSheet setBounds:CGRectMake(0, 0, 320, 485)];
}

- (IBAction)noOptAction:(id)sender {

     validateButton = 1;
    pickerArray = [[NSMutableArray alloc] init];

    [pickerArray addObject:@"1"];
    [pickerArray addObject:@"2"];
    [pickerArray addObject:@"3"];
    [pickerArray addObject:@"4"];
    [pickerArray addObject:@"5"];
    [pickerArray addObject:@"6"];
    [pickerArray addObject:@"7"];
    [pickerArray addObject:@"8"];    
    [pickerArray addObject:@"9"];

    actionSheet = [[UIActionSheet alloc] initWithTitle:nil 
                                              delegate:nil
                                     cancelButtonTitle:nil
                                destructiveButtonTitle:nil
                                     otherButtonTitles:nil];

    [actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];

    CGRect pickerFrame = CGRectMake(0, 40, 0, 0);

    pickerView = [[UIPickerView alloc] initWithFrame:pickerFrame];
    pickerView.showsSelectionIndicator = YES;
    pickerView.delegate = self;


    [actionSheet addSubview:pickerView];


    UISegmentedControl *closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:@"Close"]];
    closeButton.momentary = YES; 
    closeButton.frame = CGRectMake(260, 7.0f, 50.0f, 30.0f);
    closeButton.segmentedControlStyle = UISegmentedControlStyleBar;
    closeButton.tintColor = [UIColor blackColor];
    [closeButton addTarget:self action:@selector(dismissActionSheet) forControlEvents:UIControlEventValueChanged];
    [actionSheet addSubview:closeButton];

    [actionSheet showInView:[[UIApplication sharedApplication] keyWindow]];

    [actionSheet setBounds:CGRectMake(0, 0, 320, 485)];

}

- (IBAction)minSleepAction:(id)sender {

         validateButton = 0;

    pickerArray = [[NSMutableArray alloc] init];
    [pickerArray addObject:@"0 Minutes"];
    [pickerArray addObject:@"5 Minutes"];
    [pickerArray addObject:@"10 Minutes"];
    [pickerArray addObject:@"15 Minutes"];
    [pickerArray addObject:@"20 Minutes"];
    [pickerArray addObject:@"25 Minutes"];
    [pickerArray addObject:@"30 Minutes"];
    [pickerArray addObject:@"35 Minutes"];
    [pickerArray addObject:@"40 Minutes"];
    [pickerArray addObject:@"45 Minutes"];
    [pickerArray addObject:@"50 Minutes"];
    [pickerArray addObject:@"55 Minutes"];
    [pickerArray addObject:@"60 Minutes"];

    actionSheet = [[UIActionSheet alloc] initWithTitle:nil 
                                              delegate:nil
                                     cancelButtonTitle:nil
                                destructiveButtonTitle:nil
                                     otherButtonTitles:nil];

    [actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];

    CGRect pickerFrame = CGRectMake(0, 40, 0, 0);

    pickerView = [[UIPickerView alloc] initWithFrame:pickerFrame];
    pickerView.showsSelectionIndicator = YES;
    pickerView.delegate = self;


    [actionSheet addSubview:pickerView];


    UISegmentedControl *closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:@"Close"]];
    closeButton.momentary = YES; 
    closeButton.frame = CGRectMake(260, 7.0f, 50.0f, 30.0f);
    closeButton.segmentedControlStyle = UISegmentedControlStyleBar;
    closeButton.tintColor = [UIColor blackColor];
    [closeButton addTarget:self action:@selector(dismissActionSheet) forControlEvents:UIControlEventValueChanged];
    [actionSheet addSubview:closeButton];

    [actionSheet showInView:[[UIApplication sharedApplication] keyWindow]];

    [actionSheet setBounds:CGRectMake(0, 0, 320, 485)];
}


-(void)dismissActionSheet{
    [actionSheet dismissWithClickedButtonIndex:0 animated:YES];

}

@end

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

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

发布评论

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

评论(2

浪菊怪哟 2024-12-30 04:28:38

您可以为每个选择器设置一个标签。并使用标签值从特定选择器获取特定值。

you can set a Tag for each picker. And get the particular value from particular picker using the TAG value.

萝莉病 2024-12-30 04:28:38

正如您所做的那样,您需要设置以下委托方法:

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return PICKER_COMPONENTS;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    return [self.pickerOptions count];
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    return [self.pickerOptions objectAtIndex:row];
}

现在,就响应每个选择器的选择而言,我建议使用 EAActionSheetPicker。它具有以下可选的委托方法,使生活更轻松。

-(void)EAActionSheetPicker:(EAActionSheetPicker *)actionSheet
   didDismissWithSelection:(id)selection
               inTextField:(UITextField *)textField{
    // respond to your selection here
}

您只需通过将每个 pickerView 与关联的响应文本字段连接起来即可正确设置它。查看存储库中的示例项目以了解我在说什么。

As you've done, you need to setup the following delegate methods:

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return PICKER_COMPONENTS;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    return [self.pickerOptions count];
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    return [self.pickerOptions objectAtIndex:row];
}

Now, as far as responding to each picker's selection, I would recommend using EAActionSheetPicker. It has the following optional delegate method that makes life easier

-(void)EAActionSheetPicker:(EAActionSheetPicker *)actionSheet
   didDismissWithSelection:(id)selection
               inTextField:(UITextField *)textField{
    // respond to your selection here
}

You'll just have to set it up right by hooking up each pickerView with the associated response textField. Check out the example project at the repo to see what I'm talking about.

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