【问题标题】:How to execute func present in a ViewController from another?如何从另一个执行 ViewController 中存在的 func?
【发布时间】:2020-08-26 04:03:25
【问题描述】:

我是 Swift 的初学者,我还不了解所有的元素。

我正在尝试从另一个 ViewController (ProjectInfosViewController) 执行 ViewController (ProjectTabBarController) 中存在的函数。当我从第二个开始执行函数时,我最终遇到了错误。

对于上下文,它是属于一个UITabBarViewController的3个UIViewController的导航按钮,本身嵌入在一个UINavigationController中

提前谢谢你! (对不起我的英语不好)

import UIKit

//MARK:-TAB CONTROLLER
class ProjectTabBarController: UITabBarController {

    @IBOutlet weak var ui_saveButton: UIBarButtonItem!

    override func viewDidLoad() {
        super.viewDidLoad()
    }
    @IBAction func saveAction(_ sender: Any) {
        // code
        disableSaveButton() // ALL IT'S FINE HERE
    }
    func enableSaveButton() {
        ui_saveButton.title = "Save"
        ui_saveButton.isEnabled = true
    }
    func disableSaveButton() {
        ui_saveButton.title = "Saved"
        ui_saveButton.isEnabled = false
    } }

//MARK:-PROJECT INFORMATIONS

class ProjectInfosViewController: UIViewController, UITextFieldDelegate {

    let superController = ProjectTabBarController()

    override func viewDidLoad() {
        super.viewDidLoad()

    }
    func textFieldDidChangeSelection(_ textField: UITextField) {
        superController.enableSaveButton() // BUT HERE, DOESN'T
    } }

//MARK:-PROJECT FIXTURES

class ProjectFixturesViewController: UIViewController, UITableViewDataSource {
     }

//MARK:-PROJECT CONTACT

class ProjectContactViewController: UIViewController {
     }

【问题讨论】:

    标签: swift storyboard viewcontroller uibarbuttonitem


    【解决方案1】:

    这个

    let superController = ProjectTabBarController()
    

    是一个没有出现的新vc,如果vc在tabController里面那么做

    let res = self.tabBarController as! ProjectTabBarController
    res.......// call what you need
    

    【讨论】:

    • 感谢您的帮助,我明白了(我想!)
    猜你喜欢
    • 1970-01-01
    • 2015-08-04
    • 1970-01-01
    • 2015-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多