2012-07-10 59 views
2

好吧我试图连接一个UIPickerView自定义类。这个想法是在一个普通视图中有3个选择器视图。iOs5,试图了解UIPickerView以及如何将它连接到我的自定义类

  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 
    

但是,我如何将我的普通视图内的UIPickerView绑定到这个类/属性?

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

+0

你为什么不能创建TestPickerView类的PickerView的三个属性并将其添加到您的视图中。 – 2012-07-10 19:27:35

+0

那么我该如何知道哪个选取器需要哪些数据?以及哪些选择器值已被更改? – Patrick 2012-07-10 21:05:36

+0

基于pickerView索引,您可以识别。 – 2012-07-11 06:12:43

回答

1

请改为拨打您的TestView >>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 
    } 
} 
+0

以及如何知道调用数据(数据源)的pickerview?那价值有什么变化?最后,在同一个UIViewController中它将是3个采样器。 – Patrick 2012-07-10 20:03:17

+0

发送委托消息的选取器视图将自身作为参数之一传递。将该对象与你的类的属性进行比较,以了解你正在处理哪个选择器。 – 2012-07-10 22:08:40

+0

看到我编辑的答案。 – calimarkus 2012-07-11 16:45:32

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

〜/结束

+0

注意这是针对一个选取器的,但您可以根据需要添加尽可能多的选项。只是使用最后一位实例化它们 – 2012-07-10 19:28:30

+0

将对此进行试验,看看我是否可以使其工作。如何将拾取器数据传递给拾取器,以及如何在拾取器发生变化时捕获它。 (我的意思是,实际上改变了什么选择器)。 – Patrick 2012-07-10 21:07:25

+0

对不起有点新iPhone编程。 – Patrick 2012-07-10 21:10:23

相关问题