我如何从 ios Swift 中另一个名为 SecondViewController.swift 的 Class 调用 UIView 中的方法或函数

How can i call a method or function in UIView from another Class called SecondViewController.swift in ios Swift

我的标记中有一个 MapInfoWindow,它来自一个带有标题和按钮的 .xib 文件 it.That 意味着我有一个来自 class:

的 UIView
    class MapInfoWindow: UIView {

    @IBOutlet weak var title: String!
    @IBOutlet weak var actionButton: UIButton!

    @IBAction var didTapButton (sender: Any) {

        print(button taped)
    }


    class func instanceFromNib() -> UIView {
        return UINib(nibName: "nib file name", bundle: nil).instantiateWithOwner(nil, options: nil)[0] as UIView
    }

}

现在我不会在我的按钮内调用另一个 viewController:

中的操作表警报的方法
 class FindUsViewController: UIViewController  {

    func showAlert() {

       let alert = UIAlertController(title: "Title", message: "Please Select an Option", preferredStyle: .actionSheet)

    alert.addAction(UIAlertAction(title: "Approve", style: .default , handler:{ (UIAlertAction)in
        print("User click Approve button")
    }))

    alert.addAction(UIAlertAction(title: "Edit", style: .default , handler:{ (UIAlertAction)in
        print("User click Edit button")
    }))

    alert.addAction(UIAlertAction(title: "Delete", style: .destructive , handler:{ (UIAlertAction)in
        print("User click Delete button")
    }))

    alert.addAction(UIAlertAction(title: "Dismiss", style: .cancel, handler:{ (UIAlertAction)in
        print("User click Dismiss button")
    }))

    self.present(alert, animated: true, completion: {
        print("completion block")
    })
}
}

经过一些研究,我发现最好的是这个,但不起作用:

@IBAction var didTapButton (sender: Any) {

FindUsViewController().showAlert()

    print(button taped)
}

调试中说有个问题我不是很理解

您正在创建 IBOutlet 而不是 IBAction。从 XIB 拖动到 swift 文件时,请确保将正在创建的出口类型更改为 Action 而不是出口。

插座只是对对象的引用。

你应该为此使用 delegate,试试看

public protocol nonPaidDelegate:  class {
    func showAlertView()
}
class MapInfoWindow: UIView {
    open var delegate:nonPaidDelegate?
    @IBAction var didTapButton (sender: Any) {

        print(button taped)
self.delegate?.showAlertView()
    }

}

下一步去viewcontroller

class FindUsViewController: UIViewController,nonPaidDelegate  {

 func showAlertView() {
        self. showAlert()

    }
}

并且不要忘记将委托设置为您的 viewcontroller class。(在显示地图视图的地方 window)

your map window view object.delegate = self