iOs5,试图了解 UIPickerView 以及如何将其连接到我的自定义类

Posted

技术标签:

【中文标题】iOs5,试图了解 UIPickerView 以及如何将其连接到我的自定义类【英文标题】:iOs5, Trying to understand the UIPickerView and how to connect it to my custom class 【发布时间】:2012-07-10 19:01:04 【问题描述】:

好的,我正在尝试将 UIPickerView 与自定义类连接起来。这个想法是在一个普通视图中有 3 个选择器视图。

    到目前为止,我已经创建了一个视图并将其绑定到我的类 TestView.h 然后我在情节提要的视图中添加了一个选择器视图 (ios 5)

    然后我为这个选择器视图创建了一个类:

    @interface TestPickerView : UIPickerView <UIPickerViewDelegate, UIPickerViewDataSource> 
      
      NSArray *data;  
    
    

    然后尝试将属性添加到我的普通视图(TestView.h)

    #import "TestPickerView.h"
    @interface TestView : UIViewController
      @property (strong, nonatomic) IBOutlet TestPickerView *myTestPicker;
    @end
    

但是如何将普通视图中的 UIPickerView 绑定到此类/属性?

我最终将拥有 3 个 UIPickerView,我的想法是在我的 UIViewController 中有 3 个引用来控制这些 UIPickerViews。这样我就可以在加载普通视图时使用属性设置数据(数据源)一次,然后 PickerViews 就会显示出来。希望当其中一个视图中的值发生时,我也能够在我的正常视图中得到通知。

【问题讨论】:

为什么你不能创建你的TestPickerView类的PickerView的三个属性并将它添加到你的视图中。 然后我怎么知道哪个选择器需要什么数据?以及已更改的选择器值? 根据pickerView索引,可以识别。 【参考方案1】:

请致电您的TestView >> TestViewController,因为它是一个控制器。

在您的故事板中,选择 PickerView 并将其类名更改为 TestPickerView

之后,只需创建您的三个 IBOutlets 并连接 PickerViews。就是这样。

// 编辑:解释一下,你如何区分选择器。做3个插座,例如:

IBOutlet TestPickerView *picker1;
IBOutlet TestPickerView *picker2;
IBOutlet TestPickerView *picker3;

然后在您的委托方法中,检查哪个选择器确实调用了委托,例如:

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

    if(pickerView == self.picker1)
    
        // picker1
    
    else if(pickerView == self.picker2)
    
        // picker2
    
    else
    
        // picker3
    

【讨论】:

我怎么知道哪个pickerview正在调用数据(数据源)?它的价值改变了吗?最终会是同一个 UIViewController 中的 3 个选择器。 发送委托消息的选择器视图将自身作为参数之一传递。将该对象与您的类上的属性进行比较,以了解您正在处理的选择器。 我错过了什么,UIPickerView 的子类是什么? @jrturton,我也在想同样的事情。起初我认为子类会更干净,但当我完成来回传递参数时,我认为情况并非如此。有人对子类有充分的理由吗?【参考方案2】:

