全局使用单个完成处理程序

时间:2017-01-21 00:05:38

标签: ios swift completionhandler

我有一个只有三个按钮的自定义警报控制器,我有三个按钮取消发送添加,但我想拥有像下面这样的单一完成处理程序。

<button>button button <br/> button</button>

但是当我尝试将它实现到我的AlertController类中时,它并不允许我。 下面是我的MyAlertController WireFrame代码:

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

        })

如何实现这一点,以便我可以访问整个班级中的那些处理程序。

1 个答案:

答案 0 :(得分: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.
    }
}