【问题标题】:Understanding how UIPickerView works了解 UIPickerView 的工作原理
【发布时间】:2016-01-14 10:39:47
【问题描述】:

相当新,我需要了解 UIPickerViews。

我以编程方式为我的项目创建了一个 UIPickerView:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    UIPickerView *myPickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 200, 375, 200)];
    myPickerView.delegate = self;
    myPickerView.showsSelectionIndicator = YES;
    [self.view addSubview:myPickerView];   
}

然后添加了行数的方法:

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    NSUInteger numRows = 5;

    return numRows;
}

按预期返回五个问号。然后我可以继续创建一个数组来填充这些行等......但接下来我会添加另一个 UIPickerView,如下所示:

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    UIPickerView *myPickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 200, 375, 200)];
    myPickerView.delegate = self;
    myPickerView.showsSelectionIndicator = YES;
    [self.view addSubview:myPickerView];

    UIPickerView *my2PickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 400, 375, 200)];
    my2PickerView.delegate = self;
    my2PickerView.showsSelectionIndicator = YES;
    [self.view addSubview:my2PickerView];
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    NSUInteger numRows = 5;

    return numRows;
}

现在我有两个pickerview 控制器,它们都有五行。我的问题是如何选择该方法适用于哪个pickerview,还有谁能解释为什么该方法适用于项目中的所有pickerview?谢谢。

【问题讨论】:

    标签: objective-c uipickerview


    【解决方案1】:

    两个 PickerViews 只有一个委托方法;这是我不喜欢 iOS 的东西,但你在这里没有选择。

    你必须if-statement自己摆脱这个。

    委托方法中的pickerView参数是被分配行数的pickerview。

    请注意,这对于 iOS 的任何常用委托方法都有效,无论是您的pickerview、tableview、collectionView 的numberOfRows,还是任何具有view in 参数的委托方法。

    简单易懂的方法是将您的选择器视图作为您的类(或属性)的字段,然后简单地与它比较参数。

    @interface ViewController ()
    @property (weak, nonatomic) UIPickerView *_mySexyPickerView;
    @property (weak, nonatomic) UIPickerView *_myOtherPickerView;
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
    
        _mySexyPickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 200, 375, 200)];
        _mySexyPickerView.delegate = self;
        _mySexyPickerView.showsSelectionIndicator = YES;
        [self.view addSubview:_mySexyPickerView];
    
        _myOtherPickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 400, 375, 200)];
        _myOtherPickerView.delegate = self;
        _myOtherPickerView.showsSelectionIndicator = YES;
        [self.view addSubview:_myOtherPickerView];
    }
    
    - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
        if (pickerView == _mySexyPickerView){
             return 2;
        }
    
        if (pickerView == _myOtherPickerView){
             return 19;
        }
        return 0;
    }
    

    【讨论】:

    • 非常感谢您的回答。我发现理解委派具有挑战性。只是出于兴趣,我还可以在这一行写什么来代替“self”:myPickerView.delegate = self;?
    • 任何东西都可以是您的pickerviews委托,但通常的做法是将该角色赋予pickerview所在的控制器(因此也是self)。
    • 只有当它符合所需的协议时,您才能使视图控制器成为某物的委托。例如,在这里,self 符合 UIPickerViewDelegate,只有这样您才能将 self 指定为您的委托'myPickerView' 属性。
    • 好的,我想我明白了,我可以创建一个类并将其设为委托。这意味着控制行数、组件数、标题等的所有方法都必须在该类上实现。这样的事情是否有意义 myPickerView.delegate = MyNewClass;?许多交易
    • 没关系的。在这种情况下,您将有两个单独的类(或一个类的实例)作为委托,这可能比丑陋的 if 语句更清晰
    猜你喜欢
    • 2016-02-20
    • 2012-12-11
    • 2011-02-18
    • 2018-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多