Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to pass Arrays to a UIPickerView from one class to another?
    primarykey
    data
    text
    <p>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? </p> <p><strong>PickerViewController.h</strong></p> <pre><code>#import &lt;UIKit/UIKit.h&gt; @interface PickerViewController : UIViewController &lt;UIPickerViewDataSource, UIPickerViewDelegate&gt; { IBOutlet UIPickerView *pickerView; NSMutableArray *scrollerData; } @property (nonatomic, retain) IBOutlet UIPickerView *pickerView; @property (nonatomic, retain) NSMutableArray *scrollerData; -(void)setScrollerData:(NSMutableArray *)array; @end </code></pre> <p><strong>PickerViewController.m</strong></p> <pre><code>#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); } </code></pre> <p><strong>PickerViewAppDelegate.h</strong></p> <pre><code>#import &lt;UIKit/UIKit.h&gt; @class PickerViewController; @interface PickerViewAppDelegate : NSObject &lt;UIApplicationDelegate&gt; { UIWindow *window; PickerViewController *pvController; } @property (nonatomic, retain) IBOutlet UIWindow *window; @end </code></pre> <p><strong>PickerViewAppDelegate.m</strong></p> <pre><code>#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 </code></pre> <p><strong>Helloworld.m</strong></p> <pre><code>... 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; ... </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload