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;
...
好吧,現在我感覺非常愚蠢的,但我給我最好的拍攝。當我從@synthesize中移除setScrollerData時,我在頭文件中得到一個警告,告訴我需要寫一個方法或合成它。我想這就是爲什麼我加了它,儘管當時對我來說沒有意義。你可以擴展你的意思嗎?「添加一個新的選擇器,而不是使用xib中的一個」?我想使用PVC中的那個,但不知道如何讓選取器視圖激活。如果我不嘗試傳遞任何值,選擇器視圖會彈出,但它只是空白。 – PWiggin
已更新的答案。我假定你好世界是一個視圖控制器或等價物。 – jrturton
cocos2d問題是爲什麼我使用它作爲子視圖。我不相信UIPickerView與其他CCNode兼容,所以解決方法是將其設置爲子視圖。至於實例化pickerviewcontroller,我是否正確理解我應該使用「PickerViewController * pickerView = [[PickerViewController alloc] init];」「來爲picker分配內存? – PWiggin