如何在未触摸选择器时从uipickerview中保存数组中的默认值

时间:2013-12-30 07:53:56

标签: ios uitableview uipickerview

我有一个视图,其中我添加了Labels和按钮,以便在Label中输入问题,并在点击与标签对应的button时出现一个tableview。在该表中,每个pickerviews中都有tableviewcellpickerview包含0到5之间的数字,用于评估文本字段中typaed的各个方面的操作。通过这种方式,每个中都有多个tableviewspickers。我希望选择的值在0到5之间保存在数组中。当未触摸选择器时,我希望相应的未触摸选择器中的默认值保存在数组中。

希望你了解我的需要。请帮我解决这个问题。

谢谢。

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

    cln_total+=row;
    self.cln_label_total.text=[NSString stringWithFormat:@"%d",cln_total];
    NSLog(@"cln_total is  %d",cln_total);
    QSCEAppDelegate* qsceObj=(QSCEAppDelegate*)[[UIApplication sharedApplication]delegate];
    NSLog(@"row is %d",row);
    qsceObj.temp=qsceObj.temp+row;
    NSLog(@"temp @ didselectrow is %d",qsceObj.temp);
    [qsceObj.selectednums_second addObject:[NSNumber numberWithInt:row]];
    NSLog(@"qsceObj.selectednums_second %@",qsceObj.selectednums_second);
    NSLog(@"qsceObj.app_city %@",qsceObj.app_city);
    value=row;
    NSLog(@"value in didselectrow in picker is %d",value);
    NSLog(@"qsceObj.picview_sec %@",qsceObj.picView_sec);

 }

这是我的didDisappear功能

   -(void)viewDidDisappear:(BOOL)animated
{ QSCEAppDelegate*myAppdelegate=[[UIApplication sharedApplication]delegate];

   for(i=0;i<=44;i++)
    {
    int r=[myAppdelegate.picView_sec selectedRowInComponent:0];
   NSLog(@"r %d is %d",i,r);
    if([myAppdelegate.picView_sec selectedRowInComponent:0]==-1)
    {
        [myAppdelegate.selectednums_second addObject:[NSNumber numberWithInt:0]];
    }
    else
    {
       [myAppdelegate.selectednums_second addObject:[NSNumber numberWithInt:[myAppdelegate.picView_sec selectedRowInComponent:0]]];
    }
     NSLog(@"myAppdelegate.selectednums_second is%@",myAppdelegate.selectednums_second);
  }
}

1 个答案:

答案 0 :(得分:0)

让我告诉你我为获取默认值做了什么

@property (strong, nonatomic) NSMutableArray* _jsonDictionary; // this is array to show the values 

在viewDidLoad中我使用字典

初始化数组
for (int row = 0; row < 3; row++) {
    NSArray *keys = @[@"UserId",@"PreferenceId",@"NumericValue",@"StringValue",@"BooleanValue"                     ];
    NSArray *objects = @[@(userId), @(row + 1), @(1),  @"", @0];
    NSLog(@"%@", objects);
    [_jsonDictionary insertObject:[NSDictionary dictionaryWithObjects:objects forKeys:keys] atIndex:row];
}

并在

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    NSArray *keys = @[@"UserId",@"PreferenceId",@"NumericValue",@"StringValue",@"BooleanValue"                     ];
    NSArray *objects = nil;
    switch (_pickerSelectedIndex.row) {
        case 0:
            objects = @[@([userInfo.userId intValue]), @(_pickerSelectedIndex.row + 1), @(row + 1), @"", @0];
            if([_jsonDictionary safeObjectAtIndex:0] != nil) {
                [_jsonDictionary removeObjectAtIndex:0];}
            [_jsonDictionary insertObject:[NSDictionary dictionaryWithObjects:objects forKeys:keys] atIndex:0];
            break;
        case 1:
            objects = @[@([userInfo.userId intValue]), @(_pickerSelectedIndex.row + 1), @(row + 1), @"", @0];

            [[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInt:row] forKey:@"GenderPrefrence"];
            if([_jsonDictionary safeObjectAtIndex:1] != nil) {
                [_jsonDictionary removeObjectAtIndex:1]; }
            [_jsonDictionary insertObject:[NSDictionary dictionaryWithObjects:objects forKeys:keys] atIndex:1];
            break;
        case 2:
            objects = @[@([userInfo.userId intValue]), @(_pickerSelectedIndex.row + 1), @(row + 1), @"", @0];
            if([_jsonDictionary safeObjectAtIndex:2] != nil) {
                [_jsonDictionary removeObjectAtIndex:2]; }
            [_jsonDictionary insertObject:[NSDictionary dictionaryWithObjects:objects forKeys:keys] atIndex:2];
            break;
    }
}

这对我有用希望这可以帮助你