Change UIButton type programmatically - objective-c

I have an UIButton made with IB, it is set to "Rounded Rect". At one point, I'd like to change the type to "Custom" in the code, is that possible ? I saw the type can be set at creation, but did not see if it was possible to change it later on.

If I'm not mistaken, different button types are represented by different (private) subclasses of UIButton. That's why you can only set type at creation time but not after.

I don't know if I'll be much help, but for me it worked by doing:
button = [UIButton buttonWithType:(UIButtonType)];
example:
calculateButton = [UIButton buttonWithType: UIButtonTypeRoundedRect];

Related

Background property of UIButton

I can see that there is a Background property in the Attributes Inspector of UIButton but how do I set it programmatically?
It is not background color or background image because I have tried setting these in code but it doesn't have the desired effect.
I used setBackgroundImage of UIButton
First, be sure to have a referencing outlet for your button then,
try, button.backgroundColor = UIColor.black of course the 'button' name may be diffrent for you and the color of choice may also be.
If this doesn't work for you for some reason this video may be of help: https://www.youtube.com/watch?v=9Vq_xckdJkQ

iOS Objective C - A pointer to Interface Control on viewDidLoad

i am new into ObjC, unfortunately..
I have created a list and Tool bar at the bottom, with two buttons.
The first button does the list refresh,
the second one should be 'enabled' when you tap on a list item and when clicked it will perform additional work. This requires me to disable this button on start, but enable on didSelectRowAtIndexPath.
I want to grab a pointer to the second button on viewDidLoad method for later purposes.
Something like that would be great:
UIBarButtonItem* m_pButtonA = (UIBarButtonItem*) some_magic_function_to_call;
UIBarButtonItem* m_pButtonB = (UIBarButtonItem*) some_magic_function_to_call;
So i can later call the control method when required.
[m_pButtonRefresh method...]
Is this possible ? Thanks for any input.
In your custom view controller class, create an IBOutlet property, like this:
#property (nonatomic, weak) IBOutlet UIBarButtonItem* m_pButtonA;
#property (nonatomic, weak) IBOutlet UIBarButtonItem* m_pButtonB;
(I'd suggest using more descriptive names)
Then, in Interface Builder you can set those properties to be the Reference Outlet for each of those views (drag from the little (+) to the view control in the left sidebar in IB, and select the appropriate property).
You'll then be able to access those items using code along the lines of self.m_pButtonA. You don't need to manually create the reference to them, as the code will automatically generate them.
This is somewhat similar to the approach you would use when connecting GUI events to methods in the view controller (those would use methods labeled with type IBAction), that are triggered automatically when those events are raised. Both IBOutlet and IBAction are just little hints for Interface Builder (IBAction is actually just another name for void).
Sounds like you're coming from an Android background? You should read about outlets, the equivalent to what you're looking for.
See Apple's cheat sheet, Cocoa Application Competencies for iOS, which links to more thorough documentation if you need it.
Ok, i have found the answer here
Get UIButton reference from viewDidLoad
Long story short:
Just give tag to your button and access your button with tag value.
UIButton *btn = (UIButton*)[self.view viewWithTag:1];
[btn setHidden:YES];

Initializing IB Controls Programmatically in iOS

I have a simple app and I want to load initial settings for the UI elements. But I don't want to have to IBOutlet each one of them. Is there a way to address them programmatically and set their values. for example:
if (user hasn't created a default){
// load the default settings
button1 = 12;
label.text = text;
etc...
}
The entire purpose of IBOutlet is so that you can set and get their values programmatically, so I don't quite understand why you wouldn't use IBOutlet. Usually when you create in Interface Builder you use IBOutlets for anything you need to interact with programmatically. (Obviously, when create them programmatically, you don't need an IBOutlet.)
But if you really want to add the controls in Interface Builder and not use IBOutlets, you could set the numeric tag property with some unique value in the attributes inspector under the "view" settings. You can then programmatically retrieve the reference for that from your view controller as [self.view viewWithTag:tagNumber];. This is more awkward and less efficient, so I'd really encourage you to use IBOutlet. Is there some compelling reason not to?
For UIButtons is
[button addTarget:self action:#selector(signatureOfYourMethod) forControlEvents:UIControlEventTouchUpInside];

Use addTarget:action:forControlEvents: method to change a UIImage?

I am intending to add a "blank" star icon on my page which will be changed to a "solid" star when a user clicks on it.
I tried to set the UIImage as shown below:
UIImage *hotIcon = [UIImage imageNamed:#"blank_star.png"];
Can anyone advise me how I can use the addTarget:action:forControlEvents: method? Can I even use this method on a UIImage? Or must I definitely use a UIButton?
You can add a method to action against a button like following:
[aButton addTarget:self action:#selector(myMethod:) forControlEvents:UIControlEventTouchUpInside];
"addTarget: action:" and "removeTarget: action:" is for all UIControls (like UIButton). There is no way you can use it for UIImages directly.
There are multiple ways to achieve your requirement. (declare a custom UIControl and implement the way you want). But simplest approach is to use UIButton with required images (and background images).

Add a second image to a custom UIButton

I've got an Custom UIButton. It's got
a "static" background image
a variable text (the Title) which gets set in the code
Now I would like to add an icon (UIImage/UIImageView) inside the button on the left of the text. (I use the indent to move the text slightly to the right). Is there an easy way of adding that icon (and referencing it from code, so I can change it) or would you recommend creating a completely new button e.g. based on a UIView? (e.g. a view, that responds to touches)?
I'm just trying to get a feel for what the best approach would be for this. Any experience?
Two ways:
I prefer doing this by subclassing a UIView and (as you mention) implement the UITouch-responder methods. You're also able to override drawRect: what I really like because it makes your app soooooo much faster!
Since UIButton is a subclass of UIView, you can add a UIImageView to your button by using addSubview:. If you save it in your main-class (#property (...) UIButton *button) you can always access it by calling [self button]. You can also subclass your UIButton and add this #property. That's up to you.
It's totally up to you. However I prefer the first way!