0

I have an app that calculates the difference between two dates. It works fine in the Simulator but not on iOS device (iPad using iOS 9).

When I press the 'calculate difference' buttons on the iPad nothing happens. In the Debug Area of Xcode I get the following error message:

2015-11-16 08:33:32.246 Date-to-Date Calculator[369:64113] <CATransformLayer: >0x17eb95e0> - changing property masksToBounds in transform-only layer, will have >no effect 2015-11-16 08:33:32.250

Date-to-Date Calculator[369:64113] <CATransformLayer: 0x17d8ea30> - changing >property masksToBounds in transform-only layer, will have no effect 2015-11-16 08:33:32.251

Date-to-Date Calculator[369:64113] <CATransformLayer: 0x17d95830> - changing >property masksToBounds in transform-only layer, will have no effect

I've searched this on SO and cannot find a solution.

Does anyone have any suggestions?

Thanks!

Please find View Controller code below:

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var startDateTextField: UITextField!
@IBOutlet weak var endDateTextField: UITextField!
@IBOutlet weak var resultNumberOfDays: UILabel!
@IBOutlet weak var resultWeeksAndDays: UILabel!



override func viewDidLoad() {
    super.viewDidLoad()

    self.view.backgroundColor = UIColor(red: 210/255, green: 213/255, blue: 220/255, alpha: 1.0)

    let toolBar = UIToolbar(frame: CGRectMake(0, self.view.frame.size.height/6, self.view.frame.size.width, 40.0))

    toolBar.layer.position = CGPoint(x: self.view.frame.size.width/2, y: self.view.frame.size.height-20.0)

    toolBar.barStyle = UIBarStyle.BlackTranslucent

    toolBar.tintColor = UIColor.whiteColor()

    UIToolbar.appearance().barTintColor = UIColor(red: 0/255, green: 0/255, blue: 205/255, alpha: 1.0)

    let todayBtn = UIBarButtonItem(title: "Today", style: UIBarButtonItemStyle.Plain, target: self, action: "tappedToolBarBtn:")

    let okBarBtn = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Plain, target: self, action: "donePressed:")

    // let okBarBtn = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Done, target: self, action: "donePressed:")

    let flexSpace = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: self, action: nil)

    let label = UILabel(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width / 3, height: self.view.frame.size.height))

    label.font = UIFont(name: "Helvetica Neue", size: 13)

    label.backgroundColor = UIColor.clearColor()

    label.textColor = UIColor.whiteColor()

    label.text = "Select a Start Date"

    label.textAlignment = NSTextAlignment.Center

    let textBtn = UIBarButtonItem(customView: label)

    toolBar.setItems([todayBtn,flexSpace,textBtn,flexSpace,okBarBtn], animated: true)

    startDateTextField.inputAccessoryView = toolBar


    let toolBar2 = UIToolbar(frame: CGRectMake(0, self.view.frame.size.height/6, self.view.frame.size.width, 40.0))

    toolBar2.layer.position = CGPoint(x: self.view.frame.size.width/2, y: self.view.frame.size.height-20.0)

    toolBar2.barStyle = UIBarStyle.BlackTranslucent

    toolBar2.tintColor = UIColor.whiteColor()

    UIToolbar.appearance().barTintColor = UIColor(red: 0/255, green: 0/255, blue: 205/255, alpha: 1.0)

    let todayBtn2 = UIBarButtonItem(title: "Today", style: UIBarButtonItemStyle.Plain, target: self, action: "secondTappedToolBarBtn:")

    let okBarBtn2 = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Plain, target: self, action: "secondDonePressed:")

    // let okBarBtn2 = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Done, target: self, action: "secondDonePressed:")

    let flexSpace2 = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: self, action: nil)

    let label2 = UILabel(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width / 3, height: self.view.frame.size.height))

    label2.font = UIFont(name: "Helvetica Neue", size: 13)

    label2.backgroundColor = UIColor.clearColor()

    label2.textColor = UIColor.whiteColor()

    label2.text = "Select an End Date"

    label2.textAlignment = NSTextAlignment.Center

    let textBtn2 = UIBarButtonItem(customView: label2)

    toolBar2.setItems([todayBtn2,flexSpace2,textBtn2,flexSpace2,okBarBtn2], animated: true)

     endDateTextField.inputAccessoryView = toolBar2

    // Do any additional setup after loading the view, typically from a nib.
}



override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}




func donePressed(sender: UIBarButtonItem) {

    startDateTextField.resignFirstResponder()

}


func secondDonePressed(sender: UIBarButtonItem) {

    endDateTextField.resignFirstResponder()

}




func tappedToolBarBtn(sender: UIBarButtonItem) {

    let dateformatter = NSDateFormatter()
    dateformatter.dateStyle = NSDateFormatterStyle.MediumStyle
    dateformatter.timeStyle = NSDateFormatterStyle.NoStyle
    startDateTextField.text = dateformatter.stringFromDate(NSDate())
    startDateTextField.resignFirstResponder()
}


func secondTappedToolBarBtn(sender: UIBarButtonItem) {

    let dateformatter = NSDateFormatter()
    dateformatter.dateStyle = NSDateFormatterStyle.MediumStyle
    dateformatter.timeStyle = NSDateFormatterStyle.NoStyle
    endDateTextField.text = dateformatter.stringFromDate(NSDate())
    endDateTextField.resignFirstResponder()
}



override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    self.view.endEditing(true)
}


