0
votes

I have two view controllers, and each have a UIView that has a label on it. I want to set the view from the first view controller to be the second view when I press a button. When I go to do this, the second view controller doesn't load the view and the pointer is null to my UIView on my second view, so I can't do anything with it on my first view. All I am doing is this, as my secondViewNew is a property of my secondView. Is this possible to be done on the storyboard? I have also tried the [self.storyboard instantiateViewControllerWithIdentifier:@"beaconContentID"] method of doing things. In this project, secondViewNew is a UIView as well, and I have imported all the relevant classes.

SecondViewController *secondViewControllerInstance =  [[SecondViewController alloc] init];

UIView *test = secondViewControllerInstance.secondViewNew;

NSLog(@"%@",test);
1
i have also tried loading the view from within my first view by inserting [secondViewControllerInstance loadView];Hudson Buddy
your secondViewNew is not going to be loaded immediately after alloc/init. You are only guaranteed to have it instantiated after the awakeFromNib method is called on your second view controller. I'm not sure what you are trying to achieve but it sounds like you should separate your view from the view controller if it doesn't "belong" to one of them. Just subclass UIView and create an associated nib. This way you can instantiate the view whenever you need it.Rog
Yeah that's what I decided to do, but how do I associate a nib file with OBJ-C files? I just created a new nib file but I can't associate the file's owner to any of my classes.Hudson Buddy
I'll write it down as an answer as this might get too long.Rog

1 Answers

0
votes

Your secondViewNew is not going to be loaded immediately after alloc/init.

You are only guaranteed to have it instantiated after the awakeFromNib method is called on your second view controller.

I'm not sure what you are trying to achieve but it sounds like you should separate your view from the view controller if it doesn't "belong" to one of them. Just subclass UIView and create an associated nib. This way you can instantiate the view whenever you need it.

Here are the steps:

  • Create a new subclass of UIView i.e. CustomView
  • Create a new nib file i.e. CustomView.xib
  • Add a view to your nib file and set its custom class to CustomView, add subviews as you like, etc

Finally, in the view controller where you want to use the custom view:

CustomView *customView = [[[NSBundle mainBundle] loadNibNamed:@"CustomView" owner:self options:nil] objectAtIndex:0];