2016-08-03 116 views
0

App Screenshot更改標籤文本和顏色隨着當前時間

我試圖改變你我的截圖,有一個綠色的背景,並說我們是開放的看到標籤。

我希望底部標籤變成紅色,並在上市開放時間過去後說「對不起,我們已關閉」,然後回到綠色並在正確的開放時間說「我們是開放的」。

我已經成功地將日期和時間導入頂部標籤,但我不確定底部標籤如何。

下面是代碼:

import UIKit 
import Firebase 
import FirebaseInstanceID 
import FirebaseMessaging 

class FirstViewController: UIViewController { 

    var timer = Timer() 

    @IBOutlet weak var timeLabel: UILabel! 

    @IBOutlet weak var openStatusLabel: UILabel! 


    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 

     FIRMessaging.messaging().subscribe(toTopic: "/topics/news") 

     self.timer = Timer.scheduledTimer(timeInterval: 1.0, 
                  target: self, 
                  selector: #selector(FirstViewController.tick), 
                  userInfo: nil, 
                  repeats: true) 
    } 

    @objc func tick() { 
     timeLabel.text = DateFormatter.localizedString(from: NSDate() as Date, 
                   dateStyle: .medium, 
                   timeStyle: .medium) 
    } 

} 
+0

請發表您的代碼。 –

+0

將代碼添加到原始帖子中。很多感謝 – Elfuthark

+1

tick()函數需要首先確定是否打開或關閉...首先確定。 http://stackoverflow.com/questions/6112075/ios-compare-two-dates,一旦你這樣做,設置背景顏色和標籤內容應該很容易。 – CSmith

回答

1

下面的代碼,我嘗試它會正常工作。最初我創建了兩個具有適當約束的UILabel。然後創建網點兩個標籤來查看控制器。然後試試這個代碼。

import UIKit 

extension NSDate { 
    func dayOfWeek() -> Int? { 
     guard 
      let calender: NSCalendar = NSCalendar.currentCalendar(), 
      let component: NSDateComponents = calender.components(.Weekday, fromDate: self) else { return nil } 
     return component.weekday 
    } 
} 

class ViewController: UIViewController { 


    @IBOutlet var timeOfTheDay: UILabel! //Top Label for showing current time 

    @IBOutlet var Status: UILabel! //Status Label for showing open or close 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     self.dateCheck() 



    } 

    func dateCheck() 
    { 

     let today = NSDate().dayOfWeek() 

     if today == 1 
     { 
      //print("Sunday") 
      self.closed() 

     } 
     else if today == 2 
     { 
      //print("Monday") 
      self.closed() 
     } 
     else if today == 3 
     { 
      //print("Tuesday") 
      self.uptoEvening() 
     } 
     else if today == 4 
     { 
      //print("Wednesday") 
      self.uptoNight() 
     } 
     else if today == 5 
     { 
      // print("Thursday") 
      self.uptoNight() 

     } 
     else if today == 6 
     { 
      //print("Friday") 
      self.uptoNight() 

     } 
     else 
     { 
      //print("Saturday") 
      self.uptoEvening() 
     } 
    } 

    func getTime() -> (hour:Int, minute:Int, second:Int) { 
     let currentDateTime = NSDate() 
     let calendar = NSCalendar.currentCalendar() 
     let component = calendar.components([.Hour,.Minute,.Second], fromDate: currentDateTime) 
     let hour = component.hour 
     let minute = component.minute 
     let second = component.second 
     return (hour,minute,second) 
    } 


    func closed() 
    { 
     timeOfTheDay.text = String(getTime().hour)+" : "+String(getTime().minute)+" : "+String(getTime().second) 
     timeOfTheDay.backgroundColor = UIColor.redColor() 
     timeOfTheDay.textColor = UIColor.whiteColor() 

     Status.text = "Sorry! Today, We are Closed!" 
     Status.backgroundColor = UIColor.redColor() 
     Status.textColor = UIColor.whiteColor() 

    } 

    func opened(endTime:String) 
    { 
     timeOfTheDay.text = String(getTime().hour)+" : "+String(getTime().minute)+" : "+String(getTime().second) 
     timeOfTheDay.backgroundColor = UIColor.greenColor() 
     timeOfTheDay.textColor = UIColor.whiteColor() 

     Status.text = "Hi! still we are opened upto "+endTime 
     Status.backgroundColor = UIColor.greenColor() 
     Status.textColor = UIColor.whiteColor() 
    } 


    func uptoEvening() 
    { 

     let time = getTime().hour 

     switch time 
     { 
      case 09...16: opened("17") //set time for 09:00 to 16:59 
      default:closed() 
     } 
    } 

    func uptoNight() 
    { 

     let time = getTime().hour 

     switch time 
     { 
     case 09...20: opened("21") //set time for 09:00 to 20:59 
     default:closed() 
     } 

    } 



    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 

    } 


} 

擴展迅速3:

extension Date { 
    func dayOfWeek() -> Int? { 
      let calender: Calendar = Calendar.current 
      let component: DateComponents = (calender as NSCalendar).components(.weekday, from: self) 
     return component.weekday 
    } 
} 

enter image description here

+0

'擴展日期{0} {0} func dayOfWeek() - > Int? { 後衛 讓日曆:日曆= Calendar.current, 讓組件:DateComponents = calendar.dateComponents(。天,來自:個體經營)其他{返回nil} 回報component.weekday } }'我有麻煩使用Xcode 8中的這部分代碼,你知道Xcode 7中完美的功能嗎? – Elfuthark

+0

你得到了什麼錯誤?它完美的工作在Swift 2.0上 –

+0

是的,它在Xcode 7中完美工作,因爲我構建並運行它,但隨後當Swift 3中的語法發生變化時,我讓它自動修復所有內容,但留下了一個錯誤'Type'Set 'has no member'day'' – Elfuthark

相關問題