2012-07-10 56 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

相關問題