[英]IOS Swift Programmatic UITabBar
我正在尝试在IOS中创建和使用UITabBar 。 我的代码生成了一个UITabBar,但是当我单击一个选项卡时,它崩溃并显示错误-
这是代码,在视图控制器的viewDidLoad()中调用
let v1 : UIView = UIView(frame:self.view.frame);
let v2 : UIView = UIView(frame:self.view.frame);
v1.backgroundColor = UIColor.redColor();
v2.backgroundColor = UIColor.blueColor();
let vc1 : UIViewController = UIViewController();
let vc2 : UIViewController = UIViewController();
vc1.view = v1;
vc2.view = v2;
vc1.title = "View 1";
vc2.title = "View 2";
let nav1 : UINavigationController = UINavigationController(rootViewController: vc1);
let nav2 : UINavigationController = UINavigationController(rootViewController: vc2);
nav1.delegate = self;
nav2.delegate = self;
let tabsArr : [UINavigationController] = [nav1, nav2];
let main_tab : UITabBarController = UITabBarController();
main_tab.viewControllers = tabsArr;
main_tab.delegate = self;
self.view.addSubview(main_tab.view);
这是上面代码的视图
题
如何以编程方式100%运作? 当我单击选项卡时,我想通过更改选项卡来响应!
谢谢 :)
多亏了T先生的帮助,我解决了! 关键是UITabBarController 替换了UIViewController,它没有放入其中!
这是AppDelegate.swift中的最终解决方案:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
self.window = UIWindow.init(frame: UIScreen.mainScreen().bounds);
let N : Int = 4;
for(var i = 0; i < N; i++) {
print("please?");
let newView : UIView = UIView();
let redC : CGFloat = CGFloat(arc4random_uniform(255))/CGFloat(255);
let greenC : CGFloat = CGFloat(arc4random_uniform(255))/CGFloat(255);
let blueC : CGFloat = CGFloat(arc4random_uniform(255))/CGFloat(255);
newView.backgroundColor = UIColor(red: redC, green: greenC, blue: blueC, alpha: 1);
views.append(newView);
let newViewController : UIViewController = UIViewController();
newViewController.title = String(format: "View %d", arguments: [i]);
newViewController.view = newView;
let newNavC : UINavigationController = UINavigationController(rootViewController: newViewController);
navs.append(newNavC);
}
let main_tab : UITabBarController = UITabBarController();
main_tab.viewControllers = navs;
self.window?.rootViewController = main_tab;
self.window?.makeKeyAndVisible();
return true;
}
谢谢你Mr.T !!!!!!! :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.