-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;
}
}
}
}
내가 내 홈페이지보기 의정서는 경고를 표시하는 기능을 수행 does'nt 왜
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)
}
에서 호출이에게 있습니다.
즉시 프로토콜이 실행될 때 당신이 프로토콜 RequestProtocol
에 부합하는 당신의 ViewController
클래스에서 일부 기능
FYI -'API '는보기 컨트롤러 하위 클래스에 대한 끔찍한 이름입니다. – rmaddy
무엇이'RegistroController'이고'self'가 아닌 왜 델리게이트를 설정해야합니까? 그리고 왜'API'가'UIViewController'를 상속합니까? – JuicyFruit
거의 모든 경우에 'self'인 프로토콜을 채택하는 클래스에 위임자를 설정해야합니다. 그리고'receiverDelegateRequestRegistro'가 스토리 보드의 컨트롤러 인스턴스가 아닌 ** 가능성이 가장 높다고 생각하십시오. – vadian