2016-08-26 2 views
1

動的に作成されたボタンでカスタムアラートビューを作成しました。関数内でプログラムで作成されたボタンを押したときにコードを実行します。

それぞれのボタンを何か違うものにする方法がわからない限り、すべてうまく動作します。

私が意味するのは、関数addButtonを呼び出し、ブロックのコードを(UIAlertAction()のように)渡すということです。私はボタンを押した後にそのコードを実行したいが、ボタンがロードされると実際に実行される。

ご迷惑をおかけして申し訳ありませんが、私の問題に関する具体的な条件はわかりません。基本的に私はUIAlertViewを再現しようとしましたが、私は完全にリメイクする方法がわかりませんUIAlertAction。ここで

メソッドです(注:これは、カスタムクラスにあります):

func buttonPressed(actions actions:() -> Void) { 
    actions() // How do I get the actions from addButton? 
} 

そして、私はaddButtonを呼び出すとき、これは次のとおりです。

func addButton(buttonNumber number: Int, title: String, color: UIColor, actions:() -> Void) { 
    // Initialization 
    let button = UIButton(frame: CGRectMake(5, self.wrapper.bounds.height-CGFloat(55*number), self.wrapper.bounds.width-10, 50)) 
    // Configuration 
    button.setTitle(title, forState: .Normal) 
    button.backgroundColor = color 
    button.layer.cornerRadius = 10 
    button.addTarget(self, action: #selector(myCustomView.buttonPressed(actions:)), forControlEvents: .TouchUpInside) 

    wrapper.addSubview(button) 
} 

これはセレクタである(私はそれが正しくないことを知っています) :

 let alertView = UIStoryboard(name: "Popups", bundle: nil).instantiateViewControllerWithIdentifier("HAlertView") as! HAlertViewVC 
     prepareAlert(alertView) 

     alertView.loadAlert(title: "Hold on", message: "You need to add at least the goal before saving", image: "Warning-Icon", numberOfActions: 1) 

     alertView.addButton(buttonNumber: 1, title: "Close", color: UIColor(red: 246.0/255.0, green: 71.0/255.0, blue: 71.0/255.0, alpha: 1), actions: { 
    //  alertView.dismiss() // This is the problem. That's just a function that hides the alert view but the issue is that it gets executed right away. 
     }) 

答えて

1

あなたが探しているのは閉鎖です:

var some_code_block = { 
     print("my awesome code block!") 
    } 

func addCode() { 
    // Wont execute: 
    some_code_block = { print("new code inserted") } 
} 

func buttonPressed() { 
    // Executes: 
    some_code_block() 
} 

閉鎖(変数名)の末尾に「)(」あなたは/実行するだけで追加することにより、自由にブロックを変更することができ、より高/グローバルスコープでこれを入れて個人的には、配列/辞書を使用してコードブロックを保存/更新することができます。インデックス/キーを繰り返し処理して、必要なコードブロックを取得できます。

https://developer.apple.com/library/mac/documentation/Swift/Conceptual/Swift_Programming_Language/Closures.html

あなたの問題は異なるボタンを作り、実際にある場合は、配列や辞書

[UIButton]

にUIボタンの新しく作成したインスタンスを格納することができ、アレイ/辞書コードブロックの配列/辞書とのマッチングは確かにうまくいくはずです...それらが正しいスコープにあることを確認し、インデックス/キーを同期させるだけです。あなたはUIButtonのインスタンスを保持しているクラス/構造体、およびコードブロックを作成することができ

または、その配列/ディクショナリにクラス/構造体を入れ

// Make your own initializer for your purposes 
struct ButtonStuff { 
    var button: UIButton? 
    var code_block:()? 
} 

// Make empty array that holds type ButtonStuff (global/outer scope) 
var buttons: [ButtonStuff] = [] 

func addButton(pram1:Pram1, pram2:Pram2) { 
    // Make a new button (called locally inside of a func) 
    var new_button = ButtonStuff() 

    // Put stuff in here... 
    // new_button.button = pram1 
    // new_button.code_block = pram2 

    // Add our locally made new_button to the Global buttons array 
    buttons.append(new_buttons) 
} 

注:pram1、pram2が可能コードブロックに格納するには(実行せずに)()と入力します。コードブロックを自動的に実行する場合は、タイプ()->()

を使用します(オプションを使用する必要はありません)。簡単な例ですあなたはちょうどいくつかのロジック/ algoを右のボタンを見つけて表示し、コードブロックを呼び出す必要があります。ご質問があれば

、私の答えを編集するために私を必要とし、@fluidityで

+0

おかげでたくさんの人がコメントにメッセージを残してください!それは今働く。 – Lawrence413

+0

NP!私はまだ新しい自分。お役に立てて嬉しいです – Fluidity

関連する問題