我有一个应用程序,可以计算两个日期之间的差异。它在模拟器中工作正常但在iOS设备上没有(iPad使用iOS 9)。
当我按下iPad上的“计算差异”按钮时,没有任何反应。在Xcode的调试区域中,我收到以下错误消息:
2015-11-16 08:33:32。2446日期至今计算器[369:64113]
<CATransformLayer: >0x17eb95e0>
- 在仅变换图层中更改属性masksToBounds将具有&gt;无效果 2015-11-16 08:33:32.250日期到日期计算器[369:64113]
<CATransformLayer: 0x17d8ea30>
- 在仅变换图层中更改&gt;属性masksToBounds将无效 2015-11-16 08:33:32.251日期到日期计算器[369:64113]
<CATransformLayer: 0x17d95830>
- 在仅变换图层中更改&gt;属性masksToBounds将无效
我在SO上搜索了这个并且无法找到解决方案。
有人有任何建议吗?
谢谢!
请在下面找到View Controller代码:
import UIKit
类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 = ""
}
}
答案 0 :(得分:0)
我愿意打赌你的代码,即使在模拟器中也不会“正常工作”。例如,当我第一次点击文本字段时,日期选择器不会显示。我必须在背景屏幕上点击resignFirstResponder,然后再次点击文本字段以显示日期选择器。
问题是您没有实例化日期选择器并将其连接到文本字段,直到 文本字段成为第一个响应者之后,您需要先执行之前的操作它成为第一响应者。 (例如在viewDidLoad方法中。)