I am tired of setting property values to each UILabel in the IB. Is extending the UILabel the only possible way? or there exists some other way to do it?
You could create an IBOuletCollection for the labels and set the properties programmatically (in viewDidLoad for example) using:
[self.outletCollection setValue:whatever forKey:something];
You can also call makeObjectsPerformSelector on the collection in order to send messages to each label.
Assign all the properties to one UILabel
Copy paste the label as many times you want in storyboard or xib
New labels will also get the same properties, you just require to arrange their positions
Then you can go ahead with IBOutlets and manipulate with those
This applies to all the widgets in storyboard/xib.
Also as #dehlen metioned,
If you are using codebase for UI then, Custom Button Class
Inherit UIButton and create a subclass CustomButton
In its init method, assign all the properties which you require, like font, color etc
And assign frame value at use-time, through initWithFrame method.
Or
Add button in storyboard/xib, but in its class property change class name to CustomButton
Thanks #dehlen, You are right, I forgot to mention code side implementation.
After setting the property of a UILabel make copy of it by
Press and hold alt and drag and drop UILabel to another place it will create new copy of that UILabel with same properties.
Related
I have a CustomLabel view which is a sub-class of UILabel it self.
Is there any possibility to set properties from Xcode Interface builder for this custom view. I have tried different things specified in this link:
Reuse a uiview xib in storyboard
But nothing is working for me. Will this kind of thing work ?
Sorry, i think its not possible. if you want like this, first we need to add UILabel in interface builder and take outlet connection then we can set properties.
example: self.myLabel.text="hi welcome"
I have created a custom view (Quantity View) with nib file in Swift. I have created some IBOutlets & IBActions (for buttons, labels etc.) in my custom view.
I tried to use this custom view (Quantity View) by assigning class name to a UIView in my storyboard.
It's showing me all the IBOutlets & IBActions in the Connections Inspector, as shown in this screenshot: .
I just want to show only delegate for the Custom view.
Possible Answer:
I thought I can use the -viewWithTag to get the views instead of Outlets.
But, I want to know if it's possible with having Outlets also or if there is much better way to do this?
What are the other possible ways (optimum) to handle this situation?
You can also consider the following solution:
You can take the subviews of your QuantityViews(custom view) and you can identify the specific views by its frame origin.
Note : you should know the customview subviews frame
Its not possible to hide IBOutlets from storyboard if you declare the class members as IBs (IBOutlets or IBActions).
The IBOutlets or the IBActions are just indicators to the interface builder so that it can show the names on it when you try to bind them it actually calls the setValue: forKey: method to set the view's reference to the IBOutlet property.
Now if you try to access an subview from the file's owner class without any IBoutlets you need to have a pointer to point it, so for that either you can get the reference using ObjectID which is assigned to the subview by the interface builder or you can get it using the viewWithTag: method.
The ObjectID you need to find all time when you add or replace a subview from the view, so better and convenient approach is to use tag property of UIView class.
So my conclusion to this problem is to access the views using the viewWithTag method you mentioned earlier.
I think your way is correct. But sometimes Xcode doesn't work correctly.
The following makes the IBOutlets and IBActions reappear and work properly:
Clean project your project in Xcode.
Quit Xcode completely.
Delete all contents of ~/Library/Developer/Xcode/DerivedData/.
Restart MacOS just in case.
I hope you will resolve that :)
If I create a UIButton in Storyboard and assign its Class to be a subclass I made (HighlightTappedButton), is there a way in the subclass to change the buttonType to Custom?
I know I can just change the type to Custom in storyboard, but I don't want to have to remember to do that every time.
I think it is not possible at runtime. Since it is a read-only method. I think you will get helpful questions from these links.
set UIButton's buttonType programmatically
Change UIButton type programatically
how to set UIButton type in UIButton Subclass
I have a class MyButton : UIButton which inherits from UIButton. I do a bunch of things in the initWithFrame (the only constructor)of MyButton say like setting the backgroundcolor.
Now I want to use this MyButton in my xib. so that I dont keep setting these properties again and again for all my buttons. I have also set the Custom Class to MyButton in the Identity Inspector for the button in the xib.
Nothing still reflects the properties I set in the xib. This could have been easily done if it was in the code.
My question is,
1) What gets called when you create button thru a xib (like you call initWithFrame when you programmatically create a button) ?
2) How do I get it to see the properties I set in the MyButton ? Is moving out of xib and doing it programatically the only way ?
thanks in advance !
Typically with initWithCoder:
You can use the identity inspector in Interface Builder to set values using the keypath of the attributes. In this example, you can change the CALayer properties of the view:
You can simply set it in your xib file. For that:
Select the button in the xib file.
Click the Identity Inspector button.
Type the name of your custom class(MyButton for your case) in the Class field.
Now your button will be with the Type MyButton class.
EDIT
Check Apple's documentation on Adding a Custom Object. This will give you more idea.
I have an iOS app with multiple ViewControllers. Each view has numerous IB generated UIButtons, each set to custom. I need to change the color of the background in normal and highlighted states. Further, I need to vary the colors to an RGB value based on user interaction. Thus, I can't use image files.
I found an example of a custom class derived from UIButton that implements the color change and click methods to change the colors as I desired. I created a test button and changed its IB custom class to my new class.
I have an outlet property for my IB created button.
The problem I am having is in the viewcontroller.m file when I attempt to access the custom method in my class, xcode can't see the methods.
Use IBAction as a return type for that method
Declare that particular method in .h file