Timer.scheduledTimer在swift 3

时间:2017-03-31 09:37:03

标签: ios iphone timer swift3

我的计时器无法正常工作......任何人都可以帮助我吗?

如果用户登录成功,我会使用计时器转换到下一页,但是计时器不起作用且没有错误消息......

do {
                        if let convertedJsonIntoDict = try JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary {
                            // Print out dictionary
                            print("convertedJsonIntoDict=\(convertedJsonIntoDict)")
                            print("convertedJsonIntoDict.長度=\(convertedJsonIntoDict.count)")
                            // Get value by key
                            if convertedJsonIntoDict.count > 1{
                                let cc_account = convertedJsonIntoDict["Account"] as! String
                                self.NPaccount = cc_account
                                print("工號=\(self.NPaccount)")
                                let cc_account_2 = convertedJsonIntoDict["Account2"] as! String
                                self.Account = cc_account_2
                                print("帳號=\(self.Account)")
                                let cname = convertedJsonIntoDict["CName"] as! String
                                self.Name = cname
                                print("姓名=\(self.Name)")
                                self.Member.account = self.Account
                                self.Member.password = self.PasswordTextvield.text!
                                self.Member.npaccount = self.NPaccount
                                self.Member.name = self.Name
                                self.Member.uuid = self.UUID
                                self.Member.creatdate = self.now
                                print("Saving data to context...")
                                appDelegate.saveContext()
                                self.segueflag = true
                                EZLoadingActivity.hide(true, animated: true)
                                _ = Timer.scheduledTimer(timeInterval: 1.5, target: self, selector: #selector(self.nextBtn(sender:)), userInfo: nil, repeats: false)
                            }else{
                                self.segueflag = false
                                print("segueflag...\(self.segueflag)")
                                if !self.segueflag{
                                    print("無此帳號資料...")
                                    EZLoadingActivity.hide(false, animated: true)
                                    //self.AccoubtTextvield.text = ""
                                    //self.PasswordTextvield.text = ""
                                }
                            }
                        }
                        else{
                            print("here")
                        }
                    } catch let error as NSError {
                        print(error.localizedDescription)
                    }

@IBAction func nextBtn(sender: AnyObject) {
        print("--------Go to next page!--------")
        if members.count == 0{
            SCLAlertView().showError("請先登入~~!!",subTitle:"")
            return
        }
        if !segueflag{
            SCLAlertView().showError("登入的帳號或密碼有誤~~!!",subTitle:"")
            return
        }
        self.performSegue(withIdentifier: "login", sender: self)
    }

任何人都可以告诉我可能的答案。提前谢谢....

1 个答案:

答案 0 :(得分:3)

Timer方法是 ios10 的更改。如果您在ios10中办理登机手续,可以使用以下代码。

   let systemVersion = UIDevice.current.systemVersion

        if Int(systemVersion)! >= 10 {
            _ = Timer.scheduledTimer(withTimeInterval: 1.5,
                                         repeats: false) {
                                            timer in
                                            //Put the code that be called by the timer here.
                                            print("Testing data")
            }
        }else{

            _ = Timer.scheduledTimer(timeInterval: 1.5, target: self, selector: #selector(self.nextBtn(sender:)), userInfo: nil, repeats: false)

        }