美文网首页
iOS UIPickerView的注意

iOS UIPickerView的注意

作者: 夏天爱西瓜汁 | 来源:发表于2017-11-28 11:20 被阅读1625次

    2016.8.3

    在使用UIPickerView的时候,多个地方需要显示不同数据的picker,但是在不同的地方picker的行title会显示成第一个picker的数据,这时要在picker出现的方法里刷新一下picker的数据,即

    [self.durationPickerreloadComponent:0];

    durationPicker的应该是继承于UIPickerView的一个view

    [selfpickerView:self.durationPickerdidSelectRow:0inComponent:0];//实现默认选中第一行的点击方法,将第一行的title传给控制器

    将picker加在一个背景view上,点击view的时候picker可以消失

    UITapGestureRecognizer*tap = [[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(removeView:)];

    [_viewaddGestureRecognizer:tap];

    [[UIApplicationsharedApplication].keyWindowaddSubview:_view];

    [UIViewanimateWithDuration:0.3animations:^{

    self.frame=CGRectMake(0,_view.frame.size.height-self.frame.size.height,ScreenFullWidth,self.frame.size.height);

    }];

    - (void)removeView:(UITapGestureRecognizer*)tap {

    CGPointpoint = [taplocationInView:_view];

    //是否包含某个rect里的点

    if(CGRectContainsPoint(self.frame, point)) {

    }else{

    [selfremovePicker];

    }

    }

    - (void)removePicker {

    [UIViewanimateWithDuration:0.5animations:^{

    self.frame=CGRectMake(0,self.frame.origin.y+self.frame.size.height,ScreenFullWidth,self.frame.size.height);

    }completion:^(BOOLfinished) {

    [_viewremoveFromSuperview];

    [self.rowsArrremoveAllObjects];

    }];

    }

    如果有两列,不要设置默认选中的行,如

    [selfpickerView:self.pickerdidSelectRow:0inComponent:0];

    如果加上这句话,再次显示picker的时候第二列显示的数据会是第一列第一行对应的那个数组,不会显示上次选中的行

    [selfpickerView:self.durationPickerdidSelectRow:0inComponent:0];//实现默认选中第一行的点击方法,将第一行的title传给控制器

    相关文章

      网友评论

          本文标题:iOS UIPickerView的注意

          本文链接:https://www.haomeiwen.com/subject/hqbdbxtx.html