2

好的,我正在尝试将 aUIPickerView与自定义类连接起来。这个想法是在一个普通视图中有 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
    

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

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

4

2 回答 2

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
    }
}
于 2012-07-10T19:11:34.897 回答
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];

~/行尾

于 2012-07-10T19:27:29.653 回答