2011-08-29 115 views
0

Bah。过去几天,我已经把这个问题拉过来了,但我知道我必须忽视这个明显的问题。我做了我的PickerViewController(.H。/ M)和PickerViewAppDelegate(.H/.M)文件,它们运行良好作为一个独立的程序,但我想有选择器弹出后procedureal事件发生我“ helloworld.m“文件。我可以让选择器出现,但是我不能为了我的生活而弄清楚如何填充它以便它不是空白的。我想我已经做了所有的事情,直到我尝试将我的数组传递给我的pickerview对象。我究竟做错了什么?如何将数组传递给UIPickerView从一个类到另一个类?

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

回答

0

您已经覆盖在PickerViewController @synthesize生成的setter方法,让你不再保留它。

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

您没有设置PickerViewController在helloworld.m你的选择器视图的委托或数据源。

我不能看到你的hello world代码相符。这似乎是增加一个新的选择器视图,而不是使用从PickerViewController的厦门国际银行之一。你应该从hello world中实例化pickerviewcontroller,并将它的.view作为子视图或者将其作为一个模态视图控制器呈现,而不是设置一个新的选择器视图。然后你可以将你的数组传递给pickerviewcontroller的实例。请注意,虽然我对cocos2d没有太多的了解,但是对于本质上是子视图的单独视图控制器并不是标准的,所以我不知道在使用该框架时这是否正常。

+0

好吧,现在我感觉非常愚蠢的,但我给我最好的拍摄。当我从@synthesize中移除setScrollerData时,我在头文件中得到一个警告,告诉我需要写一个方法或合成它。我想这就是为什么我加了它,尽管当时对我来说没有意义。你可以扩展你的意思吗?“添加一个新的选择器,而不是使用xib中的一个”?我想使用PVC中的那个,但不知道如何让选取器视图激活。如果我不尝试传递任何值,选择器视图会弹出,但它只是空白。 – PWiggin

+0

已更新的答案。我假定你好世界是一个视图控制器或等价物。 – jrturton

+0

cocos2d问题是为什么我使用它作为子视图。我不相信UIPickerView与其他CCNode兼容,所以解决方法是将其设置为子视图。至于实例化pickerviewcontroller,我是否正确理解我应该使用“PickerViewController * pickerView = [[PickerViewController alloc] init];”“来为picker分配内存? – PWiggin

0

嗯,我想你应该只是通过从HelloWorld类到PickerViewController类使用属性/合成阵列。

相关问题