UIPicker 作为子视图

发布于 2024-09-09 05:41:17 字数 1279 浏览 6 评论 0原文

我正在尝试添加 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我很OK 2024-09-16 05:41:17

目前尚不清楚如果您已经在 IB 中添加了选择器,为什么还要在代码中添加它...关于您发布的代码,请尝试将选择器添加到控制器的视图,而不是超级视图(并且不要忘记设置其delegate):

-(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];  
}

编辑: 您使用可能位于可见区域之外的框架初始化 myView 实例(框架的原点 y 坐标为 480),请尝试设置它至 0:

// Bad
UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0, 480, 320, 216)];
// Good?
UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 216)]; 

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):

-(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];  
}

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:

// Bad
UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0, 480, 320, 216)];
// Good?
UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 216)]; 
笛声青案梦长安 2024-09-16 05:41:17

查看来自 Apple

日期单元格选择器

Check out this example from Apple

Date Cell Picker

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文