Apple's UIAccesibility Protocol reference states:
UIAccessibilityLabel
Discussion
The default value for this property is nil unless the receiver is a UIKit control, in which case the value is a label derived from the control’s title.
Well... both UIView and UIViewController are in the UIKit framework, but I can't find the default accessibility label for my view who's controlled by the UIViewController named LoginVC. I tried to log it, but get a blank string. Is that right? is the doc wrong? Or am I doing something wrong?
I know I can set the accessibility label manually; I'd like to avoid the clutter and use defaults if possible - which are already generally descriptive if their name is actually derived from their controller.
By default, a UIView and a UIViewController have nothing set for their title property, so the default accessibilityLabel property (which is derived from the title property) correctly and expectedly returns nil. Set their title properties (either in code or in Interface Builder) and you'll see it when you log accessibilityLabel at runtime.
I don't think Accessibility will populate itself automatically, if that's what you're asking.
If you asking for ways to set it, you should be able to do it programmatically:
[myButton setAccessibilityLabel:#"Hello"];
If you use Interface Builder, there is a field in the Identity Inspector tab that will allow you to set this. Select your view under Objects on the left. Then click the third tab from the left for Identity Inspector. Example below:
Related
I'm currently learning how to use storyboard to configure my UI.
I know that when you drag out a object e.g. UIButton from the object library, the utitlies window on the right usually display properties/variables associated with the dragged out object.
e.g. for UIButton, I can configure properties such as buttonType (Type in storyboard) , currentTitle (Title in storyboard) either programmatically or using the storyboard column. (Note that the actual property name in the implementation classes and the corresponding name for the same property in the storyboard is sometimes different)
But for UIButton, in the "identity inspector", I have noticed a property named Restoration ID, but I can't seem to find the corresponding property in the UIButton documentation. So is there a way I can programmatically set/change the Restoration ID?
And in general, if I see a property in the storyboard utility window, what is a good search strategy to find out the corresponding property name in the actual class?
Thanks in advance for any advice!
Check superclasses, too, e.g. restorationIdentifier for UIView.
Re finding property name, you can try hitting command-shift-O (the letter "Oh", not zero) and start to type the name. Or go to symbol navigator (command-2), type in some string you want to search for (e.g. restoration), and make sure to uncheck the "show project symbols only" and "show class/protocol symbols only" options next to the search box at the bottom of the symbol navigator.
FYI, while most of the storyboard properties correspond to some property of the underlying class, but not all. IIRC, there are a few exceptions, such as the "Document" section of the "Identity Inspector" or the "Installed" checkbox on the "Attributes Inspector". There might be others, too.
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 :)
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 am using storyboard and there are 5 view controllers in it. Each ViewController has only 1 UIButton. I want to set button's background color using User Defined Runtime Attributes. So I defined a keyPath "bgColor" and set corresponding hex color code "#ffaa11". But application crashes before loading the view. It gives exception:
[<UIRoundedRectButton 0x713fdb0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key bgColor.
I dont want to create custom class. Please suggest how can I paas value through IB. Please check the snapshot.
NOTE: In real my requirement is to get a string value which has been entered in Interface Builder. Very similar to User Defined Runtime attributes. I have taken example of bg color.
Why are you using User Defined Runtime Attributes anyway? Switch to the Attributes Inspector and choose button's background from there.
First thing first.
Obviously it crashes. Because there is no bgColor property on the button. Hence when you are trying to set it as button.bgColor = #yourValue. It is crashing.
If you are using ios7 try setting the button's tintColor property. and for styles go through tintAdjustmentMode values.
And after these . If it still doesn't solve your issue. We will discuss something else :) :)
cheers. Have fun.
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