0

I have a problem with UILabel and UIView. In my UIViewController I load a UIView:

NSArray *first = [[NSBundle mainBundle] loadNibNamed:@"customView" owner:self options:nil];
UIView *detail = [first objectAtIndex:0];

Inside the Xib of the UIView that i have already loaded (@"customView") there is a UILabel. How can i load also this UILabel and change the text?

Thanks

Wain
  • 118,658
  • 15
  • 128
  • 151
simox89
  • 43
  • 6

3 Answers3

0

You can tag the UILabel inside of the .xib file and then access the UILabel like this:

(Assume you have tagged the UILabel 42)

UILabel *label = [detail viewWithTag:42];

You can then change the text as you would with any other UILabel:

label.text = @"Text for the label";
Infinity James
  • 4,667
  • 5
  • 23
  • 36
0

If in the XIB the root view has a subview then that is also loaded when you load the XIB. You have a few options for connecting to it:

  1. Add an outlet to the file owner in the XIB (this is your view controller)
  2. Add an outlet to the root view that the view controller can use to get the label
  3. Use the tag on the label and viewWithTag: to search for it

Arguably option 2 is the best from that list as it is the best containment of knowledge and the most reusable.

Wain
  • 118,658
  • 15
  • 128
  • 151
  • The view i load (@"customView") is not a subview ok the storyboard, is in a different xib. I want to load the label of the xib that isn't inside the UIViewController but is in a different xib. – simox89 Mar 20 '14 at 11:02
  • You are loading it from an XIB, I see that. So what is the root view in the XIB? A `UIView` or a `UILabel`? Your question is no longer clear... – Wain Mar 20 '14 at 11:06
  • The root is the UIView and inside it I have some Label. I need to access to all the label. – simox89 Mar 20 '14 at 11:10
  • So I don't understand what about my answer isn't clear? There are 3 offered solutions to that problem... – Wain Mar 20 '14 at 11:23
  • Thanks!! I solved!! I didn't connect the File's Owner – simox89 Mar 20 '14 at 11:34
0

In the XIB file you can create an outlet to the file's owner. Then you can access the property of the view controller, since it is the file's owner of the XIB (set in your first line of code).

Lukáš Kubánek
  • 946
  • 1
  • 15
  • 27
  • The view i load (@"customView") is not a subview ok the storyboard, is in a different xib. I want to load the label of the xib that isn't inside the UIViewController but is in a different xib. – simox89 Mar 20 '14 at 11:03