UIButton selected text not showing for selected state - ios

All,
I have a UI element that contains a button. I set the text on the button, and simultaneously, the selected state. See below:
- (void)setText:(NSString *)text {
self.inputButton.selected = text != nil; // select the buttons if the text is not nil
self.cancelButton.selected = text != nil;
[self.inputButton setTitle:text forState:UIControlStateSelected];
[self.inputButton setTitle:text forState:UIControlStateHighlighted];
}
this is working properly when filled out in the view controller.
However, when I passed the text in from the previous view controller, the selected state text doesn't display. The cancelButton selected state works properly, and the inputButton state is supposedly selected, however, selected state text isn't displaying.
I don't know if it's because the button isn't redrawing or something? I've tried all the [self layoutIfNeed] and [self setNeedsDisplay] with no luck. Anyone know what's going on?

I figured out the issue. I was using both the selected and highlighted states. For some funky reason, that was creating a situation where the button state could be 5. I resolved the problem by not addressing the UIControlStateHighlighted in any way.

Can you check adding following line of code.
- (void)setText:(NSString *)text {
self.inputButton.selected = text != nil; // select the buttons if the text is not nil
self.cancelButton.selected = text != nil;
[self.inputButton setTitle:text forState:UIControlStateSelected];
//Start:Added line of Code
[self.inputButton setTitle:text forState:UIControlStateNormal];
//End:
[self.inputButton setTitle:text forState:UIControlStateHighlighted];
}
Thanks & Regards,
Amit

Related

when i click a button in ios app i want the image to change

