gpt4 book ai didi

swift - 如何从类内部调用 presentViewController

转载 作者:可可西里 更新时间:2023-10-31 23:54:52 26 4
gpt4 key购买 nike

在我的完成处理程序中,我试图返回 http 响应代码,就像 JS 中的 alert 一样。一旦我得到这个排序,我希望改进我的 if 语句来识别最终用户的连接问题。我正在看这篇文章。

http://www.ioscreator.com/tutorials/display-an-alert-view-in-ios8-with-swift

我也看到了这个答案Simple App Delegate method to show an UIAlertController (in Swift)但我得到了一个关于未命名的 window 的类似方法。

我没有所有噪音的代码是这样的:

class NewsViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

var tableView: UITableView!
var titleItems: NSMutableArray = []
var descritpionItems: NSMutableArray = []

class RemoteAPI {

// base url
let baseUrl = "http://api.dev/web/"
// returned array of news titles + descriptions
var newsTitle: NSMutableArray = []
var newsDescription: NSMutableArray = []

func getData(completionHandler: ((NSArray?, NSError?) -> Void)) -> Void {
// get news feed url
let url = NSURL(string: baseUrl + "news")
// create session
let session = NSURLSession.sharedSession()
// set task
let task = session.dataTaskWithURL(url!, completionHandler: { (data, response, error) -> Void in

// if error isn't nil return error to getData
if (error != nil) {
return completionHandler(nil, error)
}

// check response
if let httpResponse = response as? NSHTTPURLResponse {
// if response doesn't equal 200 throw an alert
if httpResponse.statusCode != 200 {
let alertController = UIAlertController(title: "Oh No!!", message: "Connection error",preferredStyle: UIAlertControllerStyle.Alert)

alertController.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.Default,handler: nil))

UIApplication.sharedApplication().keyWindow?.rootViewController?.presentViewController(alertController, animated: true, completion: nil)

} else { // no error continue

// convert data object to json
let json = JSON(data: data)

for var i = 0; i < json.count; ++i {
// get news title from json object
var title = json[i]["title"].string
var blurb = json[i]["description"].string
// add to dictionary
self.newsTitle.addObject(title!)
self.newsDescription.addObject(blurb!)
}

if (error != nil) {
return completionHandler(nil, error)
} else {
return completionHandler([self.newsTitle,self.newsDescription], nil)
}
}
}
})
task.resume()
}
}

// get data
var api = RemoteAPI()

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.view.frame = CGRect(x: 5, y: 75, width: 365, height: 480)
self.tableView = UITableView(frame:self.view!.frame)
self.tableView!.delegate = self
self.tableView!.dataSource = self
self.tableView!.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
self.view?.addSubview(self.tableView)

api.getData({data, error -> Void in
if (data != nil) {
self.titleItems = self.api.newsTitle
self.descritpionItems = self.api.newsDescription
self.tableView!.reloadData()
} else {
println("API failed :-(")
println(error)
}
})
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{

return self.titleItems.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{

var cell:UITableViewCell=UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "mycell")

if let newsTitle = self.titleItems[indexPath.row] as? NSString {
cell.textLabel?.text = newsTitle
}
if let newsDesc = self.descritpionItems[indexPath.row] as? NSString {
cell.detailTextLabel?.text = newsDesc
}
return cell
}

我得到一个错误信息:NewsViewController.RemoteAPI does not have member named presentViewController。我明白为什么,只是不确定如何在 iOS 的新类中调用它

最佳答案

在我这里的情况下,以下方法有效:

UIApplication.sharedApplication().keyWindow?.rootViewController?.presentViewController(alertController, animated: true, completion: nil)

关于swift - 如何从类内部调用 presentViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29323449/

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