我目前正在编写我的第一个自定义框架,然而,在我的alamofire请求中,我无法返回我的报价。有什么建议吗?
从应用程序的角度来看,我可以附加到.responseJSON闭包中的数组,然后是tableView.reloadData()
如何将在.responseJSON闭包中的for循环中创建的数组传递回使用此框架的视图控制器?
很抱歉这个新手的问题,因为这是我写的第一个框架
public class func getVoucherList() {
Alamofire.request(.GET, "http://surprise.com/api/vouchers", parameters: nil, encoding: .JSON, headers: ["Content-Type": "application/x-www-form-urlencoded", "Authorization": "Token " + token, "accept": "application/vnd.audaxy.com.v1"])
.responseJSON { response in
switch response.result {
case .Success:
let json = JSON(response.result.value!)
let alert = UIAlertController(title: json["status"].string, message: json["message"].string, preferredStyle: .Alert)
let okAction = UIAlertAction(title: "Ok", style: .Cancel, handler: nil)
alert.addAction(okAction)
/*
for surprise in json["surprise"].arrayValue {
offers.append(Offer(offerId: String(surprise["offerid"].intValue), thumbnailUrl: surprise["thumbnail"].string, title: surprise["merchantname"].stringValue, description: surprise["deals"].stringValue, expiryDate: surprise["expire"].stringValue, coverPhotoUrl: surprise["offerimg"].string))
}
*/
VAV.getCurrentViewController().presentViewController(alert, animated: true, completion: {
print(offers)
})
break
case .Failure:
break
}
}
}
getCurrentViewController如下所示
class func getCurrentViewController() -> UIViewController {
var topViewController = UIApplication.sharedApplication().keyWindow?.rootViewController
while ((topViewController?.presentedViewController) != nil) {
topViewController = topViewController?.presentedViewController
}
return topViewController!
}
发布于 2015-12-09 12:27:28
定义回调(getVoucherList:((offers: Offer)->Void)?){...}
完成for循环后,调用回调函数并将数组传回。
现在,在视图控制器中,您可以对其执行任何操作(例如,将其附加到表数据数组中并重新装入表,等等)
发布于 2015-12-09 04:57:36
当你呈现任何UIViewController
时,你可以直接传递数据。为此,您需要重写prepareForSegue
方法。看看下面的例子:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "YOUR_CONTROLLER_IDENTIFIER" {
if let destination = segue.destinationViewController as? YourDestinationController {
destination.arroffers = offers
}
}
}
这就是将数据传递给presenting UIViewController
的方法。
https://stackoverflow.com/questions/34170926
复制相似问题