-1
我有一个协议自定义协议斯威夫特
import UIKit
import Alamofire
import SwiftyJSON
protocol RequestProtocol: class {
func RequestConnection(json: JSON, status: Int, Message: String)
}
class API: UIViewController {
var delegate: RequestProtocol?
var json: JSON = []
override func viewDidLoad() {
super.viewDidLoad()
}
func RequestConnection() {
Alamofire.request(variablesClass.url).responseJSON { (response) -> Void in
switch response.result {
case .success:
let result = response.result.value
if response.response?.statusCode == 200 {
self.json = JSON(result!)
DispatchQueue.main.async(execute: {
self.delegate?.RequestConnection(json: self.json, status: (response.response?.statusCode)!, Message: "\(self.json["Message"])")
})
} else {
self.json = JSON(result!)
DispatchQueue.main.async(execute: {
self.delegate?.RequestConnection(json: self.json, status: (response.response?.statusCode)!, Message: "\(self.json["Message"])")
})
}
break;
case .failure(let error):
print(error)
break;
}
}
}
}
我有它在我的主视图
class ViewController: UIViewController, RequestProtocol {
func RequestConnection(json: JSON, status: Int, Message: String) {
func showAlertView()
}
override func viewDidLoad() {
super.viewDidLoad()
var senderDelegateRequest = API()
var receiverDelegateViewController = ViewController()
senderDelegateRequest.delegate = receiverDelegateRequestRegistro
senderDelegateRequest.RequestConnection()
}
func showAlertView(){
let alertView = UIAlertController(title: "You need to log in first", message: "To access the special features of the app you need to log in first.", preferredStyle: .alert)
alertView.addAction(UIAlertAction(title: "Login", style: .default, handler: { (alertAction) -> Void in
}))
alertView.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
present(alertView, animated: true, completion: nil)
}
为什么议定书一点儿也不执行函数来显示警报调用。
有一些方法,一旦在执行协议可以执行某些功能
仅供参考 - “API”是视图控制器子类的可怕名称。 – rmaddy
'RegistroController'是什么,为什么你设置委托给它而不是'self'?为什么'API'继承'UIViewController'? – JuicyFruit
您必须将委托设置为采用几乎所有情况下都是“self”的协议的类。并且认为'receiverDelegateRequestRegistro'最有可能是**而不是故事板中的控制器实例。 – vadian