以编程方式将Tab栏添加到ViewController

时间:2017-06-21 18:56:29

标签: ios swift xcode uitabbarcontroller uistoryboard

我的TabBarController文件中有一个Main.storyboard。 在我的Upload.storyboard中,我将从Main.storyboard文件中呈现一个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

2 个答案:

答案 0 :(得分:0)

您需要做的是显示标签栏视图控制器,而不是嵌入在其中的视图控制器

答案 1 :(得分:0)

一种方法是创建一个代理协议,允许点击View Profile按钮以回拨"到呈现它的View Controller。收到回调后,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()
    }

}

希望一切都有道理:)