26643


protocolとdelegateを利用して、他View内の関数を実行したいと考えています。

【やりたいこと】  
`PlacePickerView`で都道府県を選択した時に、`NoTeamView`内の`TextField`と`Button`のラベルをその都道府県に変更したい。

【現状】  
シミュレータは起動するが、`pickerview`を選択した際、  
`print("tete\(placeValues[row])")`は効いているものの、  
`print("this\(selectedValue)")`と`print("test")`が効かない。

NoTeamView.swift

[code]

    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")
        }
    }
    
[/code]

PlacePickerView.swift

[code]

    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()
        }
    }
    
[/code]


CC BY-SA 3.0
2016-06-10T16:41:19.927
26637
2016-06-11T01:45:44.560
2016-06-11T01:42:40.683
5337
15277
question
swift swift2
protocolとdelegateによる別View内の関数実行


あなたが現在提示されているコードには`PlacePickerView`クラスのインスタンスである`placePickerView`の`passPlaceDelegate`をセットするコードがどこにも見当たりません。従って:

[code]

    self.passPlaceDelegate?.setSelectedPlace(placeValues[row])
    self.passPlaceDelegate?.test()
    
[/code]

が実行される時には`self.passPlaceDelegate`は`nil`なのですから、`?.`で指定された`setSelectedPlace(placeValues[row])`も`test()`も呼び出されることはありません。

あなたのコードの書き方だと`setupCustomView()`の処理に1行付け加えるのが適当だと思います。

[code]

    placePickerView.passPlaceDelegate = self
    
[/code]


CC BY-SA 3.0
2016-06-10T21:51:26.773
26643
2016-06-11T01:45:44.560
2016-06-11T01:45:44.560
5337
13972
26637
answer