xcode 如何设置UIPickerView?[已关闭]

rqmkfv5c  于 2023-01-06  发布在  其他
关注(0)|答案(3)|浏览(140)

很难判断此处所问的问题。此问题模棱两可、模糊不清、不完整、过于宽泛或过于修辞,无法以其当前形式合理地回答。若要获得澄清此问题以便重新打开的帮助,请单击visit the help center
10年前关闭了。
我正在设置一个UIPickerView,使其具有choice achoice bchoice c等选项。我尝试解释Apple的示例代码,但对于像我这样的初学者来说,这似乎很难理解。如果可能的话,我还希望从选择器视图中的选项将我带到另一个页面。

idv4meu8

idv4meu81#

对于每个初学者来说,第一次理解这些东西显然是有些乏味的。
无论如何,您知道如何使用UITableView吗?您知道如何使用UITableViewDelegateUITableViewDataSource吗?如果您的答案是肯定的,那么就想象UIPickerViewUITableView一样(但请记住它们不是UITableViewController)。
比如说,我有一个UIPickerView

UIPickerView *objPickerView = [UIPickerView new];  // You need to set frame or other properties and add to your view...you can either use XIB code...

1)首先,您需要通过IB或代码将delegatedataSource分配给UIPickerView,这取决于您的实现(因此,这一步看起来非常类似于UITableView,不是吗?)
就像这样:

objPickerView.delegate = self; // Also, can be done from IB, if you're using
objPickerView.dataSource = self;// Also, can be done from IB, if you're using

2)接下来,您需要定义截面的数量,如下所示:

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView {
     return 1;  // Or return whatever as you intend
}

2)然后,您需要定义所需的行数:

- (NSInteger)pickerView:(UIPickerView *)thePickerView 
numberOfRowsInComponent:(NSInteger)component {
     return 3;//Or, return as suitable for you...normally we use array for dynamic
}

3)然后,定义行标题(如果有多个部分,则为每个部分定义标题):

- (NSString *)pickerView:(UIPickerView *)thePickerView 
             titleForRow:(NSInteger)row forComponent:(NSInteger)component {
     return [NSString stringWithFormat:@"Choice-%d",row];//Or, your suitable title; like Choice-a, etc.
}

4)接下来,当有人点击某个元素时,您需要获取事件(当您想要导航到其他控制器/屏幕时):

- (void)pickerView:(UIPickerView *)thePickerView 
      didSelectRow:(NSInteger)row 
       inComponent:(NSInteger)component {

//Here, like the table view you can get the each section of each row if you've multiple sections
     NSLog(@"Selected Color: %@. Index of selected color: %i", 
     [arrayColors objectAtIndex:row], row);

     //Now, if you want to navigate then;
     // Say, OtherViewController is the controller, where you want to navigate:
     OtherViewController *objOtherViewController = [OtherViewController new];
     [self.navigationController pushViewController:objOtherViewController animated:YES];

}

这就是您所需要的全部实现。

n8ghc7c1

n8ghc7c12#

我将简要说明如何通过编程实现这一点

- (void)viewDidLoad {
    [super viewDidLoad];

    UIPickerView * picker = [UIPickerView new];
    picker.delegate = self;
    picker.dataSource = self;
    picker.showsSelectionIndicator = YES;
    [self.view addSubview:picker];
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    return 3;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    NSString * title = nil;
    switch(row) {
            case 0:
                title = @"a";
                break;
            case 1:
                title = @"b";
                break;
            case 2:
                title = @"c";
                break;
    }
    return title;
}

基本上,在viewDidLoad中,我们将创建一个UIPickerView并将其添加到视图中,告诉它我们的控制器同时用作delegatedataSource(您也可以在Interface Builder中完成此操作)
然后,我们将实现两个数据源方法,分别为numberOfComponentsinPickerView:pickerView:numberOfRowsInComponent:,以了解pickerView有多少组件以及每个组件有多少行
最后,我们实现了返回每一行内容的委托方法pickerView:titleForRow:forComponent:
如果您想自定义选中一行时的行为,可以实现委托方法pickerView:didSelectRow:inComponent:,顾名思义,每次选中一行时都会调用该方法。

mnowg1ta

mnowg1ta3#

UIPickerView使用与UITableView. DataSourceDelegate协议类似的部分。
DataSource方法用于告诉选择器中有多少“列”和多少“行”。
Delegate方法用于其余的,行高,列宽,视图或标题显示和回调时,选择器被移动。
用户界面选择器视图

相关问题