简体   繁体   中英

How do I create a new minimal nib based iPhone app with a tab bar controller's view as the root view?

I am trying to learn the differences between "classic nib based" and "storyboard" based iPhone development workflows and the coding styles and approaches for each.

So, I created a new empty iPhone app in XCode 4.5, using iOS -> Application -> SingleView Application as my template. I wish to understand the way to turn this into an app with a Tab Bar Controller as the root controller and the root view of my application.

I deleted the empty root View in the iPhone nib and attempted to replace that view with a TabBarController's view:

在此处输入图片说明

At runtime, I get this exception and the app aborts:

"A view can only be associated with at most one view controller at a time"

So clearly I'm breaking a rule here, and having a TabBarController associated with a View, and then making that View also be the view pointed to by the File's Owner connection is not permitted. So what is the normal, idiomatic way to do this? Leave the original empty view in place, and then make the tabbarcontroller view a subview at runtime? Or some other technique?

Note: I'm not asking for a different template choice, nor how to do this most easily if that way involves storyboards. I'm asking for how to do this, without storyboards, with classic nibs only, and I'd like to understand how the "stock ui tab controller" object that is nib-instantiated could work.

Update: I can get rid of the error by getting rid of the default template-generated-single-view-templates' view controller object. Now I have an app that links and runs with a black empty window, and no visible root View. I attempted to use the Interface Builder to connect the IUTabBarController to the app delegate's property:

@property (strong, nonatomic )IBOutlet UIViewController *viewController; // I added IBOutlet

And in my app delegate:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    _window.rootViewController = _viewController;
}

Then I connected that outlet to the nib-instantiated UITabBarController object:

在此处输入图片说明

It seemed to me that this ought to have worked. I can run, but I have no root view.

The question in the title is easy to answer; Pick the iOS template for tab based user interface, and then uncheck "use storyboards".

However, the underlying question above, which I am trying to self-answer here is that new users would need to understand the boilerplate code that XCode would have generated if I unchecked "Use storyboards" and selected "tabbed user interface", using the template that I should have used, well enough to write it myself.

My problem was that I didn't understand the relationship of the view controller classes and how they are instantiated, and I overlooked the boilerplate code in the appcontroller.m file. I had it up and in front of me and yet I didn't see it.

  1. Create a view controller class for each page.

  2. The Apple demos don't use the UITabBarController subview directly, they parent it at runtime onto a blank nib view:

      [self.window addSubview:self.tabs.view]; 

Of course the apple sample is from the iOS 4.0 era.

Excellent sample code for reference of a simple app that uses a tab bar controller as the root UI widget: Apple's Simple FTP Sample however it's not a perfect sample because the apple sample uses a different nib for each page in the tab bar controller, and requires a lot more boilerplate code, because it is from the iOS 3.0 era.

However this answer isn't perfect because it doesn't answer the question "What is the native and expected idiom in the iOS 6.0/6.1 era, using the current framework".

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