iOS Objective-C Controlling Button Images - ios

I have a button. Unsure if it is specifically a UIButton class type. This is a Button which was dragged from the Object library into my Main.Storyboard.
I don't have text, instead I have an image. And I plan to use two images. One for "loop on" and the other for "loop off". I'm playing a sound and this button controls whether or not you will loop the sound. I'm all set with understanding how to do the looping versus not.
I have set the "loop off" image as my default image under the Attributes inspector.
So when the button gets touched, I'll change the behavior of my playing sound to be "loop forever" and I'd like to change the image of the button from within my IBAction function.
I can set a title for the button in the Attributes inspector.
But I don't see that title as a resource name (auto-typing-complete) from within my ViewController.m code.
I know there are properties per state for images. I believe there's also just a "setImage" action for a UIButton.
What I can't determine is how my Button is named in the code because this button was added via dragging it out of the Object library.
Anyone have any knowledge of where these resources get named, like a reference file?
A search found something close, the following link:
http://ranga-iphone-developer.blogspot.com/2011/08/how-to-change-uibutton-image.html
However the solution doesn't appear to reference a particular button name, title, or ID; unless the text "button" is supposed to be replaced with the specific resource name.

Have you added an IBOutlet for this UIButton in your code. If not see the link

You have to create outlets inside the code for your elements added in storyboard. After that you can programatically modify the properties of the elements.
Take a look at this for IBOutlets Link on Outlets

Create a property for the button in the view controller header
#property (nonatomic, strong) IBOutlet UIButton *yourButton;
Then link the outlet in the Interface Builder (i.e. control drag from the controller to the button) and select yourButton, now you can change the title, color and images of the button by using yourButton.

Related

Create a custom UIButton with text and icon

I would like to create a custom UIButton looking like this:
The idea is simple, I want the titleLabel to start 24px from the leading, and stop 12px or more to the icon image (which is the square view at the right of the button). It can contain up to 2 lines.
The thing is that I have no idea where I should put this code in a class inheriting from UIButton.
What should go into draw(_ rect: CGRect)?
Also, should I use the titleLabel and titleEdgeInsets UIButton properties and the imageView/imageEdgeInsets as well, or instead use custom properties for this?
You just want these buttons to work, be tapped, and react somehow?
These buttons are custom buttons, and you may a create a Cocoa Touch Class file for them; name it and let it inherit from and be a subclass of UIButton.
Then connect that file to your buttons in the storyboard.
But first, you need to set/attach the newly created file to your buttons. Click on the button, In the identity inspector of your storyboard, with your button selected go to the Class field and type the name of your new file and don't forget to tap 'return' to save your changes.
Now, your buttons are attached to that custom UIButton class.
From now, you can attach all your icon images and many more as outlets to your custom UIButton Class you just created.
Most importantly, now these buttons can be used on their own; they can be outlets or trigger action methods. And to make it look just as you except, you just have to initialize your buttons, i.e. provide an icon image, text, etc.
Is what I wrote clear?
If not, I am glad to help you.

How to create IBOutlet one handed?

For a disabled person (can only use one hand), is it possible to create an IBOutlet and IBAction in iOS programming?
At the moment we use ctrl, click drag however it is impossible for disabled person to do this. Any accebility tricks for them?
Create the IBOutlet or IBAction in code first. Then in your storyboard or xib, click on the thing that owns said IBOutlet or IBAction. Now, in Xcode's menu bar, go to View->Inspectors->Show Connections Inspector. Click down inside the little circle next to the name of the outlet/action that you're trying to connect, and drag your mouse over to the view (or whatever) that will be your outlet (or if you're trying to connect an action, then drag to the button or gesture recognizer or whatever that will be performing the action, and then select the appropriate event, like touchUpInside for a button for example).
Also, I recommend turning on Sticky Keys in your macOS settings if you can only use one hand (located at Settings->Accessibility->Keyboard->Enable Sticky Keys). This will let you press modifier keys down and then use that same hand to press other keys and still perform the key combination.

iOS - change animation when user taps screen

Lets say that i have an animation - an image is going from left side of the screen to the right. I would like to make it a little bit interactive - when user taps on a screen i want to change direction of image movement. Whats the best approach to implement it?
What I do in some cases is take the main view of the View Controller, in Storyboard, and change the class type of that UIView to UIControl.
In the code that is accessed as MyViewController.view, which you can write:
var viewAsControl = myViewController.view as UIControl
In Swift or some equivalent of that.
The UIControl subclass of UIView is the hierarchical layer (class) that adds the action/target facilities to a view. For example, UIButton is a UIControl, because it generates events (actions), and it is also a UIView so it can be added as a subview.
Then from the Connections Inspector, accessed via the far right Icon of the far right panel (that is, the panel to the right of the storyboard editor window), I'd select the Touch Up Inside event type or some other event and drag it to an #IBAction tagged function I'd add to the View Controller's source code, to receive the tap event. From that tap notification, you can cancel the current animation and add a new one, etc...
Alternatively, you can create an IBOutlet for the view if you've turned it into a UIControl in IB, and use the addTarget() method to assign an action handler for a specific event, e.g. to make it call a function in your code.
Either way the effect will be that any time the view is tapped, it will generate the event for you to respond to

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;

Creating a custom toggle with UIButton in Interface Builder

Is this possible? It seems shoddy for it not to be.
I'm trying to create a toggle button (not a UISwitch) in IB. For example a mute button, when muted would have some indication that sound is disabled, when you hit it, that indicator disappears (but still the same underlying graphic), and toggles between these states each time it is pressed.
This functionality can be achieved using the selected property, however you can't change the Selected AND Highlighted property in IB like you can in code, so whenever the button is pressed, no matter the state, the highlighted image is the same, which looks terrible and glitchy.
Is there a way to fix this with just IB, or do I have to make a custom class to avoid manually loading in all these buttons?
Have you thought about subclassing UIButton to add in the things you need?

Resources