问题
I'm contemplating writing some helper functions to make it easier to do simple changes to the UI elements in my iPhone NIB. Mainly - I want to access a UILabel, or other element, via its Name in Interface Builder. Is this possible? Is there a smarter approach?
Example
Say I want to make a super simple iPhone app that displays 'Hello World'. I start a new project and then open the NIB and drag a UILabel into my view and give it a 'Name' of 'LblMain'. Now, presuming that I've included my handy helper function, I'd like to assign the label some new text something like this:
[helper setText:@"Hello World" forLabel:@"LblMain"];
-or-
UILabel *ObjTmp = [helper getUILabel:@"LblMain"];
ObjTemp.text = @"Hello World";
Now - the trick is that I didn't add a:
IBoutlet UILabel *ObjLblMain;
anywhere in .h file - I'm just accessing that label dynamically - wouldn't that be nice?!
Now, for simple apps, to add some more labels or images, I could drag them into my NIB, assign them names in that element's inspector window, and then immediately access them inside code without the stuttering & hassle of adding them in the .h file.
Motivation
- Basically, I'm frustrated that I have to wire every element in my NIB - it's a lot of stuttering and bookkeeping that I'd rather avoid.
- I could give a design some naming conventions, and they could generate a NIB without needing to be intimate with the implementation.
回答1:
You can definitely load the NIB programmatically, find all the objects and query them to work out what points to what. Just look at Loading Nib Files Programmatically. But the problem is that the Interface Builder Identity Name isn't exposed outside of IB. So I'm not sure what you would use as the "forLabel" parameter. The "Name" field is just a convenience for the IB document window. It's not a property on NSObject
.
回答2:
Name is 100% not accessible after the object is loaded, something I always thought was odd too.
What is accessible is "tag", if you really want to access an element without defining an outlet you can set the (integer only) "tag" value, and then within the superview that holds the tagged element call viewWithTag:
passing in the same integer. Don't forget the default is "0" so use something else.
回答3:
It can be done by the element tag:
Lets say you have UIView xib file called "yourView" and inside it there is UILabel that you want to access it without wiring.
- Set a tag to the UILabel in "yourView" xib file, lets assume you set UILabel tag to 100.
- After loading "yourView" anywhere you can get UILabel without having any wiring by using this code.
UILabel* yourlabel =(UILabel*) [yourView viewWithTag: 100]; //do whatever you want to your label.
回答4:
I think you can try opening the xib in some external editor as XML and see how the compiler sees it, then you might possibly do the same way
回答5:
For iOS6+ you can use restorationId instead of tag, to make it more "readable", for example you can set the same name in your nib file and in restoration id.
If you do not want to link all the outlets from your nib to your viewcontroller, you still can access them by searching in your current view subviews tree. Note that subviews arrangement is a tree (the same tree that you can see in your nib file), so you will need to do some recursion if you have nested views.
For example:
UIButton *nibButtonView = nil;
for (UIView *view in [self.view subviews]){
if ([view.restorationIdentifier isEqualToString:@"myNibButtonView"]){
nibButtonView = (UIButton *)view;
}
}
[nibButtonView setTitle:@"Yeah" forState:UIControlStateNormal];
In your nib file you should have a button with a restorationId equals to "myNibButtonView", you can find the restorationId textfield in your identity inspector (third column of utilities)
You may use this if you have a huge number of outlets a you don't want to linked them all.
来源:https://stackoverflow.com/questions/2292355/how-can-i-programmatically-access-ui-elements-in-a-nib-without-wiring-them