Alex
Alex

Reputation: 3072

How can I create a view to add to a view controller?

I am using UAModalPanel to create a popover controller effect. I can get the popver box to display, but I am struggling to figure out how to create a view (graphically, in storyboard), instantiate that view in code, and add it to the UAModalPanel.

What I've Tried

That's it. Surely there is a way that I can make a view in storyboard, have it make a sub-class of UIView which I can then grab in code where I need to use it? Instead of laying it out in code?

Upvotes: 0

Views: 225

Answers (2)

Warren Burton
Warren Burton

Reputation: 17378

Unless you need the segues you may better off creating a standalone XIB

Layout the view as you need, set its Class to your own (MyCustomView) then instantiate like this

 NSArray *nib=[[NSBundle mainBundle] loadNibNamed:@"MyCustomView" owner:self options:nil];
 MyCustomView *view = (MyCustomView*)[nib objectAtIndex:0];

As long as your view is the first/only object in the XIB this will instantiate the view for you

Upvotes: 1

Mick MacCallum
Mick MacCallum

Reputation: 130222

In storyboards you'll want to drag and drop a new UIViewController then give it an identifier here:

enter image description here

Then in code you can get the view property of this view controller with the following:

UIViewController *myController = [self.storyboard instantiateViewControllerWithIdentifier:@"myIDfromTheLastStep"];

Now you can get the conrollers view property and make adjustments. Here's an example of frame change

[myController.view setFrame:CGRectMake(0, 0, 320, 320)];

Upvotes: 2

Related Questions