protocolとdelegateによる別View内の関数実行
protocolとdelegateを利用して、他View内の関数を実行したいと考えています。
【やりたいこと】
PlacePickerView
で都道府県を選択した時に、NoTeamView
内のTextField
とButton
のラベルをその都道府県に変更したい。
【現状】
シミュレータは起動するが、pickerview
を選択した際、
print("tete\(placeValues[row])")
は効いているものの、
print("this\(selectedValue)")
とprint("test")
が効かない。
NoTeamView.swift
import UIKit
import RealmSwift
class NoTeamView: UIView, UITextViewDelegate, UIPickerViewDelegate, PlacePickerViewDelegate {
var noTeamPlaceEditButton : UIButton = UIButton()
var noTeamPlaceTextField : UITextField = UITextField()
var placePickerView = PlacePickerView()
override init(frame:CGRect) {
super.init(frame:frame)
setupCustomView()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
setupCustomView()
}
func setupCustomView() {
noTeamPlaceTextField.placeholder = "活動地域"
noTeamPlaceTextField.inputView = placePickerView
self.addSubview(noTeamPlaceTextField)
noTeamPlaceEditButton.titleLabel!.font = UIFont(name: "HiraginoSans-W6",size: CGFloat(12))
noTeamPlaceEditButton.addTarget(self, action: "noTeamPushPlaceEditButton:", forControlEvents: .TouchUpInside)
self.addSubview(noTeamPlaceEditButton)
}
func setSelectedPlace(selectedValue: String){
print("this\(selectedValue)")
noTeamPlaceEditButton.setTitle("▼ \(selectedValue)", forState: .Normal)
noTeamPlaceTextField.text = "\(selectedValue)"
}
func test(){
print("test")
}
}
PlacePickerView.swift
import UIKit
@objc protocol PlacePickerViewDelegate {
func setSelectedPlace(selectedValue: String)
func test()
}
class PlacePickerView: UIPickerView, UIPickerViewDelegate {
weak var passPlaceDelegate:PlacePickerViewDelegate?
let placeValues = ["全国","北海道","青森","岩手","宮城","秋田","山形","福島","茨城","栃木","群馬","埼玉","千葉","東京","神奈川","新潟","富山","石川","福井","山梨","長野","岐阜","静岡","愛知","三重","滋賀","京都","大阪","兵庫","奈良","和歌山",
"鳥取","島根","岡山","広島","山口","徳島","香川","愛媛","高知","福岡","佐賀","長崎","熊本","大分","宮崎","鹿児島","沖縄"]
override func drawRect(rect: CGRect) {
// Drawing code
self.delegate = self
var indexPlaceVale = placeValues.indexOf("北海道")
self.selectRow(indexPlaceVale!, inComponent: 0, animated: true)
}
func pickerView(PickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
print("tete\(placeValues[row])")
self.passPlaceDelegate?.setSelectedPlace(placeValues[row])
self.passPlaceDelegate?.test()
}
}