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

时间:2018-10-07 22:24:07

标签: swift xcode

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

但是,我发现没有一种内置的方法可以隔离UIView。我已经在UIView类上创建了一个Protocol / Delegate,但是似乎无法实际更改视图。下面是我的代码:

UIView.swift(信息窗口的控制器)

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 个答案:

答案 0 :(得分:0)

1-这应该在包含自定义笔尖视图的vc内部(假设它是<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center" tools:context=".MainActivity"> <ImageView android:id="@+id/dice1_imageview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/die1" tools:ignore="contentDescription" /> <ImageView android:id="@+id/dice2_imageview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/die2" tools:ignore="contentDescription" /> <Button android:id="@+id/roll_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/rollButton" android:textStyle="bold" /> <TextView android:text="@string/playerScoreText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="18sp" android:textStyle="bold" /> <TextView android:id="@+id/playerScore_textview" android:text="@string/zero" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="16sp"/> <TextView android:text="@string/houseScoreText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="18sp" android:textStyle="bold"/> <TextView android:id="@+id/houseScore_textview" android:text="@string/zero" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="16sp" /> </LinearLayout>

CustomViewController

2-当实例化override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if segue.identifier == "displayLocaleData" { let destinationVC = segue.destination as! InfoViewController destinationVC.selectedItemName.text = nameField.text! } } 的实例时,需要将委托设置为该MapMarkerWindow

CustomViewController

3-在此处通过代表

let windoinfo = /// 
windowInfo.delegate = self