Reputation: 2866
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
Thanks!
Upvotes: 0
Views: 274
Reputation: 100632
If you have:
UILabel *localVariableForLabel = ...some label just created...
And you want to store it to the outlet 'instanceVariableForLabel' you can use key-value coding. So:
[targetObject setValue:localVariableForLabel forKey:@"instanceVariableForLabel"];
Or pass any other NSString
. This is exactly what Cocoa does when it loads a NIB, and is why your outlets need to be key-value coding compliant.
I'm unclear on exactly what you mean by creating a new outlet, but if you want to be able to put the label somewhere and access it later by name, an NSDictionary
is probably what you're looking for.
Upvotes: 0
Reputation: 5313
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.
Upvotes: 4