在swift中添加没有标签栏控制器的标签栏

时间:2014-12-03 08:44:20

标签: ios swift tabbar

我需要在视图中添加一个标签栏,不带标签栏控制器,因为在我的prepareForSegue函数中,我将数据传递给我的视图,其中包含标签栏。

我在对象库中添加标签栏及其项目,在我的视图类中添加UITabBarDelegate。 我的代码是这样的:

class SchedaCompletaVC: UIViewController, UITabBarDelegate {

var event:Event! // data passing from prepareforsegue

@IBOutlet var mainTabBar:UITabBar!
var descrizioneTab:UIViewController! //if i click on first tab, so my default view 
var mappaTab:UIViewController! // second tab

override func viewDidLoad() {
    super.viewDidLoad()
    println("event \(event.eventId)")

    // Do any additional setup after loading the view.
}

func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem!) {
    switch (item.tag) {
    case 1:
        self.view.insertSubview(descrizioneTab.view.belowSubview(mainTabBar)) //here I had error
       break;
    case 2:
        //if second tab is selected
        break;
    default:
        break;
    }
}

我从tutorial whitch得到了这个代码用于objective-c,我试图快速转换它。

我的代码不起作用,我不知道该怎么办?

请帮忙。谢谢!

更新: 我的故事板是这样的:enter image description here

2 个答案:

答案 0 :(得分:4)

这是swift版本: -

import UIKit

class ViewController: UIViewController, UITabBarDelegate {

    @IBOutlet weak var mainTabBar: UITabBar!

    var viewController1: UIViewController?
    var viewController2: UIViewController?


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem!) {

        switch item.tag {

        case 1:
            if viewController1 == nil {

                var storyboard = UIStoryboard(name: "Main", bundle: nil)
                viewController1 = storyboard.instantiateViewControllerWithIdentifier("ViewController1") as! ViewController1
        }
            self.view.insertSubview(viewController1!.view!, belowSubview: self.mainTabBar)
            break


        case 2:
            if viewController2 == nil {

                var storyboard = UIStoryboard(name: "Main", bundle: nil)
                viewController2 = storyboard.instantiateViewControllerWithIdentifier("ViewController2") as! ViewController2
        }
            self.view.insertSubview(viewController2!.view!, belowSubview: self.mainTabBar)
            break

        default:
            break

        }


    }


}

然后你应该点击UITabBarItem& ctrl将其拖动到viewcontroller并单击delegate。

标签栏项目的标签(第一个标签项标签为1,第二个标签项,标签为2)

ViewController的StoryBoard标识符(分别是ViewController1和ViewController2)

答案 1 :(得分:2)

我有一个Objective-C解决方案。它工作正常,你可以把它转换成Swift。

ViewController.h文件代码

        #import <UIKit/UIKit.h>
        @interface ViewController : UIViewController<UITabBarDelegate>
        @property (weak, nonatomic) IBOutlet UITabBar *mainTabBar;

        @end

ViewController.m文件

    #import "ViewController.h"
            #import "MyFirstTabViewController.h"
            #import "MySecondTabViewController.h"
            @interface ViewController ()
            {
                UIViewController *viewController1;
                UIViewController *viewController2;
            }

            @end

            @implementation ViewController

            - (void)viewDidLoad
            {
                [super viewDidLoad];
                // Do any additional setup after loading the view, typically from a nib.
                self.mainTabBar.delegate=self;
            }


            - (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {



                switch (item.tag) {
                    case 1:
                        if (viewController1 == nil) {
                            viewController1 =
                            [[UIStoryboard storyboardWithName:@"Main"
                                                       bundle:NULL] instantiateViewControllerWithIdentifier:@"First"];

                        }
                        [self.view insertSubview:viewController1.view belowSubview:self.mainTabBar];

                        break;
                    case 2:
                        if (viewController2 == nil) {
                            viewController2 =
                            [[UIStoryboard storyboardWithName:@"Main"
                                                       bundle:NULL] instantiateViewControllerWithIdentifier:@"Second"];

                        }
                        [self.view insertSubview:viewController2.view belowSubview:self.mainTabBar];

                        break;
                    default:
                        break;
                }
            }


            - (void)didReceiveMemoryWarning
            {
                [super didReceiveMemoryWarning];
                // Dispose of any resources that can be recreated.
            }

            @end

不要忘记。

1)标签栏项目标签(第一个标签项标签为1,第二个标签项,标签为2)

2)ViewController的StoryBoard标识符(分别为第一个和第二个)

StoryBoard Image