在另一个viewcontroller中调用一个函数

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (87)

rootviewcontroller的导航菜单下面有三个选项卡,每个选项卡代表一个产品列表,在导航菜单中我有一个带有数字的购物车图标,以显示购物车中的图标数量。每个标签都是一个单独的viewcontrollerrootviewcontroller像这样引用:

override func viewControllers(for pagerTabStripController: PagerTabStripViewController) -> [UIViewController] {
    let layout = UICollectionViewFlowLayout()
    let firstTab = FirstTabController(collectionViewLayout: layout)
    let seccondTab = SeccondTabController(collectionViewLayout: layout)
    let thirdTab = ThirdTabController(collectionViewLayout: layout)

    return [firstTab ,seccondTab ,thirdTab ]
}

我创建了一个函数defaultviewcontroller来重置导航菜单中的购物车编号,但我不知道如何触发此firsttabController功能

这是功能 firsttabcontroller

func AddToCart(sender:UIButton) {
    let activityIndicatorView = NVActivityIndicatorView(frame: self.view.frame, type: .circleStrokeSpin, color: .red, padding: 170)
    self.view.addSubview(activityIndicatorView)
    activityIndicatorView.startAnimating()

    let parameters : [String: Any] = [
        "productid": sender.tag ,
        "quantityid": 1
    ]

    ApiServiceCart.sharedInstance.addProductToCartCatalog(parameters: parameters) { (success) in
        activityIndicatorView.removeFromSuperview()
        if success == true {
            // here i want to fire DefaultController().setupNavigationMenu()
            self.displayMessage(Title: MessageTitle.Successfull.rawValue, Message: Message.AddToCart_Successfull.rawValue)
        }
        else {
            self.displayMessage(Title: MessageTitle.Error.rawValue, Message: Message.AddToCart_Error.rawValue)
        }
    }
}

我尝试过协议委托,但它没有用

提问于
用户回答回答于

看看这个检查你是否使用了这个代表团。

class FirstViewController: UIViewController, UITextFieldDelegate, PassDataBackDelegate {
    @IBOutlet weak var colorButton: UIButton!
    @IBOutlet weak var firstViewLabel: UILabel!
    @IBOutlet weak var firstTextView: UITextField!

    let pinkColor = UIColor(red: 255/255, green: 19/255, blue: 102/255, alpha: 1.0)
    let blueColor = UIColor(red: 67/255, green: 156/255, blue: 255/255, alpha: 1.0)

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = pinkColor
        firstTextView.delegate = self
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    @IBAction func colorButtonPressed(_ sender: UIButton) {
        if view.backgroundColor == pinkColor {
            view.backgroundColor = blueColor
            colorButton.backgroundColor = pinkColor
        } else {
            view.backgroundColor = pinkColor
            colorButton.backgroundColor = blueColor
        }
    }

    @IBAction func sendButtonPressed(_ sender: UIButton) {
        performSegue(withIdentifier: "secondView", sender: self)
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "secondView" {
            let destinationVC = segue.destination as! SecondViewController
            destinationVC.delegate = self
            destinationVC.messagePassed = firstTextView.text
        }
    }

    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        performSegue(withIdentifier: "secondView", sender: self)
        resignFirstResponder()
        return true
    }

    func dataReceivedBack(messageRcvd: String) {
        firstViewLabel.text = messageRcvd
    }

}

和协议的第二个视图控制器:

protocol PassDataBackDelegate {
    func dataReceivedBack(messageRcvd: String)
}


class SecondViewController: UIViewController,UITextFieldDelegate {
    @IBOutlet weak var secondViewLabel: UILabel!
    @IBOutlet weak var secondTextField: UITextField!

    var delegate: PassDataBackDelegate?
    var messagePassed: String?

    override func viewDidLoad() {
        super.viewDidLoad()
        secondTextField.delegate = self
        if let message = messagePassed {
            secondViewLabel.text = message
        } else {
            print("NO DATA PASSED")
        }
    }

    func passDataBack(){
        if delegate != nil {
            let messageBack = secondTextField.text
            delegate?.dataReceivedBack(messageRcvd: messageBack!)
            self.dismiss(animated: true, completion: nil)
        }
    }

    @IBAction func sendBackButtonPressed(_ sender: UIButton) {
        passDataBack()
    }

    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        passDataBack()
        resignFirstResponder()
        return true
    }
}

所属标签

可能回答问题的人

  • 应用案例分享

    1 粉丝490 提问6 回答
  • o o

    4 粉丝495 提问5 回答
  • 找虫虫

    5 粉丝0 提问5 回答
  • uncle_light

    5 粉丝518 提问4 回答

扫码关注云+社区

领取腾讯云代金券