gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 12:44:46 26 4
gpt4 key购买 nike

我有一个只有三个按钮的自定义警报 Controller ,我有三个按钮取消发送添加,但我想要如下所示的单个完成处理程序。

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.
}

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

最佳答案

只需粘贴我的代码即可。

//
// 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.
}
}

关于ios - 全局使用单个完成处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41774290/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com