gpt4 book ai didi

ios - 通过辅助方法呈现 View Controller

转载 作者:行者123 更新时间:2023-11-30 13:56:54 25 4
gpt4 key购买 nike

我目前正在编写一个助手来包装我的 alamofire 请求。并且想知道当我从我的助手类收到消息时如何呈现alertviewcontroller,或者如何从我的助手类与我应该呈现此alertviewcontroller的 View Controller 进行通信?

以下是我所拥有的,如果您发现我做错了,请提出任何建议,因为这是我第一次编写 api 包装器。

import UIKit
import Alamofire
import SwiftyJSON
import SVProgressHUD

class AlamoHelper: NSObject {

class func request(method: Alamofire.Method, url: URLStringConvertible, var parameters: [String: AnyObject]? = nil) -> Alamofire.Request {

parameters!["os"] = "ios"
if let versionString = NSBundle.mainBundle().infoDictionary?["CFBundleShortVersionString"] as? String {
parameters!["v"] = versionString
}
SVProgressHUD.showWithMaskType(SVProgressHUDMaskType.Black)
return Alamofire.request(method, url, parameters: parameters, encoding: .JSON)

.responseJSON {
response in

SVProgressHUD.dismiss()

switch response.result {
case .Success:
let json = JSON(response.result.value!)

if let message = json["message"].string {
let alert = UIAlertController(title: json["subject"].stringValue, message: message, preferredStyle: .Alert)
let okAction = UIAlertAction(title: "OK", style: .Cancel, handler: nil)
alert.addAction(okAction)
}

if json["status"].intValue == 401 {
AppDelegate.isLoggedIn = false
AppDelegate.removeCookies()
}

break
case .Failure:
break
}

}

}

}

最佳答案

使用delegation将信息从 AlamoHelper 传递给调用者 UIViewController

所以,

  1. 使用某种方法创建一个协议(protocol),传递有用的信息,例如呈现的警报等。
  2. 创建委托(delegate)属性 AlamoHelper
  3. 在实例化您的 AlamoHelper 对象时 UIViewController,将 self 设置为 AlamoHelper 对象的委托(delegate)。
  4. UIViewController 中实现协议(protocol)方法。
  5. 最后,一旦请求返回,从 AlamoHelper 类中,调用委托(delegate)对象上的协议(protocol)方法。

关于ios - 通过辅助方法呈现 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33513633/

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