0

I am having some trouble with the UIButton.

This is my code:

enter image description here

I am not using the Interface Builder and I am a new programmer.

What I want is that when the button is selected the title changes and the button's transparency changes from half visible to fully visible.

Thanks in advance, -Marnix

Marnix
  • 79
  • 2
  • 10
  • Where is that code? What exactly isn't working, and how do you know? What have you tried? – Caleb Aug 11 '12 at 22:59
  • 1. It's the .m file. 2. It show the first 'if' statement effect, not the @"UnTapped" one. – Marnix Aug 11 '12 at 23:01
  • What do you want to do exactly? – Selkie Aug 11 '12 at 23:02
  • I want the button to be half transparent when selected (alpha: 0.5), and fully visible when not selected – Marnix Aug 11 '12 at 23:04
  • This code is inside the `IBAction` of the button? –  Aug 11 '12 at 23:05
  • As I stated before; I do no use the Interface Builder, so I do not need IBAction. – Marnix Aug 11 '12 at 23:11
  • Possible duplicate of http://stackoverflow.com/questions/5843427/how-do-you-add-an-action-to-a-button-programmatically-in-xcode – dans3itz Aug 11 '12 at 23:15
  • @Marnix, without `IBAction`, you'll never reach your goal. no matter, you are in the _Interface Builder_ or you create the `UIButton` dynamically, the button's behaviour is still same. – holex Aug 11 '12 at 23:15

2 Answers2

1

The problem is that the if statement is just executed once, when you're creating the button. Inside MyCode2, add this line:

[Button1 addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchDown];


Every time that the button's pressed, the buttonAction method will be executed, so:

- (IBAction)buttonAction:(id)sender {
    if(Button1.selected == NO) {
        // Do your "NO" stuff
    }else if(Button1.selected == YES) {
        // Do your "YES" stuff
    }
}

You have to declare this IBAction in your .h and add this method in your .m file.

  • Actually, you may not need to declare it in `.h`, since it will not be used by other classed anyway. – Selkie Aug 11 '12 at 23:26
  • That's true, @Selkie. I'm that kind of developer that likes everything really organized... :-P –  Aug 12 '12 at 00:10
1

First of all, you should set title for different control state like

[Button1 setTitle:@"UnTapped" forState:UIControlStateSelected];

Second, you don't need to put setTitle method inside of this check.

Besides, you need to put this check inside of a method of the button to make it work. If you didn't use IB, then just use addTarget: action: forControlEvent like

[Button1 addTarget:self actoin:@selector(changeButtonState:) forControlEvent:UIControlEventTouchUpInside];

Lastly, did you make the button keep selected after touch? If not, add

Button1.selected = !Button1.selected

in the method. All in all, your method should looks like this

- (IBAction)buttonAction:(id)sender {
    Button1.selected = !Button1.selected
    if(Button1.selected == NO) {
        Button1.alpha = 0.5
    }else if(Button1.selected == YES) {
        Button1.alpha = 1.0
    }
}
Selkie
  • 1,773
  • 1
  • 14
  • 21
  • I would also put your answer as 'the answer' if I could set multiple answers. Thanks anyway. – Marnix Aug 11 '12 at 23:27