对于我这个入门级小白来说研究了老半天才将oc的tabbar写法翻译成swift写法·····废话不多说现在开始。
我的思路是让tabbarcontroller做四个navigationcontroller的容器。
首先把工程里的mainstoryboard里的页面删掉,拖一个tabbarcontroller控件进来,并在右侧属性栏将该控件选择为初始页面(以initial·········为开头的选项)
然后建立第一个页面的storyboard,并将里面的navigationcontroller(没有的话就拖一个进来)设置成初始页面(和tabbarcontroller设置一样)并再拖一个tabbaritem(tabbar的按钮)到该navigationcontroller下。后面的个页面也是如此照做。代码都在appdelegate里写的。下面是代码
oc代码(四个页面):
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
UITabBarController *tab =[[UIStoryboard storyboardWithName:@"Main"
bundle:nil]instantiateInitialViewController];
UINavigationController * first = [[UIStoryboard storyboardWithName:@"First" bundle:nil]
instantiateInitialViewController];
UINavigationController * second = [[UIStoryboard storyboardWithName:@"Second" bundle:nil]
instantiateInitialViewController];
UINavigationController * third = [[UIStoryboard storyboardWithName:@"Third" bundle:nil]
instantiateInitialViewController];
UINavigationController * fouth = [[UIStoryboard storyboardWithName:@"Fouth" bundle:nil]
instantiateInitialViewController];
tab.viewControllers = @[first,second,third,fouth];
self.window.rootViewController = tab;
}
swift代码(这个是五个页面的):
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
// MARK: - Action
let tab:(UITabBarController) = UIStoryboard.init(name: "Main", bundle: nil).instantiateInitialViewController()as!UITabBarController
let first:(UINavigationController) = UIStoryboard.init(name: "First", bundle: nil).instantiateInitialViewController()as!UINavigationController
let second:(UINavigationController) = UIStoryboard.init(name: "Second", bundle: nil).instantiateInitialViewController()as!UINavigationController
let third:(UINavigationController) = UIStoryboard.init(name: "Third", bundle: nil).instantiateInitialViewController()as!UINavigationController
let fourth:(UINavigationController) = UIStoryboard.init(name: "Fourth", bundle: nil).instantiateInitialViewController()as!UINavigationController
let fifth:(UINavigationController) = UIStoryboard.init(name: "Fifth", bundle: nil).instantiateInitialViewController()as!UINavigationController
tab.viewControllers = [first,second,third,fourth,fifth]
self.window?.rootViewController = tab
return true
}
结束语:
本人编程刚上路的级别·····由于工作需要所以要学会swift,现在两门语言同时啃·····如果哪里写的不好望大神们赐教。小编我在这谢谢了!我非常想快速进步学习
网友评论