Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load UIView with Nib and UIView class

I've tried this class

https://github.com/autresphere/ASDepthModal

i want it to popup like it does but i want to be able to set the labels programmatically, since i need the to change depending on what day it is. I'm using storyboard, so i've created a .xib and uiview.h and uiview.m. In my main UIViewController i have:

xibContents = [[NSBundle mainBundle] loadNibNamed:@".xib" owner:self options:nil];
testView = [xibContents lastObject];

in my .xib i have set the file owner to my uiview class, this create a problem: NSUnknownKeyException When i set the uiiew inside my .xib to my uiview class the application will load and i can open it just like it should, but i'm not able to change the state of the label programmatically? I'm complety lost here!

like image 357
rostgaard Avatar asked Dec 06 '25 13:12

rostgaard


2 Answers

Typically speaking, UIViews do not have access to IBOutlets. Apple kind of intended xibs to only be assigned to UIViewControllers.

However, you can load a view from a xib in two ways:

1) Create an extra xib to use in your UIViewController. Set the File's Owner to your view controller, and the class name of the view to your custom view class. In interface builder, this is under "custom class". You can set the view as a IBOutlet, and iOS will create an instance of your custom class when your UIViewController loads the xib and sets itself as owner (like you tried above, but only from within a controller class)

2) Load a xib in a UIView class, and set self to the resultant object:

- (id)init {
    self = [super initWithFrame:CGRectMake(0, 0, 1024, 352)];

    if (self) {
        NSArray* nib = [[NSBundle mainBundle] loadNibNamed:@"TCSNumberKeyPad" owner:self options:nil];
        [[nib objectAtIndex:0] setFrame:CGRectMake(0, 0, 1024, 352)];
        self = [nib objectAtIndex:0];
    }

    return self;
}

In either case, you will need to retrieve your label via code rather than IBOutlet properties. You can find your label in the subviews property:

UILabel* myLabel = [self.subviews objectAtIndex:0];

like image 92
James Avatar answered Dec 09 '25 04:12

James


Actually got this to work. I took the wrong approach. I found it simpler to just create the view and populate it with background image and labels when the button got clicked. would have been simple to do it in a UI designer, but this wasn't that hard actually.

Thanks to the people who helped me :)

like image 21
rostgaard Avatar answered Dec 09 '25 04:12

rostgaard



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!