UIButton titleLabel NOT changing - ios

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];

Related

UICollection with Checkbox

Hi i am working with camera app , captured picture are shown in thumbnail-view using collection view ,i have used checkbox to select the particular picture,Example, i taken three picture , i just tapping on 1st and 2nd picture to select it will get selected ,then i am taking another two picture ,now the checkbox image moved to the latest picture ,i don't know why it happens ,
i have done this in collection view cell class , and i have created a Boolean property in same class it itself.
- (IBAction)checkButton:(id)sender {
if(!checked)
{
[self.checkBoxButton setImage:[UIImage imageNamed:#"Checkbox_checked.png"] forState:UIControlStateNormal];
checked = YES;
}
else if (checked)
{
[self.checkBoxButton setImage:[UIImage imageNamed:#"checkbox_unchecked.gif"] forState:UIControlStateNormal];
checked = NO;
}}
please help me to do this ,
If checked is declared as static, then there is only one of them which is shares by every instance of your class. You should use a property instead.

UIButton selected text not showing for selected state

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

Changing button image when another button is clicked

I'm facing a problem in xcode, in one of my view controllers i have two buttons, when one of them is clicked (appleBtn) the second button's (topMenu1Btn) image should change
I created an outlet for both buttons, an action for the applebtn when clicked
- (IBAction)HandleBtnClick:(id)sender {
self.topMenu1Btn.imageView = [UIImage imageNamed:#"Apple.jpg"]
}
the thing is the imageview is read-only
is there anyway to do this?
- (IBAction)HandleBtnClick:(id)sender
{
[self.topMenu1Btn setBackgroundImage:[UIImage imageNamed:#"Apple.jpg"] forState:UIControlStateNormal];
}
You could try this to set the image for the button.
[self.topMenu1Bt setImage:[UIImage imageNamed:#"Apple.jpg"] forState:UIControlStateNormal]
If you want the background image use setBackgroundImage: instead
[self.topMenu1Btn setBackgroundImage:[UIImage imageNamed:#"Apple.jpg"] forState:UIControlStateNormal];
Or
[self.topMenu1Bt setImage:[UIImage imageNamed:#"Apple.jpg"] forState:UIControlStateNormal]
If you use the second method then make sure that the frame of image matches with the button
Make sure that Button type is Custom
(IBAction)HandleBtnClick:(id)sender {
self.topMenu1Btn.imageView.image = [UIImage imageNamed:#"Apple.jpg"]
}
You can't modify the imageview, but you can modify the image in it ;)

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