给你,伙计,这就是我在几个应用和游戏上的做法。

 #import <UIKit/UIKit.h>
    #pragma mark - Delegate Protocol 
    @protocol someDelegate
    @required
    -(void)somePickerFinishedPicking:(id)item;
    @end

    #pragma mark - Class interface 
    @interface SomePicker : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource>
    
        NSMutableArray* dataSource;
    

    #pragma mark - Property Istantiation 

    @property (nonatomic, retain) NSMutableArray* dataSource;
    @property (nonatomic, retain) id <someDelegate> pickDelegate;

    #pragma mark - Constructors / Destructors

    - (id)   initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil;
    - (void) didReceiveMemoryWarning;
    - (void) dealloc;
    - (void) createDataSource;

    #pragma mark - View Lifecycle
    - (void) viewDidLoad;

    #pragma mark - UIPicker Protocols

    -(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;
    -(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;
    -(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component;
    -(UIView*) pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view;

    #pragma mark - Delegate Protocols

    -(void) handlePickerDidFinish:(id)item;

    @end

这个给你的.m

#pragma mark - Class Implementation 
@implementation SomePicker

#pragma mark - Variable synthesize 
// ARRAYS
@synthesize dataSource;

// DELEGATES
@synthesize pickDelegate = _pickDelegate;

#pragma mark - Constructors / Deconstructors 
// Class initialization
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil;

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) 
        dataSource = [[NSMutableArray alloc] init];
        [self createDataSource];
    
    return self;


// Handles memory warning events
- (void)didReceiveMemoryWarning

    // Release the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.


// Garbage Collection
- (void) dealloc;

    // Release what you need
    self.dataSource = nil;
    [super dealloc];


// Creates the occasion entries for the picker
-(void)createDataSource

    NSMutableDictionary* dataDictionary = [[NSMutableDictionary alloc] init];
    // create your data source here or just forget about this and pass it from the parentViewController.
    [dataDictionary release];


#pragma mark - View lifecycle
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad;

    [super viewDidLoad];

    UIPickerView* occasionsPicker = [[UIPickerView alloc] init];
    [occasionsPicker setDataSource:self];
    [occasionsPicker setDelegate:self];
    [occasionsPicker setTag:888];
    [occasionsPicker selectRow:500 inComponent:0 animated:YES];
    [self.view addSubview:occasionsPicker];
    [occasionsPicker release];

    [self handlePickerDidFinish:[[self.dataSource objectAtIndex:(500 % self.dataSource.count)] objectForKey:@"key"]];


#pragma mark - UIPicker Protocols

// Creates the rows in the picker.
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
   
    // Endless roll illusion else just bind it to the size of the data source
    return 1000;


// Determines the number of columns in the picker
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView

    // Add however many columns you need
    return 1;


// Handles the event when the user picks a row.
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component

    //this does something with the row selected
    [self handlePickerDidFinish:[[self.dataSource objectAtIndex:(row % self.dataSource.count)] objectForKey:@"key"]];


// Creates the custom view for each cell so the text shows in accordance to the App Style
-(UIView*) pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view

    UILabel* customRowLabel = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, [pickerView rowSizeForComponent:component].width, [pickerView rowSizeForComponent:component].height)] autorelease];

    customRowLabel.font = [UIFont fontWithName:@"HelveticaNeue" size: 16];
    customRowLabel.textColor = [UIColor colorWithRed:kColorRed green:kColorGreen blue:kColorBlue alpha:1];
    customRowLabel.textAlignment = UITextAlignmentCenter;
    customRowLabel.backgroundColor = [UIColor clearColor];

    customRowLabel.text = [[self.dataSource objectAtIndex:(row % self.dataSource.count)] objectForKey:@"key"];

    return customRowLabel;


#pragma mark - Delegate Protocols 
// Notifies Delegate class that an action has been perfomed and passes the Mood String selected
-(void)handlePickerDidFinish:(id)item

    [self.pickDelegate somePickerFinishedPicking:item];


@end

然后像这样在你的父 ViewController 上实例化它:

CGRect rectPicker = CGRectMake(60, 100, 200, 216);
self.somePicker = [[[SomePicker alloc] init] autorelease];
[self.somePicker setPickDelegate:self];
[self.somePicker.view setBackgroundColor:[UIColor clearColor]];
self.somePicker.view.frame = rectPicker;
[self.somePicker.view setTag:777];
[self.somePicker.view setAlpha:0];
[self.view addSubview:self.somePicker.view];

~/行尾

【讨论】:

注意这是一个选择器,但您可以添加任意数量的选择器。只需使用最后一位实例化它们 将对此进行试验,看看我是否可以让它工作。我如何将选择器数据传递给选择器,以及如何在选择器更改时捕获。 (我的意思是,实际上改变了什么选择器)。 抱歉对 iPhone 编程有点陌生。 我没有为那个特定的例子使用故事板,我以编程方式实例化了它们。

以上是关于iOs5,试图了解 UIPickerView 以及如何将其连接到我的自定义类的主要内容,如果未能解决你的问题,请参考以下文章

当在 UIPickerView 中选择某些内容时,我试图让按钮消失

了解 UIPickerView 的工作原理

UIPickerView 弹出窗口

如何正确禁用 UIPickerView 组件滚动?

如果 UIPickerView 未触及,则 UIPickerView 选择第 0 行

为啥我的 UIPickerView 在选择时崩溃