0
votes

Here is the scenario

Create new project Tabbed Application. Add new controller, TestViewController with XIB file, in the XIB file just add one button with text "TestViewControllerButton".

If I create a button in FirstViewController, and add an action to go to TestViewController, the button in XIB file is displaed.

TestViewController* vc = [[TestViewController alloc] init];
[self presentViewController:vc animated:YES completion:nil];

The problem is when I add TestViewController to tab controller (index 2, start from 0), it does not show the XIB (I mean, the button does not displayed).

Here are my steps. I add UIViewController in Tab Controller, then change the class to TestViewController in Identity Inspector. Then add Tab Bar Item to TestViewController and link it to Tab Bar Controller, so when the last tab is clicked, it linked to TestViewController. But the problem is, the button in the XIB is not displayed (I add the button in TestViewController.xib).

Yeah, off course I can add the button directly in Main.storyboard in my TestViewController, but the idea is I want to manage the UI in TestViewController XIB, and the main.storyboard just load the view in TestViewController image.

Ok, I add some screenshot to make understand. This screen runs well when I do programmatically, run in UIViewController (code above - self presentViewController:vc animated:YES completion:nil).

Run in UiViewController

When I run from Tab

enter image description here

The setting I think is already correct (since it runs well in UIViewController)

Main storyboard custom class controller

Already set file owner to the TestViewController

File Owner

Already bind the outlet

Outlet

Thanks!

2

2 Answers

1
votes

I think you are using the ViewController of size Inferred , Change it to iPhone 4 inch in Attribute Inspector (Simulated Metrics -> Size and set the button frame acc. to that .

enter image description hereenter image description here

Hope this help :)

0
votes

Of course it will not show the controls. See, you are using two different view controllers at these places. In the xib, you have actually added your button and hence when an instance of your testViewController is created from the xib, it has the button and its associated action with it. However, in the case when you add a viewController in your storyboard and set its class to be that of TestViewController, all you are doing is setting the class type of the new view controller to be TestViewController. But you are not providing any information by which the storyboard can know that it has to create the new instance of testViewController from the image saved in its XIB. So it just crates a new view controller of type TestViewController, using its image from inside the storyboard (without the button in it).

So to get your view controller from the xib you will have to override initWithCoder: method inside your TestViewController implementation and return an instance of your viewController from the XIB. Something like:

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
  return [self initWithNibName:nil bundle:nil];
}