let title: String = "您的開獎時間爲" let time: String = "2017-10-23 12:23:18" let count_down: NSString = "6" as NSString var countdown = count_down.integerValue let timeout: String = "開獎時間已超時,請從新獲取" let alertVc = UIAlertController.init(title: nil, message: title + "\n" + time, preferredStyle: UIAlertControllerStyle.alert) let alertAction0 = UIAlertAction.init(title: "取消", style: .default, handler: { (action) in }) let alertAction1 = UIAlertAction.init(title: "肯定(\(countdown))", style: .default, handler: { (action) in //肯定的操做 }) alertVc.addAction(alertAction1) alertVc.addAction(alertAction0) weakSelf?.present(alertVc, animated: true, completion: { }) if countdown != 0 { let queue: DispatchQueue = DispatchQueue.global() let countdownTimer = DispatchSource.makeTimerSource(flags: [], queue: queue) countdown = countdown + 1 countdownTimer.scheduleRepeating(deadline: .now(), interval: .seconds(1)) countdownTimer.setEventHandler(handler: { countdown = countdown - 1 if countdown <= 0 { countdownTimer.cancel() DispatchQueue.main.async { alertAction1.setValue("肯定(0)", forKey: "title") alertAction1.setValue(UIColor.hrgb("CCCCCC"), forKey: "titleTextColor") alertAction1.isEnabled = false // message let one: NSString = "\(title)\n\(time)\n" as NSString let two: NSString = "\(timeout)" as NSString let message = "\(title)\n\(time)\n\(timeout)" let alertControllerMessageStr = NSMutableAttributedString(string: message) alertControllerMessageStr.addAttribute(NSForegroundColorAttributeName, value: UIColor.hrgb("DF503C"), range: NSMakeRange(one.length, two.length)) alertControllerMessageStr.addAttribute(NSFontAttributeName, value: UIFont.systemFont(ofSize: 13), range: NSMakeRange(one.length, two.length)) alertVc.setValue(alertControllerMessageStr, forKey: "attributedMessage") } }else { DispatchQueue.main.async { alertAction1.setValue("肯定(\(countdown))", forKey: "title") } } }) countdownTimer.resume() }