Utilisation des boutons

Les boutons sont utilisés pour gérer les actions de l'utilisateur. Ils interceptent les événements tactiles et envoient des messages aux objets cibles.

Bouton rectangle arrondi

button

Propriétés du bouton dans xib

Vous pouvez modifier les propriétés du bouton xib dans l'inspecteur Attributs de la zone Utilitaires (côté droit de la fenêtre).

buttonAttribute

Type de bouton

  • UIButtonTypeCustom
  • UIButtonTypeRoundedRect
  • UIButtonTypeDetailDisclosure
  • UIButtonTypeInfoLight
  • UIButtonTypeInfoDark
  • UIButtonTypeContactAdd

Propriétés importantes

  • imageView
  • titleLabel

Méthodes importantes

+ (id)buttonWithType:(UIButtonType)buttonType
- (UIImage *)backgroundImageForState:(UIControlState)state
- (UIImage *)imageForState:(UIControlState)state
- (void)setTitle:(NSString *)title forState:(UIControlState)state
- (void)addTarget:(id)target action:(SEL)action forControlEvents: (UIControlEvents) controlEvents
Ajouter une méthode personnalisée addDifferentTypesOfButton
-(void)addDifferentTypesOfButton
{
    // A rounded Rect button created by using class method
    UIButton *roundRectButton = [UIButton buttonWithType:
    UIButtonTypeRoundedRect];
    [roundRectButton setFrame:CGRectMake(60, 50, 200, 40)];
    // sets title for the button
    [roundRectButton setTitle:@"Rounded Rect Button" forState:
    UIControlStateNormal];
    [self.view addSubview:roundRectButton];

    UIButton *customButton = [UIButton buttonWithType: UIButtonTypeCustom];
    [customButton setBackgroundColor: [UIColor lightGrayColor]];
    [customButton setTitleColor:[UIColor blackColor] forState:
    UIControlStateHighlighted];
    //sets background image for normal state    
    [customButton setBackgroundImage:[UIImage imageNamed:
    @"Button_Default.png"] 
    forState:UIControlStateNormal];
    //sets background image for highlighted state
    [customButton setBackgroundImage:[UIImage imageNamed: 
    @"Button_Highlighted.png"] 
    forState:UIControlStateHighlighted];
    [customButton setFrame:CGRectMake(60, 100, 200, 40)];
    [customButton setTitle:@"Custom Button" forState:UIControlStateNormal];
    [self.view addSubview:customButton];

    UIButton *detailDisclosureButton = [UIButton buttonWithType:
    UIButtonTypeDetailDisclosure];
    [detailDisclosureButton setFrame:CGRectMake(60, 150, 200, 40)];
    [detailDisclosureButton setTitle:@"Detail disclosure" forState:
    UIControlStateNormal];
    [self.view addSubview:detailDisclosureButton];

    UIButton *contactButton = [UIButton buttonWithType:
    UIButtonTypeContactAdd];
    [contactButton setFrame:CGRectMake(60, 200, 200, 40)];
    [self.view addSubview:contactButton];

    UIButton *infoDarkButton = [UIButton buttonWithType:
    UIButtonTypeInfoDark];
    [infoDarkButton setFrame:CGRectMake(60, 250, 200, 40)];
    [self.view addSubview:infoDarkButton];

    UIButton *infoLightButton = [UIButton buttonWithType:
    UIButtonTypeInfoLight];
    [infoLightButton setFrame:CGRectMake(60, 300, 200, 40)];
    [self.view addSubview:infoLightButton];
}

Remarque :

Nous allons ajouter les images nommées "Button_Default.png" et "Button_Highlighted.png" à notre projet, ce qui peut être fait en faisant glisser les images vers la zone de navigation qui liste les fichiers de notre projet.

Mettez à jour viewDidLoad dans ViewController.m comme suit :

(void)viewDidLoad
{
    [super viewDidLoad];
    //The custom method to create our different types of button is called
    [self addDifferentTypesOfButton];
    //Do any additional setup after loading the view, typically from a nib
}

Sortie

Maintenant, lorsque nous exécutons l’application, nous obtenons la sortie suivante :

buttonOutput

Autres extensions