我在程序中有多个NSTimers,因为我需要它们在完成后更改标签的值。但是,每次我设置它时,它都会跳到我创建的最后一个。我一直在浏览这个论坛,但还没有找到任何与我的情况相似的东西,所以我已经在下面输入了代码。 我正在使用xcode 7和swift 2.0。
//
// ViewController.swift
// Mobile Streaming Countdown
//
// Created by Benjamin Peterson on 10/11/15.
// Copyright © 2015 Ben Peterson. All rights reserved.
//
import UIKit
import Foundation
import iAd
class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
@IBOutlet var imageview: UIImageView!
@IBOutlet var loadimage: UIButton!
@IBOutlet var step1: UILabel!
@IBOutlet var step2: UILabel!
@IBOutlet var oneminute: UIButton!
@IBOutlet var tenminutes: UIButton!
@IBOutlet var startingsoon: UIButton!
@IBOutlet var steo3: UILabel!
@IBOutlet var starttimer: UIButton!
@IBOutlet var numberOfMinutes: UILabel!
@IBOutlet var minutes: UILabel!
@IBOutlet var ThreeMinutes: UIButton!
let imagePicker = UIImagePickerController()
var tNumberOfMinutes = 0
var timer = NSTimer()
var timer2 = NSTimer()
var timer3 = NSTimer()
var timer4 = NSTimer()
var timer5 = NSTimer()
let timeInterval:NSTimeInterval = 10.0
var timeCount:NSTimeInterval = 10.0
let secondscountdown = 0
@IBAction func loadImage(sender: AnyObject) {
imagePicker.allowsEditing = false
imagePicker.sourceType = .PhotoLibrary
presentViewController(imagePicker, animated: true, completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
imagePicker.delegate = self
self.canDisplayBannerAds = true
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
imageview.contentMode = .ScaleAspectFit
imageview.image = pickedImage
}
dismissViewControllerAnimated(true, completion: nil)
}
func imagePickerControllerDidCancel(picker: UIImagePickerController) {
dismissViewControllerAnimated(true, completion: nil)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func oneMinute(sender: AnyObject) {
tNumberOfMinutes = 1
}
func timeString(time:NSTimeInterval) -> String {
let minutes = Int(time) / 60
//let seconds = Int(time) % 60
let seconds = time - Double(minutes) * 60
let secondsFraction = seconds - Double(Int(seconds))
return String(format:"%02i:%02i.%01i",minutes,Int(seconds),Int(secondsFraction * 10.0))
}
func timerDidEnd(timer:NSTimer){
//first iteration of timer
numberOfMinutes.text = "Ready!!"
}
func timerTwo(timer:NSTimer) {
numberOfMinutes.text = "1"
}
func timerThree(timer:NSTimer) {
numberOfMinutes.text = "2"
}
func timerFour(timer:NSTimer) {
numberOfMinutes.text = "3"
}
func timerFive(timer:NSTimer) {
numberOfMinutes.text = "4"
}
func timerSix(timer:NSTimer) {
numberOfMinutes.text = "5"
}
func timerSeven(timer:NSTimer) {
numberOfMinutes.text = "6"
}
func timerEight(timer:NSTimer) {
numberOfMinutes.text = "7"
}
func timerNine(timer:NSTimer) {
numberOfMinutes.text = "8"
}
func timerTen(timer:NSTimer) {
numberOfMinutes.text = "9"
}
@IBAction func tenMinutes(sender: AnyObject) {
tNumberOfMinutes = 10
}
@IBAction func startingsoon(sender: AnyObject) {
tNumberOfMinutes = 11
}
@IBAction func threeMinutes(sender: AnyObject) {
tNumberOfMinutes = 5
}
@IBAction func startTimer(sender: AnyObject) {
if tNumberOfMinutes > 0 {
step1.hidden = true
step2.hidden = true
loadimage.hidden = true
oneminute.hidden = true
tenminutes.hidden = true
startingsoon.hidden = true
steo3.hidden = true
starttimer.hidden = true
if tNumberOfMinutes == 1 {
timer = NSTimer.scheduledTimerWithTimeInterval(timeInterval,
target: self,
selector: "timerDidEnd:",
userInfo: "Pizza Done!",
repeats: false)
}
if tNumberOfMinutes == 5 {
//Part one is starting here
timer = NSTimer.scheduledTimerWithTimeInterval(timeInterval,
target: self,
selector: "timerTwo:",
userInfo: "this!",
repeats: false)
//Second Part is starting here
timer = NSTimer.scheduledTimerWithTimeInterval(timeInterval,
target: self,
selector: "timerThree:",
userInfo: "is",
repeats: false)
//Third Part is starting here
timer = NSTimer.scheduledTimerWithTimeInterval(timeInterval,
target: self,
selector: "timerFour:",
userInfo: "a",
repeats: false)
//Fourth Part is starting here
timer = NSTimer.scheduledTimerWithTimeInterval(timeInterval,
target: self,
selector: "timerFive:",
userInfo: "test",
repeats: false)
//Fifth Part is starting here
timer = NSTimer.scheduledTimerWithTimeInterval(timeInterval,
target: self,
selector: "timerSix:",
userInfo: "to",
repeats: false)
}
if tNumberOfMinutes == 10 {
//Part one is starting here
timer = NSTimer.scheduledTimerWithTimeInterval(timeInterval,
target: self,
selector: "timerTwo:",
userInfo: "see",
repeats: false)
//Second Part is starting here
timer = NSTimer.scheduledTimerWithTimeInterval(timeInterval,
target: self,
selector: "timerThree:",
userInfo: "if changing",
repeats: false)
//Third Part is starting here
timer = NSTimer.scheduledTimerWithTimeInterval(timeInterval,
target: self,
selector: "timerFour:",
userInfo: "these fixes",
repeats: false)
//Fourth Part is starting here
timer = NSTimer.scheduledTimerWithTimeInterval(timeInterval,
target: self,
selector: "timerFive:",
userInfo: "the",
repeats: false)
//Fifth Part is starting here
timer = NSTimer.scheduledTimerWithTimeInterval(timeInterval,
target: self,
selector: "timerSix:",
userInfo: "problems",
repeats: false)
//Part one is starting here
timer = NSTimer.scheduledTimerWithTimeInterval(timeInterval,
target: self,
selector: "timerSeven:",
userInfo: "in",
repeats: false)
//Second Part is starting here
timer = NSTimer.scheduledTimerWithTimeInterval(timeInterval,
target: self,
selector: "timerEight:",
userInfo: "the",
repeats: false)
//Third Part is starting here
timer = NSTimer.scheduledTimerWithTimeInterval(timeInterval,
target: self,
selector: "timerNine:",
userInfo: "code",
repeats: false)
//Fourth Part is starting here
timer = NSTimer.scheduledTimerWithTimeInterval(timeInterval,
target: self,
selector: "timerTen:",
userInfo: "derple",
repeats: false)
//Fifth Part is starting here
//timer = NSTimer.scheduledTimerWithTimeInterval(timeInterval,
//target: self,
//selector: "timerDidEnd:",
//userInfo: "flerple",
//repeats: false)
}
if tNumberOfMinutes == 11 {
numberOfMinutes.text = ("Show is starting soon!")
minutes.hidden = true
}
}
}
}
我没有使用计时器2-5,并计划摆脱它们,但我想包括它们以表明我尝试按时间间隔分配每个预定时间,但这不起作用任
提前感谢您的帮助!
答案 0 :(得分:1)
所有这些都在解雇,但因为计时器处理程序方法都在改变相同的变量,所以你只看到最后一次触发。
所以:
timerTwo将文本设置为1(然后立即设置)
timerThree将文本设置为2 ...
等...
等...
timerNine将文本设置为8
timerTen将文本设置为9
因为所有这些都发生得非常快,你只看到最后的结果。
在每个处理程序方法中放入一个print(x),你应该看到所有这些方法都是一个接一个地调用。
func timerTwo(timer:NSTimer) {
numberOfMinutes.text = "1"
print("timerTwo")
}
func timerThree(timer:NSTimer) {
numberOfMinutes.text = "2"
print("timerThree")
}
/// etc...
所以基本上它只是一个逻辑问题。