swift - 创建从 .xib 文件中的 UIButton 到 ViewController 的 Segue

标签 swift xcode

在我正在构建的应用程序中,我希望向他们展示用户区域的 map ,其中包含用自定义标记标识的某些地标。当用户点击标记时,会出现一个信息窗口,其中包含一些基本信息和一个显示“获取更多信息”的按钮。目标是当用户单击该按钮时,他们将被带到一个新的 ViewController。

然而,我发现没有内置的方法来隔离 UIView。我已经在 UIView 类上创建了一个协议(protocol)/委托(delegate),但似乎无法真正改变 View 。下面是我的代码:

UIView.swift(信息窗口的 Controller )

import UIKit

protocol MapMarkerDelegate: class {
    func didTapInfoButton(data: NSDictionary)
}

protocol MoreButtonTapped: class {
    func didClickMoreButton(name: String)
}



class MapMarkerWindow: UIView {
    @IBOutlet weak var nameField: UILabel!
    @IBOutlet weak var addressField: UILabel!
    @IBOutlet weak var openField: UILabel!

    var secondDelegate: MoreButtonTapped!
    weak var delegate: MapMarkerDelegate?
    var spotData: NSDictionary?


    @IBAction func didTapInfoButton(_ sender: UIButton) {
        delegate?.didTapInfoButton(data: spotData!)
    }

    class func instanceFromNib() -> UIView {
        return UINib(nibName: "MapMarkerWindowView", bundle: nil).instantiate(withOwner: self, options: nil).first as! UIView
    }

    @IBAction func infoButtonPressed(_ sender: Any) {
        if(secondDelegate != nil) {
            self.secondDelegate.didClickMoreButton(name: nameField.text!)
        }
        print("Info Button Tapped for \(nameField.text!)")
    }


    func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "displayLocaleData" {
            let destinationVC = segue.destination as! InfoViewController

            destinationVC.selectedBarName.text = nameField.text!
        }
    }
}

这是接收 VC:

import UIKit

class InfoViewController: UIViewController, MoreButtonTapped {




    @IBOutlet weak var selectedBarName: UILabel!


    @IBOutlet var selectedBarData: UIView!
    override func viewDidLoad() {
        super.viewDidLoad()
        //Assign delegate to self
        //        MapMarkerWindow.delegate = self
        selectedBarName.text = selectedBarName.text
    }

    func didClickMoreButton(name: String) {
        print("Success")
    }


}

我没有收到此代码的任何错误。如果我尝试添加 MapMarkerWindow.delegate = self,我会收到错误,因为 UIView 不具备该功能。任何想法或解决方法将不胜感激。

最佳答案

1- 这应该位于包含自定义 nib View 的 vc 内部(假设它是 CustomViewController )

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "displayLocaleData" {
        let destinationVC = segue.destination as! InfoViewController  
        destinationVC.selectedItemName.text = nameField.text!
    }
}

2- 当您实例化 MapMarkerWindow 实例时,您需要将委托(delegate)设置为该 CustomViewController

let windoinfo = /// 
windowInfo.delegate = self

3-在此处采用委托(delegate)

class CustomViewController: UIViewController, MoreButtonTapped {

   func didClickMoreButton(name: String) {
      print("Success")
      // here do self.performSegue.....
   } 
}

关于swift - 创建从 .xib 文件中的 UIButton 到 ViewController 的 Segue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52693441/

相关文章:

Swift//多个列表单元格中的相同图像已更改(每第 4 个单元格)

c++ - 文件在多个程序中持续无法打开

flutter - 在 xcode v 13.1 中存档应用程序时,命令 PhaseScriptExecution 失败并出现非零退出代码

ios - 填充单元格时无法展开可选

ios - 使用 CMake 的 Xcode 部署信息和图标

ios - 如何使用 Swift 在 XCode 上单击按钮时使分数上升一分

ios - 从另一个类捕获 View 中的事件

swift - CALayer 将 UILabel 隐藏在 UIView 之上

swift - 找不到“iTunesLibrary/ITLibDefines.h”文件

swift - 如何让一个 Sprite 节点围绕另一个节点旋转? (还有一条线)