iOS UIPickerView的注意

2017-11-28  本文已影响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传给控制器

上一篇下一篇

猜你喜欢

热点阅读