UIPicker 作为子视图
我正在尝试添加 UIPicker 作为子视图,但不太明白。我已将带有 UIPickerview 的第二个 UIView 控件添加到我的笔尖 (NewWorkoutViewController.xib)。我的代码如下:
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView
{
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component
{
return [list count];
}
-(NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [list objectAtIndex:row];
}
-(void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
NSLog(@"Selected item: %@ index of selected item: %i", [list objectAtIndex:row], row);
}
-(void)viewDidLoad
{
[super viewDidLoad];
counterInt = 0;
list = [[NSMutableArray alloc] init];
[list addObject:@"red"];
[list addObject:@"blue"];
[list addObject:@"yellow"];
[list addObject:@"pink"];
}
-(IBAction)routePicker
{
UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0, 480, 320, 216)];
UIPickerView *thePickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0,0,320,216)];
[myView addSubview:thePickerView];
[self.view.superview addSubview:myView];
}
我的子视图没有按预期弹出。
I'm trying to add a UIPicker as a subview, but can't quite figure it out. I've added a second UIView control with a UIPickerview to my nib (NewWorkoutViewController.xib). My code is as follows:
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView
{
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component
{
return [list count];
}
-(NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [list objectAtIndex:row];
}
-(void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
NSLog(@"Selected item: %@ index of selected item: %i", [list objectAtIndex:row], row);
}
-(void)viewDidLoad
{
[super viewDidLoad];
counterInt = 0;
list = [[NSMutableArray alloc] init];
[list addObject:@"red"];
[list addObject:@"blue"];
[list addObject:@"yellow"];
[list addObject:@"pink"];
}
-(IBAction)routePicker
{
UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0, 480, 320, 216)];
UIPickerView *thePickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0,0,320,216)];
[myView addSubview:thePickerView];
[self.view.superview addSubview:myView];
}
My subview is not popping up as expected.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
目前尚不清楚如果您已经在 IB 中添加了选择器,为什么还要在代码中添加它...关于您发布的代码,请尝试将选择器添加到控制器的视图,而不是超级视图(并且不要忘记设置其delegate):
编辑: 您使用可能位于可见区域之外的框架初始化
myView
实例(框架的原点 y 坐标为 480),请尝试设置它至 0:It is not clear why do you add the picker in code if you've already added it in IB... Regarding the code you posted, try to add picker to controller's view, not to the superview (and do not forget to set its delegate):
Edit: You initialize you
myView
instance with a frame that is likely to be outside of the visible area (frame's origin y coordinate is 480), try to set it to 0:查看来自 Apple
日期单元格选择器
Check out this example from Apple
Date Cell Picker