Reputation: 79
I am having some trouble with the UIButton.
This is my code:
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
Upvotes: 0
Views: 1235
Reputation: 1783
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
}
}
Upvotes: 1
Reputation:
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.
Upvotes: 1