使用情节串连板定制视图

在复杂的屏幕上(查看控制器) ,我用来把整个东西分成更小的部分(我称之为小部件)。这些小部件基本上由 MyWidget.hMyWidget.m文件以及 MyWidget.xib文件组成,其中根元素是 UIView,MyWidget 类是 UIView 的 File Owner。在这个小部件的 init 中,我做了一个 loadNibNamed

在我的视图控制器中,我然后做一个 [[MyWidget alloc] init],我把它作为一个子视图添加到视图的控制器主视图中。到目前为止,一切都很顺利。

我现在想知道,如何用故事板做同样的事情,因为我不能真正开始在某个地方拖入一个 UIView,我总是必须从一个 UIViewController开始,我不想这样做。

如果没有可能的方法做到这一点与故事板,我可以只是做它的老方法,通过使用故事板为我的主要屏幕和继续,并使用一个单独的。Xib 文件来定义自定义视图?

70252 次浏览

If you're just looking to make your view controllers else-where(and not in your story-board), then there's a pretty simple way to accomplish this:

1) Create your CustomViewControllers(abcdController in the code I tried) with their individual xibs as usual.

2) Add a UIViewController(or whatever was the superclass of your CustomViewController) to the story-board.

3) Set the CustomClass to CustomViewController instead of UIViewController as shown here:

enter image description here

4) Finally, in your viewDidLoad, load the custom xib and you're done.

- (void)viewDidLoad
{
[super viewDidLoad];
[[NSBundle mainBundle] loadNibNamed:@"abcdController" owner:self options:nil];
// Do any additional setup after loading the view from its nib.
}

Putting the widget/view in a separate .xib file works, and is appropriate especially if you might want to reference that same view from multiple View Controllers.

However, sometimes you do want to see the additional view/widget within the same storyboard, and it is possible. Here's how you do it:

  1. Select your view controller in IB (click on the black bar below the view), then drag a UIView from the Object Library into the black bar:

    enter image description here

  2. When a view is in the black bar, it's instantiated like any other view in IB but just isn't added to your view hierarchy until you do so in code. Change the view's class to match your own subclass if necessary:

    enter image description here

  3. You can hook it up to your view controller like you would hook up any other view: enter image description here

  4. The added view shows up in your Document Outline and you can hook up actions and references there too:

    enter image description here


Now, the problem that remains is that you can't actually see the view no matter how many times you try to click or double click, which would defeat the whole purpose of putting it in the same storyboard. Fortunately there are two workarounds that I know of.

The first workaround is to drag the view from the black bar back into your view controller's view, edit it, then drag it back into the black bar once you're done. This is troublesome but reliable.

The other workaround is more finicky, but I prefer it because it lets me see all my views at the same time:

  1. Drag a UITableView from the Object Library into your newly added view.

  2. Then drag a UITableViewCell into that UITableView.

    enter image description here

  3. Once you do that, your view pops out magically by the side, but you have a UITableView that you don't want. You can either resize that to 0x0, or you can delete it and your UIView will (usually) still stay visible.

  4. Occasionally the secondary view will become hidden again in IB. You can repeat the above steps if you deleted the UITableView, or if the UITableView is still in the hierarchy you just need to click on the UITableViewCell and the view will appear again.

The second method works for UIViews but not so well for UIToolbars and is impossible for UIButtons, so the cleanest solution I've found when you need to include lots of different subviews is to attach a single secondary UIView to your view controller as a container that never gets shown, put all your secondary views in there, and use the UITableViewCell trick to make everything visible. I resize my dummy UITableView to 0x0 to make that invisible. Here's a screenshot of how it all looks like together:

enter image description here

I think you can do something like this to get instance of specific viewcontroller from Storyboard and use view on top of it.

ex:
MyViewController* myViewController = [[UIStoryboard storyboardWithName:@"Main"  bundle:nil] instantiateViewControllerWithIdentifier:@"myViewController"];
UIView* view = myViewController.view; //Get the view from your StoryBoard.

Hope this helps

Thanks Vijay