UIButton setTitle doesn't change title immediately - ios

I'm having trouble by writing and reading the title of UIButton.
My problem:
// If I execute this line of code everything works fine, the title gets changed in the UI
[self.lastButtonPressed setTitle:stringDate forState:UIControlStateNormal];
// In the next line of code I want to read the title I've set before, but everything I get is the title of the UIButton before I changed the title.
NSString *myText = [self.lastButtonPressed.titleLabel.text];
Is it possible that [UIButton setTitle] gets called asynchronous? If yes, is there a possibility to make this call synchronous?

You probably want currentTitle not titleLabel.text. Or perhaps titleForState:UIControlStateNormal.

Related

iOS Button background image not working. (UIButton backgroundImage) [duplicate]

This question already has an answer here:
UIButton image behavior changed in iOS 15?
(1 answer)
Closed last month.
(A) Following line change the image in my imageview.
[(UIImageView *)[cell viewWithTag:400] setImage:[UIImage imageNamed:#"sellsign"]];
(B) Following code change only Title of button. [ I have checked for both UIButton type "System" and "Custom" background image line not working.]
[(UIButton *)[cell viewWithTag:500] setBackgroundImage:[UIImage imageNamed:#"sellsign"] forState:UIControlStateNormal];
[(UIButton *)[cell viewWithTag:500] setTitle:#"1" forState:UIControlStateNormal];
(C) Button Configuration
Button tittle gets change to 1 but there is no change in background image.
Fact 1: I am referring correct button as button title change to 1
Fact 2: Button state is also normal as I can see button title 1
Fact 3: I am referring correct image name as it set in Imageview
Any idea that why only UIButton Background image has this strange behaviour. I think I am missing something in latest iOS.
How can I set my desire image in UIButton as a background image?
Note:
Some one close the question by saying it's duplicate and provided following link,
UIButton image behavior changed in iOS 15?
This is not the solution. It says change the button style. It's patch to achieve output. On that link it's mention that it's old way and one need to adopt new way. Apple introduce new way so there must be solution for it as well.
You need to set the Background Configuration for Background field as Custom also.

Trying to make my image's position change randomly

I am using Xcode to try to change my images position randomly every time a button is pushed but for some reason, the image only moves once in a while when the button is pushed even though I know it received the button's IBAction because the NSLog in the IBAction displayed in the console, I have tried it two ways and both methods only worked sporadically
Method 1:
ranX = arc4random() %320;
ranY = arc4random() %480;
myImage.center = CGPointMake(ranX, ranY);
Method 2:
myImage.center = CGPointMake(arc4random() %320, arc4random() %480);
One more thing: every time it does work, the next IBAction to run puts it right back into its original position.
More information that might be pertinent I don't know, I do not have size classes enabled, it is triggered by touch up inside. I also DID make all the RNGs arc4random_uniform.
The problem is fixed, just deselect auto layout.
What control event did you add to your button? Since you stated it moves every once in a while, you might have done the wrong event. Try UIControlEventTouchUpInside
So,
[myButton addTarget:self action:#selector(moveImage) forControlEvents:UIControlEventTouchUpInside];
You're using a storyboard I see, Make sure that the send event for the button is "Touch Up Inside". Click on the button, go to the Connections Inspector and double check this.

UIButton Programmatically get Default Highlighted Style

I searched for this problem for a while and couldn't find anything about it, but eventually figured out my mistake, so I figured I'd post it here to help anyone with the same situation.
When creating UIButtons in Interface Builder, it automatically styles them for the highlight state (If you press and hold the button, it looks dim/lighter in whatever color you set it as); however, I was trying to create a button programmatically (just a normal text button) and wasn't achieving this result. If I pressed and held the button, the appearance was unchanged, although the button still worked.
The issue was that I was initializing the button with
UIButton *myButton = [[UIButton alloc]init];
instead of
UIButton *myButton = [UIButton buttonWithType: UIButtonTypeSystem];
Which is apparently the best-practice way of initializing a UIButton.

Adding a custom UIButton and action to a view in window app delegate (ios)

I am working on a tab bar application which needs a global title bar with a setttings button attached. I have achieved adding an imageview to the main window, then a label, another image, and finally a button. However, I cannot get this button to fire an action. it is set up like this:
[settingsButton addTarget:self action:#selector(settings) forControlEvents:UIControlEventTouchupInside];
Then the action defined like:
-(void)settings{
NSLog(#"please do something");
}
However nothing happens, the image doesn't even change like it is being pressed. Can i not define buttons in the app delegate this way? is it because the target is not something that is control based? i tried different targets but i must not fully understand what setting a target is. thanks for any help.
P.S.- i tried setting up the method like so as well:
-(void) settings:(id)sender{
}
and calling it by replacing the action with #selector(settings:) to no avail. Thank you.
I solved it. I forgot I changed the view behind the button to a UIImageView. I can't believe I didn't think of this before since this same issue cost me 2 hours the other day. When you have an imageView always remember to set this flag if you want stuff on it:
(someImageView).userInteractionEnabled = YES;
Hopefully I save someone an hour or two of needless headaches.
P.S. Thanks for the speedy comment !

How to set the Button text programmatically in iOS?

I know how to change the button text programmatically in iOS:
[myButton setTitle:#"myTitle" forState:UIControlStateNormal];
but my need is I have a label called _lblgenisis, I put some code to change the text of this label and it works fine for me, the code for that is:
_lblGenisis.text = [NSString stringWithFormat:#"%# %#",delegate.selectedBook,delegate.selectedChapter];
my requirement is I have to convert to this code to show the same in button instead of label my button name is _btnGenisis. How to do this?
Solution
Answer that works for me:
NSString *myTitle = [NSString stringWithFormat:#"%# %#",delegate.selectedBook,delegate.selectedChapter];
[nextButton setTitle:myTitle forState:UIControlStateNormal];
Try to add the label to the button?
[_btnGenisis addSubview:_lblGenisis]
Let me see if I get this right.
The code that normally works for changing your button label is not working to set it programmatically?
I have had this issue it turns out that:
When you set the label in the xib file you can see the results and won't make any mistakes. Recently I had a custom button with an image and couldn't figure out why I could not add a title programmatically.
It turns out I was setting the title properly (the way you already know how to). The real issue was that I set my button's image with setImage:forState. This image is above the button and you should check your xib file to see if you have an image set for the button. If so, delete the image and set it as the background image.
To see if this is actually the issue, try this in your code where you are trying to set the title
NSLog(#"image for button %#", _btnGenisis.currentImage);
It should output (null)
If it outputs like you have an image covering your button's title.
Check the interface file and inspect the button. Make sure there is no default image set. You can set a default background image.
If it is all created in code do a search for
[_btnGenisis setImage:
You should not be setting the image if you want a title. Use
[_btnGenesis setBackgroundImage:yourTitleHere forState:UIControlStateNormal];

Resources