2017-11-13 11 views
1

UITextViewと2つのボタンがあるビューが表示されるクラスを作成しました。ボタンをタップに反応しない以外はすべて動作します。私のクラスの関連するコードは以下の通りです:iOSスウィフトボタンが表示されない

class FullPrompt:UIViewController { 
    var canceledPressedCallback:(()->()?)?=nil 
    var okPressedCallback:((_ txt: String)->()?)? = nil 
    var popupView = UIView() 
    var field = UITextView() 
    var parentView = UIView() 

    func prompt(_ message:String,view: UIViewController,numberInput: Bool, callback: @escaping (_ txt: String)->()) { 
     prompt(message, view: view, numberInput: numberInput, callback: callback,cancelCallback: nil,defaultText: "") 
    } 

    func prompt(_ prompt:String,view: UIViewController,numberInput: Bool, callback: @escaping (_ txt: String)->(), cancelCallback: (()->())?,defaultText: String) { 
     canceledPressedCallback=cancelCallback 
     okPressedCallback=callback 

     let cancelButton=UIButton(frame: CGRect(x: 10, y: y, width: 70, height: 32)) 
     cancelButton.setTitle("Cancel", for: .normal) 
     cancelButton.addTarget(self, action: #selector(cancelButtonPressed(_:)), for: .touchUpInside) 
     popupView.addSubview(cancelButton) 
     let okButton=UIButton(frame: CGRect(x: 210, y: y, width: 70, height: 32)) 
     okButton.setTitle("Ok", for: .normal) 
     okButton.setTitleColor(UIColor.blue, for: .normal) 
     okButton.addTarget(self, action: #selector(okButtonPressed(_:)), for: .touchUpInside) 
     popupView.addSubview(okButton) 
     view.view.addSubview(popupView) 
    } 

    func cancelButtonPressed(_ sender: UIButton) { 
     popupView.removeFromSuperview() 
     if canceledPressedCallback != nil { 
      canceledPressedCallback!() 
     } 
    } 

    func okButtonPressed(_ sender: UIButton) { 
     popupView.removeFromSuperview() 
     okPressedCallback!(field.text) 
    } 
} 

私は間違っていますか?

+0

あなたのビューには有用なフレームがありません。 – rmaddy

+0

このVCをインスタンス化して表示する方法がわかりませんが、このVCから呼び出し元VCのビュー階層にボタンを追加しているようです。これを行う場合は、両方のVCが何が起こっているかを知るように、特定のメソッドを呼び出す必要があります(そうでなければ、接触が認識されないなど)。詳細については、[この回答](https://stackoverflow.com/a/28852897/3985749)を参照してください。 – pbasdf

答えて

0

私はそれを理解しました。私の間違いは、クラス内にビュープロパティ(popupView)を追加することでした。私のクラスはUIViewControllerのサブクラスなので、別のビューを作成するのではなく、クラスのビュープロパティにサブビューを追加する必要がありました。ですから、基本的に、popupViewに何かを追加したところで、私はself.viewに置き換えました。彼らはボタンが正常に働いた。

関連する問題