Christian Ziemer
Christian Ziemer

Reputation: 125

Change Text or Background of UIButton after a tap

I try to change the the Look of 3 Buttons (TitleLable + Background) and Text of a TextView in dependents of the click on one of the 3 Buttons. (Something like Tabbed Content)

I 've created the 4 elements in the xib, and linked them with the .h:

   IBOutlet UIButton *buttonTab1;
    IBOutlet UIButton *buttonTab2;
    IBOutlet UIButton *buttonTab3;

    IBOutlet UITextView *thisDescription;

    - (IBAction)showTab1:(id)sender;
    - (IBAction)showTab2:(id)sender;
    - (IBAction)showTab3:(id)sender;

@property (strong, nonatomic) IBOutlet UIButton *buttonTab1;
@property (strong, nonatomic) IBOutlet UIButton *buttonTab2;
@property (strong, nonatomic) IBOutlet UIButton *buttonTab3;

at the .m i try to change the 4 Elements (here the code for Button 1):

@synthesize buttonTab1, buttonTab2, buttonTab3;

- (IBAction)showTab1:(id)sender{
        thisDescription.text = [NSString stringWithFormat:@"INHALT TAB 1: %@",transferDescription];
        buttonTab1.imageView.image = [UIImage imageNamed:@"content_tab1_on.png"];
        buttonTab2.imageView.image = [UIImage imageNamed:@"content_tab2.png"];
        buttonTab3.imageView.image = [UIImage imageNamed:@"content_tab3.png"];
        buttonTab1.titleLabel.text = @"Tab1on";
        buttonTab2.titleLabel.text = @"Tab2";
        buttonTab3.titleLabel.text = @"Tab3";
    }

The Text of the TextViewElement changes fine, but the Title & the Background is the same like in the xib.

Upvotes: 2

Views: 1240

Answers (2)

Hermann Klecker
Hermann Klecker

Reputation: 14068

The point is that you do not change the background image in your code. What you do is you set an (foreground) image of the UIButton object. You will end up with a background image as defined in IB, a text as set programmatically and an image as set programmatically.

In principle, that should work and you would not even notice your mistake, but on a UIButton you cannot set the image and a text at the same time. A Button has either of them but not both - or at least it cannot display both at once.

Solution: Set the background image instead of the image.

Upvotes: 0

rckoenes
rckoenes

Reputation: 69469

To set background image and title on buttons you should use the setTitle:forState: methods and setBackgroundImage:forState: methods. Since button may have a different title or background depending on the state, settings the image or label directly will not work.

This should give you some idea on how to implement this in you code:

[buttonTab1 setBackgroundImage:[UIImage imageNamed:@"content_tab1_on.png"] forState:UIControlStateNormal];
[buttonTab2 setBackgroundImage:[UIImage imageNamed:@"content_tab2.png"] forState:UIControlStateNormal];
[buttonTab3 setBackgroundImage:[UIImage imageNamed:@"content_tab3.png"] forState:UIControlStateNormal];
[buttonTab1 setTitle:@"Tab1on" forState:UIControlStateNormal];
[buttonTab2 setTitle:@"Tab2" forState:UIControlStateNormal];
[buttonTab3 setTitle:@"Tab3" forState:UIControlStateNormal];

Upvotes: 1

Related Questions