@IBAction func textFieldEditing(sender: UITextField) {
    let datePickerView: UIDatePicker = UIDatePicker()
    datePickerView.datePickerMode = UIDatePickerMode.Date
    sender.inputView = datePickerView
    datePickerView.addTarget(self, action: Selector("datePickerValueChanged:"), forControlEvents: UIControlEvents.ValueChanged)

}



@IBAction func secondTextFieldEditing(sender: UITextField) {
    let datePickerView: UIDatePicker = UIDatePicker()
    datePickerView.datePickerMode = UIDatePickerMode.Date
    sender.inputView = datePickerView
    datePickerView.addTarget(self, action: Selector("secondDatePickerValueChanged:"), forControlEvents: UIControlEvents.ValueChanged)

}



func datePickerValueChanged(sender: UIDatePicker) {
    let dateFormatter = NSDateFormatter()
    dateFormatter.dateStyle = NSDateFormatterStyle.MediumStyle
    dateFormatter.timeStyle = NSDateFormatterStyle.NoStyle
    startDateTextField.text = dateFormatter.stringFromDate(sender.date)

}



func secondDatePickerValueChanged(sender: UIDatePicker) {
        let dateFormatter = NSDateFormatter()
        dateFormatter.dateStyle = NSDateFormatterStyle.MediumStyle
        dateFormatter.timeStyle = NSDateFormatterStyle.NoStyle
        endDateTextField.text = dateFormatter.stringFromDate(sender.date)

}


@IBAction func calculateDays(sender: UIButton) {

    if startDateTextField.text! == "" || endDateTextField! == "" {

        let alert2 = UIAlertController(title: "Oops!", message: "Please Select a Start Date!", preferredStyle: UIAlertControllerStyle.Alert)
        alert2.addAction(UIAlertAction(title: "Close", style: UIAlertActionStyle.Default, handler: nil))
        self.presentViewController(alert2, animated: true, completion: nil)


    }
    else {

        let start = String(startDateTextField.text!)
        let end = String(endDateTextField.text!)
        let dateFormatter = NSDateFormatter()
        dateFormatter.dateFormat = "MM-dd-yyyy"


        guard let startDate = dateFormatter.dateFromString(start), endDate = dateFormatter.dateFromString(end) else {
            // You don't have dates, show error(print("error"), do no nothing - your choice.
            return
        }


        // let startDate:NSDate = dateFormatter.dateFromString(start)!
        // let endDate:NSDate = dateFormatter.dateFromString(end)!
        let calendar = NSCalendar.currentCalendar()
        let components = calendar.components([.Day], fromDate: startDate, toDate: endDate, options: [])
        let secondNewString = "\(components.day) days"
        resultNumberOfDays.text = secondNewString

    }

}


@IBAction func calculateWeeksAndDays(sender: UIButton) {

    if startDateTextField.text! == "" || endDateTextField! == "" {

        let alert2 = UIAlertController(title: "Oops!", message: "Please Select an End Date!", preferredStyle: UIAlertControllerStyle.Alert)
        alert2.addAction(UIAlertAction(title: "Close", style: UIAlertActionStyle.Default, handler: nil))
        self.presentViewController(alert2, animated: true, completion: nil)
    }
    else {

    let start = String(startDateTextField.text!)
    let end = String(endDateTextField.text!)
    let dateFormatter = NSDateFormatter()
    dateFormatter.dateFormat = "MM-dd-yyyy"



        guard let startDate = dateFormatter.dateFromString(start), endDate = dateFormatter.dateFromString(end) else {
            // You don't have dates, show error(print("error"), do no nothing - your choice.
            return
 }


    // let startDate:NSDate = dateFormatter.dateFromString(start)!
    // let endDate:NSDate = dateFormatter.dateFromString(end)!
    let calendar = NSCalendar.currentCalendar()
    let components = calendar.components([.Day], fromDate: startDate, toDate: endDate, options: [])
    let days = components.day
    let weeks = components.day / 7
    let weeksanddays = days % 7
    let newString = "\(weeks) weeks + \(weeksanddays) days"
    resultWeeksAndDays.text = newString
}

}

@IBAction func resetLabels(sender: AnyObject) {

    resultNumberOfDays.text = ""
    resultWeeksAndDays.text = ""


}

}

mhs5819
  • 147
  • 3
  • 13
  • All you have to do is launch an iPad Simulator, and press (CommandKey). –  Nov 16 '15 at 06:19
  • You question doesn't provide any details of the problem. You should at least provide the explanation of your calculation method, or, embed the code of the method in the question, so that SO can help you. And yes, I don't think warning messages from the console are related to your problem. – Fahri Azimov Nov 16 '15 at 06:29
  • See this SO Question: http://stackoverflow.com/questions/32775575/catransformlayer-0x14fea3710-changing-property-maskstobounds-in-transform-o – pkc456 Nov 16 '15 at 06:41
  • Please provide information is you are whice simulator you are using(5s, 4s, ipad etc) is your design ipad compatible ? – Varun Naharia Nov 16 '15 at 07:28
  • Thanks, have just added the View Controller code to my original question. If anyone can see where I went wrong, much appreciated. PS. Am using Xcode 7.1.1 and iPad is using iOS 9.1 – mhs5819 Nov 17 '15 at 00:30

1 Answers1

0

I'm willing to bet that your code, as it stands, doesn't "work fine" even in the simulator. For example when I tap on a text field the first time, the date picker doesn't show. I have to tap on the background screen to resignFirstResponder and then tap on the text field again to get the date picker to show.

The problem there is that you aren't instantiating the date picker and connecting it to the text field until after the text field has become the first responder, you need to do that before it becomes first responder. (For example in the viewDidLoad method.)

Daniel T.
  • 32,821
  • 6
  • 50
  • 72