I have just started learning Objective-C / Cocoa Touch. I am working on an app that allows for creation of views on the fly (for the sake of this question lets just assume that clicking some button causes a button and label to be added to the current view)
So when a user clicks this ‘Create Button’ a new button and label is added to the current view. When a user clicks on the newly created button I want text in the newly created label to change to . Now I know that I can assign an IBAction to the button when I create it at runtime but I am not sure how to associate the new label with an Outlet.
Furthermore I want to do this with as little code as possible but I am not seeing how this is going to be possible. AFAIK the only way I can really identify the controls in the view is by their tag property which isn’t entirely helpful so I will have an event handler that ends up looking like this (pseudocode):
switch(tag)
{
case 1:
//label1.text = @"foo";
break;
case 2:
//label2.text = @"foo";
break;
//etc
}
In C# I could have done something like this (assuming e is of CommandEventArgs, all buttons are wired to this event, and CommandName contains the id of the label we want to update):
Label lbl = (Label)panel1.FindControl(e.CommandName);
lbl.Text = "foo";
So my questions are
- Is it possible to assign/create Outlets at run time and associate them with a given label
- Is there a better way of figuring out what label should be acted upon other than using a switch?
Thanks!
The purpose of IBOutlets is generally to let you associate things created in Interface Builder with instance variables in your code. Since you are creating the views in code to begin with, it sounds like you should just be storing references to them in instance variables and not worrying about IBOutlets.