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

时间:2012-07-10 19:01:04

标签: iphone ios uiview uiviewcontroller uipickerview

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

  1. 到目前为止,我创建了一个视图并将其绑定到我的类TestView.h
  2. 然后我在故事板(iOS 5)的视图中添加了一个选择器视图
  3. 然后我为这个选择器视图创建了一个类:

    @interface TestPickerView : UIPickerView <UIPickerViewDelegate, UIPickerViewDataSource> 
    {  
      NSArray *data;  
    }
    
  4. 然后尝试将属性添加到我的普通视图(TestView.h)

    #import "TestPickerView.h"
    @interface TestView : UIViewController
      @property (strong, nonatomic) IBOutlet TestPickerView *myTestPicker;
    @end
    
  5. 但是如何将普通视图中的UIPickerView绑定到此类/属性?

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

2 个答案:

答案 0 :(得分:1)

请致电您的TestView&gt;&gt; TestViewController而是,因为它是一个控制器。

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

enter image description here
enter image description here

之后只需创建三个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
    }
}

答案 1 :(得分:0)

在这里你老兄,这就是我在我的几个应用和游戏中做到的。

 #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];

〜/行结束