How can I access various UIViews within a Nib, programmatically? - iphone

First, I am super-new to Objective-C/iOS development and, in fact, this question is for my first, test project. Also, I come from a C#/WinForms background so I'm coming into iOS development with certain pre-conceived notions of user interface design and application state. Please bear with me and help clear up my confusion.
I just created my first iOS application project which, consists of a Single View. I allowed Xcode to create all the files for me through the Single View project wizard. When it was finished, I opened my new applications, single UIView Nib file in the designer and I dropped three sliders onto the view.
The desired purpose of this application is very simple-- each slider corresponds to either the R, G or B values associated with the background color of my view.
I have figured out how to set the background of my view but I can not figure out how to access the values of each slider objects. Yes, I can hook-up and respond to an IBAction for each slider, but my plan is that each time a slider's value changes, I want the IBAction to call a refactored method that accesses the values of all three sliders and then set's the views Background Color based of the values associated with each one.
How can I access the values of my sliders? Specifically, how can I access the value of the sliders that I created by dragging and dropping them into the Nib designer window? I've seen code explaining how to programatically add a UISlider to your UIView and then access the value, but how do you access the value of a UISlider that's added to the Nib and, I assume, will be automatically "wired" in at compile time?
Hopefully this makes since? If I'm missing an intermediate step or critical concept, please let me know.

