簡體   English   中英

iOS Swift程序化UITabBar

[英]IOS Swift Programmatic UITabBar

我正在嘗試在IOS中創建和使用UITabBar 我的代碼生成了一個UITabBar,但是當我單擊一個選項卡時,它崩潰並顯示錯誤-

  • (主線程) “線程1:EXC_BAD_ACCESS(代碼= 1,地址= 0x18)”

這是代碼,在視圖控制器的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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM