我正在尝试添加UIPicker作为子视图,但无法弄明白。我已经向我的nib(NewWorkoutViewController.xib)添加了第二个带有UIPickerview的UIView控件。我的代码如下:
-(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];
}
我的子视图没有按预期弹出。
答案 0 :(得分:2)
目前尚不清楚为什么你在代码中添加了选择器如果你已经在IB中添加了...关于你发布的代码,尝试将选择器添加到控制器的视图,而不是超级视图(并且不要忘记设置其代表):
-(IBAction)routePicker
{
UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0, 480, 320, 216)];
UIPickerView *thePickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0,0,320,216)];
thePickerView.delegate = self;
[myView addSubview:thePickerView];
[self.view addSubview:myView];
}
修改:使用可能位于可见区域之外的帧(框架的原点y坐标为480)初始化myView
实例,尝试将其设置为0:
// Bad
UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0, 480, 320, 216)];
// Good?
UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 216)];
答案 1 :(得分:0)
从Apple
查看此示例