开发者

How to pass Arrays to a UIPickerView from one class to another?

Bah. I've pulled my hair out over this problem for the past couple days now, but I know I must be overlooking the obvious. I've made my PickerViewController(.h./m) and PickerViewAppDelegate(.h/.m) files and they run fine as a standalone program, but I would like to have the picker pop up after a procedureal event occurs in my "helloworld.m" file. I can get the picker to show up, but I cannot for the life of me figure out how to populate it so that it isn't blank. I THINK I've done everything right up until I try to pass my array to my pickerview object. What am I doing wrong?

PickerViewController.h

#import <UIKit/UIKit.h>

@interface PickerViewController : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate> {

    IBOutlet UIPickerView *pickerView;
    NSMutableArray *scrollerData;
}
@property (nonatomic, retain) IBOutlet UIPickerView *pickerView;
@property (nonatomic, retain) NSMutableArray *scrollerData;

-(void)setScrollerData:(NSMutableArray *)array;
@end

PickerViewController.m

#import "PickerViewController.h"


@implementation PickerViewController
@synthesize pickerView, scrollerData;


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];

    self.pickerView.delegate = self;
    self.pickerView.dataSource = self;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
    // Release anything that's not essential, such as cached data
}


- (void)dealloc {
//  [arrayColors release];
    [super dealloc];
}

-(void)setScrollerData:(NSMutableArray *)array
{
    //[self.scrollerData arrayByAddingObjectsFromArray:array];
    scr开发者_如何学编程ollerData = array;
}

#pragma mark -
#pragma mark Picker View Methods

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView {

    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component {

    return [scrollerData count];
}

- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {

    return [scrollerData objectAtIndex:row];
}

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

    NSLog(@"Selected Number: %@. Index of selected numbers: %i", [scrollerData objectAtIndex:row], row);
}

PickerViewAppDelegate.h

#import <UIKit/UIKit.h>

@class PickerViewController;

@interface PickerViewAppDelegate : NSObject <UIApplicationDelegate> {
    UIWindow *window;
    PickerViewController *pvController;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;

@end

PickerViewAppDelegate.m

#import "PickerViewAppDelegate.h"
#import "PickerViewController.h"

@implementation PickerViewAppDelegate

@synthesize window;


- (void)applicationDidFinishLaunching:(UIApplication *)application {    

    pvController = [[PickerViewController alloc] initWithNibName:@"PickerView" bundle:[NSBundle mainBundle]];

    [window addSubview:pvController.view];

    // Override point for customization after application launch
    [window makeKeyAndVisible];
}


- (void)dealloc {
    [pvController release];
    [window release];
    [super dealloc];
}


@end

Helloworld.m

...
        UIView* view = [[CCDirector sharedDirector] openGLView];

        UIPickerView *pickerView=[[UIPickerView alloc] init];
        pickerView.frame=CGRectMake(100,100, 200, 200);



        NSMutableArray *arrayNumbers = [[NSMutableArray alloc] init];
        [arrayNumbers addObject:@"30"];
        [arrayNumbers addObject:@"31"];
        [arrayNumbers addObject:@"32"];
        [arrayNumbers addObject:@"33"];
        [arrayNumbers addObject:@"34"];
        [arrayNumbers addObject:@"35"];
        [arrayNumbers addObject:@"36"];

        [pickerView setscrollerData: arrayNumbers];//Should I be calling pickerView here or something else?


        [view addSubview: pickerView];
        pickerView.hidden=NO;
...


You have overridden the setter method generated by @synthesize in PickerViewController, so you are no longer retaining it.

Then, you are calling setScrollerData on your pickerView (this should be giving you a warning or crashing since pickerView doesn't respond to that method).

You are not setting PickerViewController as the delegate or datasource of your picker view in helloworld.m.

I can't see where your hello world code fits in. It seems to be adding a new picker view rather than using the one from the xib of PickerViewController. You should be instantiating pickerviewcontroller from your hello world and adding its .view as a subview or presenting it as a modal view controller rather than setting up a new picker view. You can then pass your array to the instance of pickerviewcontroller. Note though that it is not standard to have a separate view controller for what is essentially a subview, though I don't have much knowledge of cocos2d so I don't know if this is normal when using that framework.


Well, i think you should just pass the array from HelloWorld class to PickerViewController class using property/synthesize.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