2017-01-21 29 views
0

私は唯一の三つのボタンで自分のカスタムアラートのコントローラを持っていると私はをキャンセル三つのボタンがあり、を送信し、を追加しますが、私は以下のような単一完了ハンドラを持っていると思います。使用単一完了ハンドラグローバル

self.showAlertWithButtonClicked(clickedButton: { (clickedButton) -> Void in 
     //check the button index by clickedButton.tag 

     }) 

しかし、私は私のAlertControllerクラスにこれを実装しようとしたとき、それは私を許可していません。以下 は私のMyAlertControllerワイヤーフレームコードです:

class MyAlertController: UIViewController { 


typealias CompletionHandler = (_ clickedButton: UIButton) -> Void 
override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view. 
} 

func showAlertWithButtonClicked(clickedButton: @escaping (MultipleMethodRunningHandle)) { 
    //Do what you want 

} 

func pressedCancelButton(button: UIButton) { 
    //At this point compiler doesn't allow me to use that completionHandler globally in the class 
    button.tag = 0 
    clickedButton(button) 
} 
func pressedSendButton(button: UIButton) { 
    //At this point compiler doesn't allow me to use that completionHandler globally in the class 
    button.tag = 1 
    clickedButton(button) 
} 
func pressedAddButton(button: UIButton) { 
    //At this point compiler doesn't allow me to use that completionHandler globally in the class 
    button.tag = 2 
    clickedButton(button) 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 

私は、クラス全体でこれらのハンドラにアクセスできるように、どのように私はこれを達成することができます。

答えて

0

私のコードを貼り付けてください。

// 
// ViewController.swift 
// Touristan 
// 
// Created by Syed Qamar Abbas on 21/01/2017. 
// Copyright © 2017 Syed Qamar Abbas. All rights reserved. 
// 

import UIKit 

typealias MultipleMethodRunningHandle = (_ clickedButton: UIButton) -> Void 

class ViewController: UIViewController { 

    var buttonClicked: MultipleMethodRunningHandle? 

    override func viewDidLoad() { 
     super.viewDidLoad() 
    } 

    func showAlertWithButtonClicked(clickedButton: @escaping (MultipleMethodRunningHandle)) { 
     //Do what you want 
     self.buttonClicked = clickedButton 
    } 

    func pressedCancelButton(button: UIButton) { 
     //Do what you want 
     button.tag = 0 
     self.buttonClicked!(button) 
    } 

    func pressedSendButton(button: UIButton) { 
     //Do what you want 
     button.tag = 1 
     self.buttonClicked!(button) 
    } 

    func pressedAddButton(button: UIButton) { 
     //Do what you want 
     button.tag = 2 
     self.buttonClicked!(button) 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 
} 
関連する問題