the title much explains it really in my button
- (IBAction)LikeBtn:(id)sender
when the button is clicked i want ti to swap images from a small grey icon to a green icon
im trying it this way but i cant seem to get it to work
- (IBAction)LikeBtn:(id)sender {
UIButton *senderButton = (UIButton *)sender;
[senderButton setImage :#"liked.png" forState:UIControlStateSelected];
{
Has Any body any ideas how this can be done at the moment the image is selected in the xcode control panel and is named likebtn.png and need it changing to liked.png after clicked
Assuming you have the image properly added to your project, and it's a png:
- (IBAction)LikeBtn:(id)sender {
UIButton *senderButton = (UIButton *)sender;
[senderButton setImage:[UIImage imageNamed:#"liked"] forState:UIControlStateSelected];
}
PS: Are you sure you wish to set the image for the selected state? If not, use UIControlStateNormal instead.
I think you need to setImage in viewDidLoad when creating button:
[LikeBtn setImage :[UIImage imageNamed:#"liked.png"] forState:UIControlStateSelected];
In addition to what Maddy says about passing a UIImage rather than a string for a selected state, you will also need to declare that the button is now selected. If you change your code to this, it should work for you
-(IBAction)LikeBtn:(id)sender {
UIButton *senderButton = (UIButton *)sender;
[senderButton setImage:[UIImage imageNamed:#"liked.png"] forState:UIControlStateSelected];
senderButton.selected = YES;
}
You can also put a if (senderButton.selected) condition within the IBAction method to detect and toggle between button states, changing the state from YES to NO.
Hope this helps

UIButton titleLabel NOT changing

I have two View Controllers : "A" (main View Controller) & "B" (second View Controller). In VC "A", there is a button who's title I would like to set in VC "B" via a Table View. When the user selects a row from the Table View, the button's title should be set to the selected row. After much editing and testing I think I have isolated the problem to when I set the button's label in VC "A", following the selection of the Table View. Here is the code from where I think the problem is (View Controller "A"):
//firstParameterString = the button label's string which I set in VC "B"
- (void)setButtonLabels {
NSLog(#"%#", firstParameterString);
if (self.firstParameterString == nil) {
[self.firstParameter setTitle:#"M" forState:UIControlStateNormal];
}
else {
[self.firstParameter setTitle:firstParameterString forState:UIControlStateNormal];
}
NSLog(#"%#", [self.firstParameter titleForState:UIControlStateNormal]);
}
When I run the code in Simulator, the first NSLog returns the correct value, however the second NSLog returns a value of "(null)". I don't understand why this isn't working...I had it up and running in another project I was working on. Any help or comments would be much appreciated!
Thanks!
It's because the titleLabel isn't what you are setting, so it is null (hasn't been created yet).
Also, you are setting the title of firstParameter, but trying to check secondParameter.
You want to instead check:
[self.firstParameter titleForState:UIControlStateNormal];
Would your try
[self.firstParameter setTitle:self.firstParameterString forState:UIControlStateNormal];
[self.firstParameter setTitle:self.firstParameterString forState:UIControlStateHighlighted];
[self.firstParameter setTitle:self.firstParameterString forState:UIControlStateDisabled];
[self.firstParameter setTitle:self.firstParameterString forState:UIControlStateSelected];

UIButton and selected highlight state

The UIButton has a normal/default, highlighted, and selected image. I then have a IBAction method that is called on Touch Down. The method changes the highlighted image depending if it's selected. But when the button is selected, the method is called and so the highlight image is changed, however what is displayed is the normal/default image with a tint. I have tested that image used is in not nil. What happens is when the UIButton in a selected state is pressed displays the normal state with a tint. Why is it not using the highlight image and is there another way of showing a selected highlight image?
Why do you set the highlighted state in the IBAction method? You only need to set the highlighted image for you button when you create it. It will switch automatically. Adding a tint when selected is the default behavior of 'selection' when no highlighted image is assigned.
if your using Interface Builder, just assign the highlighted image there.
Im assuming your looking for normal button selection behavior with your IBAction method set to the touchUpInside event.
I got around this problem by using the selected state and notifications (not via the UI). When a notification is called I change image for the default state, change the selected state and then change the image for selected state.
Update:
I came up with a much better idea from another question that was doing something similar to me. The way to do it is to things in setSelected
- (void)setSelected:(BOOL)selected
{
if ( selected )
{
[self setImage:[CFCHStyleSheet imageForTickButtonChecked] forState:UIControlStateNormal];
[self setImage:[CFCHStyleSheet imageForTickButtonChecked] forState:UIControlStateSelected];
[self setImage:[CFCHStyleSheet imageForTickButtonCheckedDisabled] forState:UIControlStateDisabled];
}
else
{
[self setImage:[CFCHStyleSheet imageForTickButtonUnchecked] forState:UIControlStateNormal];
[self setImage:[CFCHStyleSheet imageForTickButtonUnchecked] forState:UIControlStateSelected];
[self setImage:[CFCHStyleSheet imageForTickButtonUncheckedDisabled] forState:UIControlStateDisabled];
}
[super setSelected:selected];
}

Setting an UIImage for UIControlStateHighlighted

board[i] is an array of UIButtons that I have created programmmatically, and I can't change their image for UIControlStateHighlighted:
[board[i] setImage:[UIImage imageNamed:#"block"] forState:UIControlStateNormal];
[board[i] setImage:[UIImage imageNamed:#"blockPressed"] forState:UIControlStateHighlighted];
When I press the button with the mouse in the simulator the image doesn't change. I think this is a very noob question, but I don't what the code doesn't work.
when adding button programatically do this:
add target of each same.
provide tag all button from 0 to count.
set UserInteraction to true
setBackgroundImage:[UIImage imageNamed:#"blockPressed.png"] forState:UIControlStateHighlighted if u want button to be highlited
Now button is pressed same method is called for all button: For example
-(void)ButtonTouched:(id)sender
{
UIButton *btntouched = sender;
NSLog(#"%#", btntouched);
[btntouched setBackgroundImage:[UIImage imageNamed:#"blockPressed.png"] forState:UIControlStateHighlighted];// it can be forState:UIControlStateNormal also
}
I don't think you are triggering the highlighted state. This could be because they are not set to have interaction enabled. Or there is something else missing from the way you set up your buttons.
The other thing you can try is to add a selector to each of the buttons for when they are touched, and then change the image by referencing sender for the selector function.
Assuming you've made sure your image doesn't return nil, this code should work:
[myUIButton setImage:[UIImage imageNamed:#"myHighlightedButtonImage.png"] forState:UIControlStateHighlighted];
It should work find if you call that line in your viewDidLoad().

Changing Image of Button When the button is selected In Iphone

I have two buttons on my first view,so when I click on one of the button the view changes,so I have two images one for the default state and one for the selected state,
first i tried with xib,goin into the properties and changing the states and then selecting the proper images and when I build and run my code,On cliking the image doesnt change..
So I did this way through code
- (IBAction) handleButton:(id)sender
{
UIButton *button = (UIButton*)sender;
int tag = [button tag];
switch (tag)
{
case BUTTON_1:
if ([m_Button1 isSelected])
{
[m_Button2 setImage:[UIImage imageNamed:#"image.png"] forState:UIControlStateNormal];
[m_Button1 setSelected:NO];
}
else
{
[m_Button1 setImage:[UIImage imageNamed:#"image_pressed.png"] forState:UIControlStateSelected];
[m_Button1 setSelected:YES];
}
[self displaymethod1];
break;
case BUTTON_2:
[self displaymethod2];
break;
default:
break;
}
}
here the image changes when i click on it and i go to diffrent view..when i again come back to my first view,the button is still in selected mode..so How shall i fix this..
Waiting for your reply
I think is a bit simpler through IB.
When you add a regular Round Rect Button in IB you can modify its behavior by going to the Button section in the Attributes Inspector panel.
First select the images you want for it's default state by leaving the State Config in Default. There is an image and a background image properties for this. Once that is set, you can change the State Config to Highlighted and select the image you want to show when the button is highlighted.
NOTE: This is for xcode4.
when your view will appear then you have to initiate all the variable and UI property
in view will appear method. (not necessary in all cases solution is only for your requirement)
There you can set your button default state and image.
-(void)viewWillAppear:(BOOL)animated
{
[m_Button2 setImage:[UIImage imageNamed:#"image.png"] forState:UIControlStateNormal];
[m_Button1 setSelected:NO];
...
}

Resources