2017-06-21 87 views
0

我在我的Main.storyboard文件中有TabBarController。 在我的Upload.storyboard中,我展示了Main.storyboard文件中的ViewController,但它不包含标签栏。以编程方式向ViewController添加选项卡栏

viewProfile按钮应转到名为Sharks的选项卡,并在该选项卡中显示基于Upload.storyboard(模式视图)中收集的数据的视图控制器。

我可以通过编程方式添加标签栏,还是不能正确显示正确的VC?

// MARK: - Actions 
@IBAction func viewProfileButtonPressed(_ sender: UIButton) { 
    let stb = UIStoryboard(name: "Main", bundle: nil) 
    let sharkProfile = stb.instantiateViewController(withIdentifier: "sharkProfile") as! SharkProfileTableViewController 
    self.present(sharkProfile, animated: true) { 
     // add tab bar here? 
    } 
} 

TabBarController.swift

UploadConfirmationViewController.swift

+0

你想在这里发生什么?你是否开始使用Tab Bar,点击“viewProfile”,然后你想显示“SharkProfile”*代替该Tab的当前视图?你想让“SharkProfile”像典型的导航控制器一样从右侧滑入吗?或者你想让“SharkProfile”拥有自己的单独标签栏? – DonMag

+0

该应用程序以标签栏开始 - 但是然后我呈现一个没有标签栏的模式视图。在这个模式视图中,点击viewProfile应该显示SharkProfile,因为它在Main.storyboard中(带有一个标签栏)。 – pmanning

回答

0

你需要做的是出现在标签栏视图控制器,而不是嵌入它的视图控制器

0

一种方法是创建一个委托协议,允许点击View Profile按钮“回调”到提供它的视图控制器。当收到该回调时,VC会设置“当前”选项卡。

它会是这个样子:

// define "call back" delegate protocol 
protocol EncounterUploadedDelegate : class { 
    func didTapSharkProfileButton() 
} 

Encounter视图控制器都必须遵守该协议:

class EncounterViewController: UIViewController, EncounterUploadedDelegate { 

    // the normal stuff for this VC and all the other code for it 
    // ... 

    // conform to the protocol 
    func didTapSharkProfileButton() -> Void { 
     // when we get this call-back, switch to the Shark Profile tab 
     // tabs are zero-based, so assuming "SharkProfile" is 
     // is the 4th tab... 
     self.tabBarController?.selectedIndex = 3 
    } 

    // assuming a "Show Modal" segue named "ShowEncounterUploadSegue" is used 
    // to present the Modal View 
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
     if segue.identifier == "ShowEncounterUploadSegue" { 
      let vc = segue.destination as! TabModalVC 
      vc.encounterDelegate = self 
     } 
    } 

} 

视图控制器来呈现,模式:

class TabModalVC: UIViewController { 
    weak var encounterDelegate: EncounterUploadedDelegate? 

    @IBAction func profileButtonTapped(_ sender: Any) { 
     // dismiss self (the modal view) 
     self.dismiss(animated: true, completion: nil) 
     // this will call back to the delegate, if one has been assigned 
     encounterDelegate?.didTapSharkProfileButton() 
    } 

} 

希望一切都有道理:)

相关问题