You create UISlider ivars/properties and make them IBOutlets:
#property (nonatomic, retain) IBOutlet UISlider* yourSlider;
You synthesize it in your .m file and the connect the IBOutlet in interface builder. The yourSlider pointer is then a reference to the object you connected it to. Note that it will only be loaded with the view of the UIViewController and therefore will be nil until viewDidLoad is called. You also must set it back to nil
self.yourSlider = nil;
in your viewDidUnload method (so that it is released). In Xcode 4 you have a convenient way of doing all the above steps in one action (see the "Interface Builder is Built-in" of What's new in XCode 4)

Similar to CTRL+dragging from the UISlider to the .m file to create the IBAction, you can CTRL+drag the UISlider from your .nib designer to the associated .h file and have it create a property for you. Then, from your .m file, you can access self.mySlider (or whatever you name the property).
There's a video on this page that shows binding to a UISlider specifically.

Another approach is to use the tag property of a view.
#property(nonatomic) NSInteger tag
- (UIView *)viewWithTag:(NSInteger)tag
You can set the tag property of a view in the interface builder and later reference that view in your controller.
UIView *myViewFromTag = [self.view viewWithTag:theNumberYouSetInIB];
In general, it's better to stick with IBOutlets. However, there are certain situations where it makes more sense to use tags.
Good luck!

Related

how to update label/textfield of storyboard uiviewcontroller from controller.m?

I am new to iphone development, and i'm currently using xcode 4.2.
I drew a UIViewController into the storyboard. I put some labels into the UIViewController. It is associated to a controller.m file.
How do I, from the viewdidload function of the controller.m file, update the text in the label?
I don't know how to get the variable name or handle name of the elements in the uiviewcontroller on the storyboard.
If you are creating things through storyboard, the easiest way is to create an IBOutlet property for each UI element. This creates an instance property for the UIElement which you can then reference in your code - to set state, get values, etc.
You can do this through Control-click-drag on the UIElement to your UIViewController.h file (split view: RETURN-Command-Option: to display counterparts). XCode will pop up a dialog enabling you to name it (the same gesture will also enable you to create IBActions - the function that is called when the UIElement gets interacted with). You can also Control-click-drag on the left sidebar listing of the UIElements in your ViewController if that is easier.
There are YouTube videos that show it better than my weak explanation. I was skeptical # storyboard/IB at first because I don't like UI magic and prefer to do things through code, or at least see the code that results from the magic, but it really does work pretty well and saves some of the tedium of UI coding.
One gotcha to be aware of: if you make an IBOutlet and then delete the UIElement, you will have code errors because of the errant reference. Those are easy to find and fix. The UIViewController object will also contain those references and will result in unpleasant crashes - so Control-click on the UIViewController object (or use the Inspector panel) and any element that has an orange triangle == broken.

UIImageView Created In Interface Builder Set To nil When Needed

I have an application where small number of objects are defined in the MainWindow. A pair of these objects are a UIViewController and the UIImageView that goes along with this controller. When the application starts up, the entire MainWindow.xib get loaded & I can see that this UIImageView has been loaded into memory. At this point, I have no need for this UIImageView.
When I need the UIViewController, I perform the usual alloc/init setup. At this point, when I look at this controller with the debugger, the controller is setup correctly, but the UIImageView that should have been pre-wired with Interface Builder is always nil.
Any suggegstions on how to make sure this UIImageView is loaded into memory correctly? This is the only case where I've had an issue with objects defined within this single MainWindow.xib file.
Assuming you have connected the view outlet, all you need to do is access the view property. When you look in the debugger, you're seeing the property's corresponding ivar. When you access the property in code, it should load for you. So, it should be as simple as:
[controller view];
Not quite sure I understand the problem, but instead of having the UIImageView in MainWindow.xib, why not have it in the xxxViewController.xib where it's actually going to be needed?

UIView (subview) does not send IBActions to AppDelegate

I'm having this problem because I originially made everything in the main NIB, but then after reading learned that it is better to use subviews.
I've got my IBActions in the AppDelegate, and I've successfully connected and loaded my subviews. However, when I try to connect my buttons in the subviews to the IBActions in the AppDelegate, the IBActions appear under the "First Responder". They seem to connect fine, but when running the application they do not trigger the IBActions (I've confirmed this with some NSLogs, it's not an error in the code within the IBActions). What am I doing wrong?
Thanks!
The AppDelegate should only be used for very specific items such as implementing the UIApplicationDelegate protocol (i.e. methods like applicationDidFinishLaunching) or in some cases storing global variables.
You should keep IBActions and other outlets in their respective view controller files (i.e. if you created MyViewController.h and MyViewController.m which are linked with MyViewController.xib where you have some buttons, images, etc.). They can then be hooked up via dragging the inspector control you want (i.e. TouchUpInside) to the File's Owner.
Something you should read to better understand view controllers: http://developer.apple.com/iphone/library/featuredarticles/ViewControllerPGforiPhoneOS/Introduction/Introduction.html
Typically it is best to create a unique view controller for each view you will present to the user. For instance, if I had a main screen and then an "about" or a settings screen, I would make each of those their own view controller. It helps organize things better than using one view with a whole bunch of subviews that you hide/show and will also improve loading times and general performance.
Update for your 2nd question in the comments about accessing the app delegate:
First, you need to import the .h file (i.e. #import "AppDelegate.h") for the app delegate into whichever view controller .m file you wanna use to access whatever variables, arrays, etc you have stored in the app delegate files. Make sure you synthesize whichever objects you create in the app delegate's .h file in the app delegate's .m file so the getters and setters are created (so you can access them).
Then in the view controller .m file, in whichever method you are using:
-(void)someMethod {
// here we just create a shortcut to the app delegate called "theAppDelegate"
YourAppDelegateFileNameHere *theAppDelegate = (YourAppDelegateFileNameHere *)[[UIApplication sharedApplication] delegate];
// now you can use the dot notation if you wanna access a variable
int SomeNewInteger = theAppDelegate.someIntegerYouHaveStored;
// or some array you have stored
return [theAppDelegate.someArrayYouCreated count];
}
Hope that helps!

How can I programmatically access UI elements in a NIB without 'wiring' them?

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.
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.
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.
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.
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
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.

Arbitrary objects to handle actions?

My question may be a bit stupid, but why can't any object instantiated in IB handle, say, button click? I mean I'm able to add my own object to a xib, and link outlets to cotrols and control actions to object's method, but once I press the button everything just crashes (uknown selector).
Do you guys have a hint around that?
EDIT: The code, as requested:
#interface TextController {
IBOutlet UILabel * textLabel;
IBOutlet UITextField * textField;
}
-(IBAction)buttonClicked:(id)sender;
#end
#implementation TextController
-(IBAction)buttonClicked:(id)sender {
textLabel.text = #"Ololo";
}
#end
Connections in IB are ok, just believe me. It's really hard to get them wrong with all this drag'n'drop stuff :)
EDIT 2: TextController is not a file owner (in this case it works fine). However, I just want to understand why I can't wire up an action to some object (may be even not a subclass of UIViewController).
You can wire outlets and actions to any object in the nib-file. Drag an NSObject form the library palette onto your nib-file, in Interface Builder. Then go to the Identity tab of the information palette and set the Class of your object.
This way you can instantiate any object of any class from your nib. If the target you want to hook to is statically created from the nib-file. Make sure that the file's owner have at least one reference to your object, or else it will be deallocated as soon as it has been created. Targets are not retained by the sender.
If the object you want to hook up should not be statically created from your nib, then implement awakeFromNib in a class that is instantiated from the nib-file and hook up the targets in code.
Last option is if you do not have any sub-class of your own in the nib-file at all. Then implement initWithNibName:bundle: in your UIViewController subclass, and hook up your targets in code after calling the super implementation.
post code, this usually means you dont have your connections wired up correctly. Is file's owner TextController in IB?