Converting an Utility application (2 view controllers) to a Tab bar Application

最后都变了- 提交于 2020-01-06 04:31:11

问题


As the title suggests, I have built an utility based app.
The app consists (at the moment) of 2 view controllers + a model class hierarchy.
The thing is , as I'd like to add some features to the app , I would like to convert it to a TabBar based application.

As a first step , I would like the first view to be the first view of the tab bar , and the flipSideView to be one of the other tab bar items.

Is there any "standard procedure" / "grocery list" for such tasks ?
I'm sure some of you have encountered the same problem , and would love some advice on "slicing up" the app , and "wiring it up" after creating a new nib file for the main window (is that the first step ? )

Thanks in advance.


回答1:


I would probably start as follows (assuming you are using Interface Builder and using the standard Xcode utility app template):

  • Edit your MainWindow.xib file and drag a Tab Bar Controller object into the top level of your view hierarchy.
  • The default tab bar controller in IB includes two view controller items as examples. Click the first one and change its class to be your existing Main View Controller class.
  • You can also set the icon and tab bar title for the main view controller item by adding the associated Tab Bar Item that IB has already added to the view hierarchy.
  • Do the same for the second tab bar item setting the class for the FlipsideViewController.
  • Delete the old version of your Main View Controller from the NIB file and also remove the IBOutlet property from the Application Delegate (you probably also have references to the main view controller in dealloc which should be removed).
  • In your App Delegate add an IBOutlet property for the Tab Bar Controller as follows (don't forget to synthesise the property):

    @property (nonatomic,retain) IBOutlet UITabBarController *tabBarController;

  • In IB wire up the tabBarController outlet from the App delegate object to the Tab Bar Controller object.

  • Finally to get the tab bar controller to show up in place of the main view controller change the code in application:didFinishLaunchingWithOptions: to the following:

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

This should get the basic tab bar app up and running and you can add additional view controllers to the tab bar.

Since you no longer want to flip between the MainView and FlipsideView you can remove the references to the FlipsideViewControllerDelegate from the MainViewController along with the info button and its IBAction method showInfo. Likewise in the FlipeSideViewController you should remove the done button from the view and its IBAction method as these no longer make sense when used with the tab bar.




回答2:


so, step by step :): 1. declare UITabController outlet in your app delegate. 2. in MainWindow.xib: first drag the instance of UITabBarController in document window, then make connection from app delegate to that instance. now you can set MainViewController as UiTabBarController's first tab viewcontroller, then set FlipsideViewController as UiTabBarController's second tab viewcontroller. 3. in app delegate's appDidFinishLaunching replace [self.window addSubview:mainViewController.view]; with [self.window addSubview:theNameOfUITabBarControllerOutlet.view];. that will do the work. sorry, I don't know your background, that's why I'm not very specific about performing a particular action I decribe, so let me know if you find yourself stuck



来源:https://stackoverflow.com/questions/6398161/converting-an-utility-application-2-view-controllers-to-a-tab-bar-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!