如何将Arrays从一个类传递到另一个类的UIPickerView?

时间:2011-08-29 03:24:44

标签: objective-c xcode4 cocos2d-iphone

呸。在过去的几天里,我已经把头发拉过来解决了这个问题,但是我知道我必须忽视这个问题。我已经制作了我的PickerViewController(.h。/ m)和PickerViewAppDelegate(.h / .m)文件,它们作为一个独立的程序运行良好,但我希望在我的“程序事件发生后弹出选择器” helloworld.m“文件。我可以让选择器出现,但我不能为我的生活弄清楚如何填充它以使它不是空白。我认为我已经完成了所有工作,直到我尝试将我的数组传递给我的pickerview对象。我究竟做错了什么?

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];
    scrollerData = 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;
...

2 个答案:

答案 0 :(得分:0)

好吧,我认为你应该使用property / synthesize将数组从HelloWorld类传递给PickerViewController类。

答案 1 :(得分:0)

您已经覆盖了PickerViewController中@synthesize生成的setter方法,因此您不再保留它。

然后,你在pickerView上调用setScrollerData(这应该会给你一个警告或崩溃,因为pickerView没有响应那个方法)。

您没有将PickerViewController设置为helloworld.m中选取器视图的委托或数据源。

我看不到你的hello世界代码适合的位置。它似乎是添加一个新的选择器视图而不是使用PickerViewController的xib中的那个。您应该从hello world实例化pickerviewcontroller并将其.view作为子视图添加或将其作为模态视图控制器呈现,而不是设置新的选取器视图。然后,您可以将数组传递给pickerviewcontroller的实例。请注意,对于本质上是子视图的单独视图控制器并不标准,虽然我对cocos2d知之甚少,所以在使用该框架时我不知道这是否正常。