繁体   English   中英

按下按钮时如何更改日期?

[英]How would I change the date when pressing a button?

我正在使用NSDate,我得到了要显示的日期,并且它使用iPhone的时间进行了更改。 我现在在按下按钮时无法更改日期,无论是前一天还是第二天。 我需要像健身应用程序中那样,它们具有两个箭头,您可以按左一个箭头转到上一个日期,而按右箭头箭头转到第二天。 我该怎么做? 这是我现在拥有的代码:

    override func didMoveToView(view: SKView) {

    dateLabel.zPosition = 22
    dateLabel.fontName = "Krungthep"
    dateLabel.fontColor = UIColor.whiteColor()
    dateLabel.fontSize = 17
    dateLabel.text = NSDateFormatter.localizedStringFromDate(NSDate(), dateStyle: NSDateFormatterStyle.FullStyle, timeStyle: NSDateFormatterStyle.NoStyle)
    dateLabel.position = CGPointMake(self.size.width / 2.0, self.size.height / 1.1)
    self.addChild(dateLabel)

   }

       override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {

    var touch: UITouch = touches.first as! UITouch
    var location = touch.locationInNode(self)
    var node = self.nodeAtPoint(location)

            if node.name == "next" {
             //next day

              }







    if node.name == "before" {

        //previous day 
        let earlyDate = NSCalendar.currentCalendar().dateByAddingUnit(NSCalendarUnit.CalendarUnitDay,value: -1,toDate:
            NSDate(),options: NSCalendarOptions.WrapComponents)
        date.earlierDate(earlyDate!)


    }

看看NSCalendar 具体来说,请在Xcode文档中搜索“日历计算”。

您将需要获取当前日历,然后创建一个NSDateComponents对象,将其day值设置为1或-1,然后使用dateByAddingComponents:toDate:options方法从日期中添加或减去某天。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM