2015-07-19 52 views
0

嘿所以我是新來的編碼,我在我的視圖控制器(拖放)上放置一個按鈕。該按鈕沒有響應觸摸或任何內容,並顯示爲「靜態」。我累了,精疲力竭。有人幫忙?非常感謝!斯威夫特:按鈕沒有響應輕按

import UIKit 

class ViewController: UIViewController { 

    @IBOutlet weak var lbl1: UILabel! 
    @IBOutlet weak var lblMadeToday: UILabel! 

    @IBOutlet weak var Cents: UILabel! 
    var timer = NSTimer() 
    var hourlyWage : CGFloat = CGFloat (hourlyPrice) 
    var timeTo1 : CGFloat = 0 
    var time : Float = 0 
    var earnt : CGFloat = 0 
    var madeToday : CGFloat = 0 

    @IBAction func btnClick(sender: AnyObject) { 

    } 

    func tick() { 
    earnt = CGFloat (time)/timeTo1 
    lbl1.text = NSString(format: "%.2f", earnt) as String 


     if (earnt >= 1) { 
      time = 0 
      madeToday++ 
      lblMadeToday.text = madeToday.description 
     } 
     time++ 
    } 


    override func viewDidLoad() { 
     super.viewDidLoad() 
     timer = NSTimer.scheduledTimerWithTimeInterval(0.01, target: self, selector: Selector("tick"), userInfo: nil, repeats: true) 
     timeTo1 = CGFloat (360000)/hourlyWage; 

     println("view Loaded :)") 
     NSNotificationCenter.defaultCenter().addObserver(self, selector:"doYourStuff", name: 
      UIApplicationWillEnterForegroundNotification, object: nil) 
    } 


    func doYourStuff(){ 
     var nowHour = NSCalendar.currentCalendar().component(.CalendarUnitHour, fromDate: NSDate()) 
     var nowMinute = NSCalendar.currentCalendar().component(.CalendarUnitMinute, fromDate: NSDate()) 
     var nowSecond = NSCalendar.currentCalendar().component(.CalendarUnitSecond, fromDate: NSDate()) 

     var time1 = (nowHour - globalHour) * 60 
     var time2 = (nowMinute - globalMinute) 
     var time3 : CGFloat = (CGFloat(nowSecond)-CGFloat(globalSecond))/60 
     var timeInMinutesGone : CGFloat = CGFloat(time1) + CGFloat(time2) + CGFloat(time3) 
     println (timeInMinutesGone) 
     madeToday = (timeInMinutesGone/60) * (CGFloat(hourlyWage)) 
     var ef : Int = Int(madeToday) 
     lblMadeToday.text = String(ef) 
    } 

    @IBAction func btnPress(sender: AnyObject) { 
     println("hello") 
    } 
    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 
} 
+0

IBAction爲FUNC btnPress,IBAction爲FUNC btnClick。爲什麼你有兩個ibactions?他們鏈接到相同的UIButton? – agy

+0

刪除所有IBAction Funcs後,我重新運行應用程序,並且該按鈕仍然不會突出顯示:/是否存在阻止視圖控制器操作的內容?或者是否有定時器引起的處理過載? :/ –

+0

嘗試在IBAction函數中放置打印語句。單擊時查看它是否在控制檯中。 – rholmes

回答

0

您的NSTimer正在UI線程中每0.01秒執行一次。這可能對您的應用程序不利。如果您在後臺線程中執行NSTimer,您的按鈕可能會按預期工作。

的一種方式您的NSTimer添加到背景可能是:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {() -> Void in 

    let timer = NSTimer.scheduledTimerWithTimeInterval(0.01, target: self, selector: Selector("tick"), userInfo: nil, repeats: true) 
    NSRunLoop.currentRunLoop().addTimer(timer, forMode: NSDefaultRunLoopMode) 
    NSRunLoop.currentRunLoop().run() 
}) 
+0

非常感謝! :)我如何在後臺線程中執行此操作?非常感謝!我是新來的,所以請儘可能簡單:) –

+0

@LouisJordan,它爲你工作嗎? – agy

+0

Omg是的非常感謝你! :)你是如此有幫助 - 我需要找到這個東西的在線課程哈哈我看着udemy –