2014-12-03 90 views
0

我需要在標籤欄控制器中添加標籤欄,因爲在我的prepareForSegue函數中,我將數據傳遞給包含標籤欄的視圖。在swift中添加沒有標籤欄控制器的標籤欄

我添加了對象庫中的標籤欄及其項目,並在我的視圖類中添加了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

+0

究竟是什麼_」我的代碼不起作用「_是什麼意思? – 2014-12-03 08:48:17

+0

您嘗試將鏈接的代碼轉換爲_Swift_只是做了一半......您真的想知道它不起作用嗎? – holex 2014-12-03 10:06:32

+0

我是編程新手,你能告訴我我錯過了什麼嗎? – Chongzl 2014-12-03 10:25:48

回答

1

我只有一個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 Image

+0

感謝您的提交,但我的問題仍未解決。 – Chongzl 2014-12-03 10:34:03

+0

你是用故事板嗎? – 2014-12-03 10:36:54

+0

是的,我用main.storyboard – Chongzl 2014-12-03 11:02:04

3

這裏是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並點擊委託。

標籤的標籤欄項目(第一個選項卡項目標記爲1,對於第二個選項卡項目,標籤是2)

故事板標識符視圖控制器(ViewController1和ViewController2,分別)

+1

有沒有什麼辦法可以在不創建視圖控制器的新實例的情況下實現這一點? – wasimsandhu 2017-05-02 02:05:17