简体   繁体   中英

Is there a way to 'design' a custom UIView in storyboard, and use in multiple ViewControllers without adding to any specific controller?

I'm in need of a custom UIView which is to be used in multiple different ViewControllers . Usually when I create a custom UIView , I drag an instance of UIView into my ViewController in storyboard, create a subclass of UIView , and point to this subclass in the Identity Inspector for the view. From there, I would connect the UI-objects as outlets to the header-file of the subclass.

The view I want to make now, is not supposed to be a part of any specific controller, but should be added to any controller that asks for it. A rogue UIView . I know I can create the entire UIView programmatically, and just create an instance of it, but I'd like to have it (and design it) in my storyboard.

In Storyboard, the only objects I'm allowed to drag 'outside a ViewController', are other ViewControllers.

I have never used anything other than Storyboard for iOS-developing, but I have come over tutorials which have been using the other mode, from the olden days , which looks like what I need. Is it possible to get something similar into my storyboard? Or would this require its own setup/design? If so, how?

I was also thinking of solving this with adding a 'phantom' UIViewController containing my custom View; designing it there, but instantiate it from the other controllers, but this sounds so hacky..

I'd like to do this with a UITableViewCell as well, but I guess that would have the same solution.

For your UIView, you should be creating a custom UIViewController in your storyboard and instantiate the view controller to access the view:

MyViewController *myViewController = [self.storyboard  instantiateViewControllerWithIdentifier:@"MyViewController"];
// Access the view using "myViewController.view"

This is a very common practice in iOS since storyboards were presented. If you are using multiple storyboards, you should create a new instance of UIStoryBoard with:

UIStoryboard * storyboard = [UIStoryboard storyboardWithName:@"StoryboardName" bundle:nil];

And then instantiate the view controller with this instance of the storyboard.

If you want to solely use a UIView, you should be creating a .xib file, ie the olden days format. To create a custom UITableViewCell, I would absolutely use a .xib file. Your last option would be to create a UIView programmatically, which could be called from any place in your application. Depending on the complexity of this view, this may be a valid option.

I don't think you can create a custom UIView in storyboard. So in this case, you can create a xib file for that custom view. Then when you want use that view, just use

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

to get that view.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM