How do I know how many outlets do i need? [closed]
How do you figure out how many outlets you need by just looking at the finished App Design ?
开发者_开发技巧I know outlet is An ivar that you create and want to connect to an object in a nib file.
The right answer to this is that you don't care how many IBOutlets you have - the number is meaningless. It's like asking how many variables I need in my program.
As a few people have pointed out with varying degrees of accuracy, you need an outlet if you need to update a UI element independently of user interaction with that element. Examples would be:
- A cancel button that is only active when a network transfer is in progress
- An activity spinner that stops when the network transfer finishes
- a login field you need to populate with data from NSUserDefaults.
The best advice I can give you is to stop worrying about this and start coding. When you need an outlet it'll be obvious from the code. You'll have a user story that needs the UI to be updated and to allow that to happen, you'll need to add the outlet.
If you really care about the number, you can count them when the app ships to apple and come back to add your own answer here :-)
disclaimer - the next section for information only, I don't recommend you do this:
To be accurate, you don't need any IBOutlets to interact with the UI. You can locate a reference to a control (or any UIView subclass) at runtime using tags. A view's tag is simply a NSInteger proerty that you can search for in the view hierarchy. UIView
exposes a method called - (UIView *)viewWithTag:(NSInteger)tag
which searches all of its subviews for a view with the specified tag. You can either set tags at runtime or in interface builder. Of course using tags is both a runtime (processing) overhead and is more difficult to maintain that using outlets.
Count them.
But more seriously, you need to create an IBOutlet if you need to reference a UI element from the UIViewController. If the UI element is a button or other control, a reference to that item can be passed to the action method typically as "sender" and you may not need to create an IBOutlet for that element.
As an example, you may need to create an IBOutlet for a UILabel if you are programmatically setting the label's text.
It depends on the items you want to show in your view. If you want to show 3 items on the view, then you need to add 3 IBOutlets
.
But this is not necessary if you are not using a .nib file
as you can just programmatically add it to the self.view
as a subview.
You only need outlets if you need to modify the interface object in your code. So the question then is how many separate dynamic items do you have on your view? Does that answer the question for you?
精彩评论