2010-11-19 2 views
3

J'essaie de créer un uiPickerView par programme et de l'ajouter à une vue sans utiliser de constructeur d'interface. Ne vous méprenez pas, j'aime IB mais la raison pour laquelle je veux le faire de cette façon est parce que j'essaie de construire un objet que je peux rapidement brancher pour produire des menus contextuels en utilisant UIPopoverViewController et une variété de sous-vues différentes (comme uiPickerView) comme le menu dans le popup. J'ai déjà fait ce travail en construisant le menu dans IB et en initialisant le popup avec son ViewController, donc je sais comment cela fonctionne pour la plupart.en train de corriger cette erreur: Impossible de trouver l'image mappée UIPickerViewFrameLeft-162-Popover.png

Je suis entré dans le code correspondant ci-dessous et voici les deux erreurs que je reçois quand je le lance: - « Impossible de trouver l'image cartographiée UIPickerViewFrameRight-162-Popover.png » - « Impossible de trouver l'image cartographiée UIPickerViewFrameLeft -162-Popover.png "

Je ne sais pas ce que sont ces images mais je suppose qu'elles sont les png de la vue du sélecteur.

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

Maintenant, ce code va planter le programme sauf si vous supprimez la ligne où je tente d'ajouter le sélecteur à la vue, à quel point je reçois juste le popover blanc. Donc, je sais que c'est le sélecteur qui cause ce problème mais je ne sais pas comment le réparer. J'ai cherché toute la journée mais chaque tutoriel en ligne sur uipickers comprend l'utilisation d'IB. Je suppose que c'est une erreur vraiment stupide comme manquer une importation ou quelque chose, mais si quelqu'un peut me dire ce que je fais mal, il serait grandement apprécié.

Notez également que j'ai suivi les tutoriels sur comment configurer les méthodes dataSource et déléguer pour UIPickerView et je suis assez sûr qu'ils sont bien, mais si vous voulez vérifier ici, vous êtes: Merci encore.

#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. 
    // e.g. 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 { 
} 

Répondre

2

Bon et bien je ne suis pas tout à fait sûr ce qui est arrivé, mais j'ai supprimé le projet et re-écrit ce code et voila ... Plus de questions.

5

Si quelqu'un d'autre rencontre cet avertissement particulier dans la console "Impossible de trouver l'image mappée UIPickerViewFrameRight-162-Popover.png", je pense avoir compris pourquoi il apparaît. Le HIG indique vaguement que UIPickerView ne doit être ajouté qu'à un Popover sur l'iPad.

"Sur l'iPad, présentez un sélecteur de date et d'heure uniquement dans un popover."

http://developer.apple.com/library/ios/#documentation/userexperience/conceptual/mobilehig/UIElementGuidelines/UIElementGuidelines.html

Dans mes expériences, le UIPickerView doit être la vue directe et seulement du UIViewController courant dans un popover. Si le UIPickerView apparaît ailleurs dans une hiérarchie de vue, l'avertissement apparaît et le UIPickerView semblera mauvais (comme il manque les parties gauche et droite de la vue).

Dans le code ci-dessus, vous pouvez voir que le UIPickerView a été ajouté en tant que sous-vue du rootView dans le customController:

[customViewController.view addSubview:menu]; 

Cela aurait probablement travaillé si le UIPickerView était la vue racine dans le customController.

Vous pouvez programme faire le UIPickerView la vue racine du contrôleur en redéfinissant la méthode loadview du contrôleur et l'affectation du UIPickerView directement à la vue racine du contrôleur:

- (void)loadView { 
    CGRect frame = CGRectMake(0,0, 300, 300); 
    UIPickerView *v = [[[UIPickerView alloc] initWithFrame:frame] autorelease]; 
    v.delegate = self; // assuming controller adopts UIPickerViewDelegate 
    v.dataSource = self; // assuming controller adopts UIPickerViewDataSource 

    self.view = v; 
} 

Hope this helps quelqu'un.

0

J'ai récemment rencontré le même problème avec mon application sur iPad et iPhone. C'était en fait une solution simple mais stupide qui impliquait de faire de la hauteur sur l'iPad à 180 et 162 sur l'iPhone. Dieu doit aimer apple s/w mais je ne le fais pas.