I am trying to create a uiPickerView programmatically and add it to the view without using the interface constructor. Don't get me wrong, I like IB, but the reason I want to do this is because I am trying to create an object that I can quickly connect to create pop-up menus using the UIPopoverViewController and various subtypes (like uiPickerView for example ) as a menu in a popup window. I already did this job by creating a menu in IB and initializing a popup using the ViewController, so I know how this works for the most part.
I entered the appropriate code below, and these are two errors that I get when I start: - "Could not find the displayed image UIPickerViewFrameRight-162-Popover.png" - "Could not find the displayed image UIPickerViewFrameRight-162-Popover.png"
I do not know what kind of images these are, but I assume that they are picker png representations.
menu = [[UIPickerView alloc]initWithFrame:CGRectMake(0,100,162,162)]; menu.delegate = self; menu.dataSource = self; [menu reloadAllComponents]; [menu selectRow:0 inComponent:0 animated:YES];
Now this code will crash the program if you do not delete the line where I am trying to add a collector to the view, after which I just get an empty spot. Therefore, I know that this is the collector that causes this problem, but I do not know how to fix it. I searched all day, but every online tutorial on uipickers includes using IB. I suppose this is a really stupid mistake, for example, the lack of imports or something like that, but if someone tells me what I'm doing wrong, we will be very grateful.
Also note that I have been following tutorials on setting up the dataSource and delegation methods for UIPickerView, and I'm sure they are fine, but if you want to check that you are here: Thanks again.
#import "PopUpMenuViewController.h" @implementation PopUpMenuViewController @synthesize menuType; @synthesize data; @synthesize popView; @synthesize menu; @synthesize customViewController; #pragma mark - #pragma mark UIPOPOVERCONTROLLER DELEGATE METHODS #pragma mark - - (BOOL)popoverControllerShouldDismissPopover:(UIPopoverController *)popoverController{ //Delegate this too the User of this class return TRUE; } - (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController{ //Delegate this too the User of this class } #pragma mark - #pragma mark CUSTOM POPOVERVIEWCONTROLLER METHODS #pragma mark - -(void) initWithMenuType:(int)type{ menuType = type; } -(id) initWithMenuType:(int)type andData:(NSMutableArray *)dataSet fromViewItem:(id)sender withMainView:(UIView *)mView{ [super init]; menuType = type; data = dataSet; rootView = sender; displayView = mView; arrowDir = UIPopoverArrowDirectionUp; customViewController = [[UIViewController alloc] initWithNibName:@"PopUpMenu" bundle:nil]; return self; } -(void) setPopUpArrowDirection:(UIPopoverArrowDirection) arrow{ arrowDir = arrow; } -(void) showPopUp{ //UIPicker Menu if (menuType==1) { //UIPicker Setup menu = [[UIPickerView alloc]initWithFrame:CGRectMake(0,100,162,162)]; menu.delegate = self; menu.dataSource = self; [menu reloadAllComponents]; [menu selectRow:0 inComponent:0 animated:YES]; //Add the picker to the view [customViewController.view addSubview:menu]; popView = [[UIPopoverController alloc] initWithContentViewController:customViewController] ; [popView setDelegate:self]; CGRect pos = [rootView frame]; [popView presentPopoverFromRect:CGRectMake(pos.origin.x,pos.origin.y,0,pos.size.height) inView:displayView permittedArrowDirections:arrowDir animated:YES]; //[popView setPopoverContentSize:CGSizeMake(menu.frame.size.width+5,menu.frame.size.height+5)]; } } #pragma mark - #pragma mark VIEW CONTROLLER DELEGATE METHODS #pragma mark - // The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad. /* - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization. } return self; } */ /* // Implement loadView to create a view hierarchy programmatically, without using a nib. - (void)loadView { } */ /* // Implement viewDidLoad to do additional setup after loading the view, typically from a nib. - (void)viewDidLoad { [super viewDidLoad]; }*/ - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { // Overriden to allow any orientation. return YES; } - (void)didReceiveMemoryWarning { // Releases the view if it doesn't have a superview. [super didReceiveMemoryWarning]; // Release any cached data, images, etc. that aren't in use. } - (void)viewDidUnload { [super viewDidUnload]; // Release any retained subviews of the main view. // eg self.myOutlet = nil; } - (void)dealloc { [super dealloc]; [data release]; [popView release]; [menu release]; [rootView release]; [displayView release]; [customViewController release]; } #pragma mark - #pragma mark UIPICKERVIEW DELEGATE & DATASOURCE METHODS #pragma mark - #pragma mark - #pragma mark UIPickerViewDataSource Methods - (NSInteger) pickerView: (UIPickerView *) pickerView numberOfRowsInComponent: (NSInteger) component { return [data count]; } - (NSInteger) numberOfComponentsInPickerView: (UIPickerView *) pickerView { return 1; } #pragma mark - #pragma mark UIPickerViewDelegate Methods // Row height in pixels - (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component { return 40.0f; } // Column width in pixels - (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component { return 90.0f; } - (NSString *) pickerView: (UIPickerView *) pickerView titleForRow: (NSInteger) row forComponent: (NSInteger) component { return [data objectAtIndex:row]; } - (void) pickerView: (UIPickerView *) pickerView didSelectRow: (NSInteger) row inComponent: (NSInteger) component { }