在Swift中移动到下一个视图之前验证信息

时间:2016-02-28 15:33:42

标签: mysql swift synchronous

我有一个应用程序需要在允许我的iOS应用程序进入下一个视图之前从数据库验证一些信息(邮政编码)。我使用邮政编码项目导入数据库表将所有有效的美国邮政编码,并且我希望在我允许它们继续之前由用户验证的邮政编码。如果邮政编码无效,我会在当前视图中将其保留并显示警告。我有一个类来验证邮政编码,但邮政编码在加载下一个视图之前不会被验证。我一直倾向于使用完成处理程序,但我不确定这是否是我最好/唯一的选择。提前致谢。 编辑: 以下是检索数据的整个类

protocol ZipCodeLocationProtocol: class {
    func zipCodeLocationDownloaded(zipLocation: Location)
} 

class RetrieveZipCodeLocation: NSObject, NSURLSessionDataDelegate {

// MARK: Properties
weak var delegate: ZipCodeLocationProtocol!
var data: NSMutableData = NSMutableData()


let urlPath: String = "xxxx"

func downloadZipCodeLocation(zipcode: Int)  {

        let path = self.urlPath + "?zipcode=\(zipcode)"
        let url: NSURL = NSURL(string: path)!
        var session: NSURLSession!
        let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
        session = NSURLSession(configuration: configuration, delegate: self, delegateQueue: nil)

        let task = session.dataTaskWithURL(url)

        task.resume()


}

func URLSession(session: NSURLSession, dataTask: NSURLSessionDataTask, didReceiveData data: NSData) {

    self.data.appendData(data)

}

func URLSession(session: NSURLSession, task: NSURLSessionTask, didCompleteWithError error: NSError?) {

    if error != nil {
        print("Failed to download data")
    }else {
        print("Data downloaded")
        self.parseJSON()
    }

}

func parseJSON() {
        var jsonResult: NSMutableArray = NSMutableArray()

        var location = Location(title: "TITLE", coordinate: CLLocationCoordinate2D(latitude: 0, longitude: 0))


        do {
            jsonResult = try NSJSONSerialization.JSONObjectWithData(self.data, options:[]) as! NSMutableArray

        } catch let error as NSError {
            print(error)

        }

        var jsonElement: NSDictionary = NSDictionary()

        for(var i = 0; i < jsonResult.count; i++) {

            jsonElement = jsonResult[i] as! NSDictionary

            let point = CLLocationCoordinate2D(latitude: (jsonElement["LATITUDE"] as! NSString).doubleValue, longitude: (jsonElement["LONGITUDE"] as! NSString).doubleValue)
            // Get Information
            location = Location(title: "TITLE", coordinate: point)



            self.delegate.zipCodeLocationDownloaded(location)
        }

}

1 个答案:

答案 0 :(得分:1)

我将假设按钮触发segue到下一个视图。我也会假设按钮连接到目标动作的功能。我也会假设你有代码来获取邮政编码,否则你不得不另外提出一个问题。

除了假设,您需要在点击按钮时显示UIAlertController 而不是转到下一个视图控制器。为了做到这一点:

func buttonAction() {

    if verifyZipCode() {

        let alert = UIAlertController(title: "Hold Up", message: "That zip code is invalid.", preferredStyle: .Alert)

        let fixIt = UIAlertAction(title: "Fix It!", style: .Default, handler: nil) // handler could also contain code to make text field red or something interesting

        alert.addAction(fixIt)

        presentViewController(alert, animated: true, completion: nil)

    } else {
        // existing segue code
    }

}

func verifyZipCode() -> Bool {
    // Take text field text and verify zip code
}