why cant I change button title in ios - ios

I am new to ios, and trying to change a simple button text from implementation side in an action.
I have tried two of these: while one of them works, the other one doesnt !
self.Btn_Analyse.titleLabel.text = #"Stop Anlaysing"; // didnt work
And...
[self.Btn_Analyse setTitle:#"Deneme" forState:UIControlStateNormal]; //worked
why doesnt the first one work?
PS: I have connected the button as IBOutlet

Why are you telling us what worked?
If it worked then there isn't a problem.
Anyway, the reason the first one didn't work is because a button is not just a label. It has various states and each state can have different properties.
You set the image for the state. You set the title for the state. etc...
Then with different states you can have different properties.

UIButton sets the title on its own, so if you directly change the text in label, button changes it back.
That's because button can have different titles for different states (label can't do that), and sets the appropriate one automatcally.

Related

Highlight UIButton when not selecting the button itself

I have a custom class here that consists of a UIButton inside of a UIView.
The goal of this class is to allow users to have a greater area (being the UIView) to select the button than just the buttons frame itself.
Now when a user taps on the view I want the buttons highlighted image to show... But the problem is, it does not.
I've read many possible solutions to this issue For Example:
Calling: [btnObject sendActionsForControlEvents:UIControlEventTouchUpInside]
This however did not change the buttons highlight.
I also tried just settings the button.highlighted = YES;
But that did not work either.
I have the images for different states properly setup (Normal and Highlighted), I'm sure of that.
I also have the gestureRecognizer working properly as the functionality is great except for the lack of highlight.
Does anybody know if I'm missing any thing special that needs to be done in order to pull off this seemingly very simple task? Surely it's been done many times.
Thank you
You were on the right track. -[UIButton setHighlighted:] is just a flag. What you need to do is call setNeedsDisplay on that button right after you change the highlighted property.
I solved my problem a little while ago and I'm not sure if Kevin Low's answer would've worked too, it very well might have.
But for some reason, a UITapGesture doesn't work well with highlighting buttons as a view transitions (That might be because I didn't call setNeedsDisplay). The gesture that ended up working was the UILongPressGesture with a 0.0 sec for minimum duration.

Swift UIButton - How to remove underline?

