13

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.

Sti
  • 8,275
  • 9
  • 62
  • 124
  • did you get any luck on this? – Bhavesh Lathigara May 24 '14 at 11:19
  • @BhaveshLathigara I'm afraid not. I ended up designing the views as individual .xib or .nib or whatever they're called. Exactly what I needed, but I did not get instances of what I designed into the Storyboard. Create a new file, but instead of choosing the regular `Cocoa Touch -> Objective-C class` choose `User Interface -> Empty`. This will open the .xib/nib-editor-thingy, and you can drag whatever you'd like into the screen, eg. a UIView or a tableViewCell. To add them in Storyboard, add a regular view or cell to the controller, and change the class in the inspector to the one you just made – Sti May 24 '14 at 14:45
  • yes that's right I already used what are you trying to say but thing is that, I don't want to use any xib when I am using Storyboard. ok so lets keep as it is. Now I am using custom view by xib and use it any where. thanks for reply – Bhavesh Lathigara May 26 '14 at 10:57

2 Answers2

6

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, i.e. 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.

Chris
  • 1,663
  • 1
  • 15
  • 19
2

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.

Eric Qian
  • 2,246
  • 1
  • 18
  • 15