2008-11-12 6 views

Répondre

7

J'ai trouvé une solution à: http://www.iphonedevsdk.com/forum/iphone-sdk-development/1704-uitextfield-inside-uialertview.html

Voici le code qui fonctionne pour moi.

UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Your title here" message:@"this gets covered" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil]; 
UITextField *myTextField = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 45.0, 260.0, 25.0)]; 

CGAffineTransform myTransform = CGAffineTransformMakeTranslation(0.0, 130.0); 
[myAlertView setTransform:myTransform]; 

[myTextField setBackgroundColor:[UIColor whiteColor]]; 
[myAlertView addSubview:myTextField]; 
[myAlertView show]; 
[myAlertView release]; 
4

Vous devez créer votre popup (UIAlertView) et ajouter un UITextField (ou n'importe quel composant que vous souhaitez utiliser) comme sous-vue. L'UIAlertView ne sera pas automatiquement redimensionné pour le composant que vous ajoutez, vous devez donc hacker cette partie en y ajoutant du texte. Le texte augmentera la hauteur de votre popup et à condition que vous n'ajoutez pas trop d'être caché par votre composant.

1

Ajoutez cette fonction à votre code, elle est appelée avant l'affichage de l'alerte.

  • (void) willPresentAlertView: (UIAlertView *) alertView { alertView.frame = CGRectMake (x, y, largeur, hauteur); }
1

Une astuce pour redimensionner votre UIAlertView ...

Comme Milen Milkovski écrit ci-dessus/ci-dessous, si vous définissez un délégué pour la UIAlertView:

UIAlertView* theAlert = [[UIAlertView alloc] initWithTitle:@"Lah" 
                message:@"dee dah" 
                delegate:self 
             cancelButtonTitle:nil 
             otherButtonTitles:nil]; 
//NSLog(@"Pre Show: alert frame x,y: %f,%f, alert frame width,height: %f,%f", theAlert.frame.origin.x,theAlert.frame.origin.y, theAlert.frame.size.width, theAlert.frame.size.height); 
[retrievingListAlert show]; 

Vous pouvez ensuite modifier la cadre de l'UIAlertView en définissant le rappel UIAlertView suivant (dans la classe délégué - dans ce cas, puisque nous avons utilisé self, la même classe que celle où UIAlertView a été créé):

(void)willPresentAlertView:(UIAlertView *)alertView { 
    //NSLog(@"willPresentAlertView: alert frame midx,midy: %f,%f, alert frame width,height: %f,%f", alertView.frame.origin.x, alertView.frame.origin.y, alertView.frame.size.width, alertView.frame.size.height); 
    alertView.frame = CGRectMake(x, y, width, heigth); 
} 

J'ai trouvé que le réglage du cadre à d'autres moments ne fonctionnera pas. Il semble que la fonction show modifie le cadre, vraisemblablement en l'autorisant à son contenu.

Questions connexes