I have problem when in iOS settings is enabled this setting "Button Shapes"
It causing this underline in application (first picture with enabled setting, second without)
Any idea how to programatically or in storyboard disable it?
I tried attributed text but I get same result :(
I'm newbie in Swift.
Thanks for help!
It's not a problem. You should not make any attempt to counter any accessibility changes set by the user. They are there for a reason.
This is an answer by user4291543 from this question Remove underline on UIButton in iOS 7
[yourBtnHere setBackgroundImage:[[UIImage alloc] init] forState:UIControlStateNormal];
I found this answer works with SWFrameButton
And for all the others saying "Don't Do This", SWFrameButton is a very good example of when you would want to do this. I also think the OP's situation is a perfectly valid scenario as well...
I totally agree with #maddy's comment:
It's not a problem. You should not make any attempt to counter any accessibility changes set by the user. They are there for a reason.
But I did stumble on a way to accomplish the task at hand...
In addition to a UIButton, you'll also need to make a .png file that contains nothing (meaning the entire contents have an opacity of 0%). Go ahead and load that into your xcode project's assets.
Now go ahead and set the Button's Background to that image you just provided. (In my case, I called it clear) This will remove the underline from the button's text. However, now you can't see the boundaries of the button. This can be solved by changing the Background of the button's View. Go ahead and select any color for the View's Background property and now the background of the View visibly defines the button's boundaries. You're able to see this because your clear.png has an opacity of 0%.
see the Attributes inspector for UIButton here.
Rather than trying to defeat the underline by going to make a label perform some action via UITapGestureRecognizer, this allows you to still use a UIButton. Keeping inline with accessibility features to mark buttons for people that want to do that.
You could create a custom button class with a label (with clear color). If you set the text of this label instead it shouldn`t get an underline.
Are you sure you want to do that?
Apple added an accessibility feature to mark buttons for people that want to do that. Apple will probably reject your app because it defeats a system function meant to help the disabled.
I found the solution. All you have to do is set a picture as the background of the button. just pick a picture with the same color as the button you created.

clickable labels or writable buttons

I'm trying to make a multiple choice quiz application for iPhone. The text is displayed in a label, as are the different answer choices, however, I couldn't find a way to make labels clickable to let the application know which answer choice was selected. I then thought I might be able to use buttons for the answer choices by setting the button title to be each answer choice, however when I do that it says assignment to read only property
self.answerChoice1.titleLabel = answerChoice1;
Is there a way to make either of these alternatives (clickable labels, or writable buttons) work, if not how would I accomplish what I am trying to do?
You can use buttons but your attempt to set the label isn't valid.
Use:
[myButton setTitle:#"Some Label" forState:UIControlStateNormal];
Please see the docs for UIButton.
If you want to use tappable labels, you need to enable user interaction on the label and then apply a UITapGestureRecognizer to the label.

How do I link my button to the selected state?

Now, I have sort of asked this question already, but this time it's a little different. Since my last question, I have received several answers and excellent advice, and I did some extensive research as well. I am using xcode, and coding for iOS 6.
My question specifically is how do I keep my button in a temporary "selected" state when pressed? Like I want it to stay pressed through the duration of the sound and then switch back to the default image.
My button is mostly linked up through interface builder, because it's easy and less code to write. I have the sound coded in my button's IBAction to play when pressed and I have the second image that it changes to when pressed, however the Image isn't set in my code it is set in Interface Builder.
I've seen several code that seems like it would work wonders if I knew how to correctly input it. Which leads me to a second part of my question.
Can someone help me set it up?
for example:
[btnClear setBackgroundImage:[UIImage imageNamed:#"blue_button.png"]
forState:UIControlStateHighlighted];
This seems like it would do the trick in my head. I just don't know which variable of mine to replace "btnClear" with and setBackgroundImage isn't showing up as a command.
On my other question I received and answer in the form of a comment that made the most sense but they didn't elaborate, it was:
"you can set image for Selected state of your button, and on touchUpInside set property selected = YES.
So I have the image I want in the selected state, and my button is linked for touchUpInside, but the only "property" or action it has is to play the sound, and because my button's images are set in Interface Builder I'm not really sure how I can set selected = YES without coding a new class which sets up the image.
Now if that's what I have to do, that's fine, but I need some guidance setting that up I am highly familiar with Objective-C it's just I'm new to these default classes in the iOS SDK I'm still getting a hold of things and having to refer to tutorials every now and again.
Here's a link to my other question if it helps:
How do I make a button stay pressed?
Again, I already have everything set up regarding the Image in Interface Builder, I'm just not sure how to fit the selected state option it into my already existing button action.
Thank you!
I think what you may be looking for is a button property:
#property (nonatomic, weak) IBOutlet UIButton *button;
(Or with a more descriptive name!) Hook this up to your button in Interface Builder. Then when you want to switch to the selected image (which you said is already in IB, right?), simply do this:
self.button.selected = YES;
And to turn back to the default image:
self.button.selected = NO;

Make green color UIActionSheet button?

I know I can access a individual button on a actionSheet using a for loop to access the particular button in the action sheet I want, but the thing is, how would I make a green button?
I know each button acts almost identically to a UIButton so how should I go upon making one of my buttons in the action sheet green?
I just need some tips or help with this as it is the last part of my app that isn't done!
Thanks!
When I have needed customizations for UIActionSheets, sometimes I find methods to make it happen simply, but more often I end up having to set up a custom view on way or another. I've added all manners of custom controls with:
[sheet addSubview: myCustomActionSheetController.view];
You might need to set the size as well.

Resources