ChatGPT解决这个技术问题 Extra ChatGPT

Custom views with Storyboard

In complex screens (View Controllers) I used to separate the whole thing in smaller pieces (I call them widgets). These widgets consist basically of a MyWidget.h and a MyWidget.m file as well as a MyWidget.xib file, where the root element is a UIView and the MyWidget class is the File Owner of the UIView. In the init of this widget I do a loadNibNamed.

In my View Controller I then do a [[MyWidget alloc] init], which I add to View's Controller main view as a sub view. This, so far, works perfectly.

I'm now wondering, how to do the same with storyboard, because I cannot really start to drag in a UIView somewhere, I always have to start with an UIViewController, which I don't want to.

If there is no possible way doing this with a Storyboard, can I simply do it the old way, by using the Storyboard for my main screens and segues, and use a separate .xib file to define custom views?

Do you wish to create xibs for your view controllers separately and not in the story-board?
Did you find a solution to this?
@aryaxt: I'm using a mixture of Storyboard and xib's. Storyboard for the main screens and xib's with only an UIView as the root for complex views or widgets. So not really an answer to my original question (by using storyboard), but basically doing the same what I did before already.
these days, just use a container view, it's really that simple .. stackoverflow.com/questions/23399061/…

C
Community

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:

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: 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: You can hook it up to your view controller like you would hook up any other view: The added view shows up in your Document Outline and you can hook up actions and references there too:

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:

Drag a UITableView from the Object Library into your newly added view. Then drag a UITableViewCell into that UITableView. 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. 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:

https://i.stack.imgur.com/ZgJPH.png


March 2013, I didn't need to do this - you should ignore the black bar completely, and drag/drop your view directly into the main view, and it works fine (Xcode 4.5+)
Is it possible to reuse that custom uivew on the same ViewController - If I ex. needed two custom uivews - And if it is how?
Dang, I prefer to just create a separate xib. I probably won't remember all this when I have to update the app in 6 months.
Though this is an awesome answer, I accidentaly found another better way I think. When hooking up outlets to actual view components. If you hover a little over a component of such an inner view - it pops up for easier selection. Then you see it on the storyboard...
While this is an incredibly admirable historic answer, really it is now totally out of date. Apple fixed the whole issue by introducing "container views", which is now the central, basic thing you do when making apps - everything a "container view" all the time. Thank goodness, it's now very easy!
t
tipycalFlow

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:

https://i.stack.imgur.com/s79p3.png

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.
}

Thanks for your very detailed answer, but I'm actually not looking for a custom view controller, but a custom view (a widget), which gets added to a standard UIViewController. So the view controller is not the problem, but I was wondering what the best approach is to create custom widgets. Either A) do it as I did before, with a .xib file or B) some other approach which I don't know, yet :-)
Hmm...I think I got what you want. Basically you want to handle multiple views with the same controller, right? I can't think of a way other than loadNibNamed in that case... XO
This solved this problem for me: stackoverflow.com/questions/11047991/… However, instead of viewDidLoad, I used loadView...
Just to repeat, this is all (thank goodness!) totally out of date now that container views are here. Just click a container view and it's all done
v
vijay_hrd

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


关注公众号,不定期副业成功案例分享
Follow WeChat

Success story sharing

Want to stay one step ahead of the latest teleworks?

Subscribe Now