Swift 4 奇怪的意外跳回到早期的故事板
Swift 4 Odd unexpected jump back to earlier storyboard
我是 Swift 和 Xcode 的新手,我构建了一个简单的双故事板应用程序,它有一个欢迎屏幕和一个按钮,用于触发第二个故事板的序列(为此我基于 UIVIewController 创建了自己的 class。第二个故事板有几个 UIPickerView 对象和几个 UIDatePickerView 对象和一个按钮。我在 didSelectRow 函数下有一些基本操作。但是当我构建和 运行 应用程序我得到一个奇怪的和看似随机的行为,在第一个选择器视图中选择一些东西将我带回到欢迎屏幕(放松序列?)。我没有输入任何代码来做到这一点!有什么想法吗?
import UIKit
class SecondViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
@IBOutlet weak var cityPicker: UIPickerView!
@IBOutlet weak var fromDatePicker: UIDatePicker!
@IBOutlet weak var toDatePicker: UIDatePicker!
@IBOutlet weak var nextThirdViewButton: UIButton!
// temporay array for tsting - Populate with sqlite db extract
var cityData = ["Please select city", "Beijing", "Calcutta", "Detroit", "Edinburgh", "Glasgow", "London", "Manchester", "New York", "Rio de Janerio", "Washington"]
// key variables and flags
var cityPicked = false
var fromTimePicked = false
var toTimePicked = false
var citySelected: String?
var fromDate: Date?
var toDate: Date?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
fromDatePicker.minimumDate = Date()
toDatePicker.minimumDate = Date()
fromDate = Date()
nextThirdViewButton.layer.cornerRadius = 15
nextThirdViewButton.layer.masksToBounds = true
cityPicker.delegate = self
cityPicker.dataSource = self
}
//PickerView functions
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return cityData.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return cityData[row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
//check user has selcted a city from pickerview and set flags accordingly
if self.cityData[row] != "Please select city" {
self.citySelected = self.cityData[row]
self.cityPicked = true
} else {
self.citySelected = "Please select city"
self.cityPicked = false
}
}
@IBAction func fromDatePicked(_ sender: Any) {
fromDate = fromDatePicker.date
if toTimePicked == true {
checkDates(fromDate: fromDate!, toDate: toDate!)
}
fromTimePicked = true
}
@IBAction func toDatePicked(_ sender: Any) {
toDate = toDatePicker.date
checkDates(fromDate: fromDate!, toDate: toDate!)
}
//Function to check if 'to date' is earlier than 'from date' and issue alert
func checkDates (fromDate: Date, toDate: Date) {
switch toDate.compare(fromDate) {
case .orderedDescending:
toTimePicked = true
case .orderedAscending:
presentAlert(title: "Date Alert", message: "You can't travel back in time!")
toTimePicked = false
default: return
}
}
// Function to move to third storyboard
@IBAction func nextThirdViewButton(_ sender: Any) {
if !(cityPicked && fromTimePicked && toTimePicked) {
presentAlert(title: "You're not ready yet", message: "Please complete choices")
} else {
performSegue(withIdentifier: "thirdViewSeque", sender: nil)
}
}
//reusable alert function
func presentAlert(title: String, message: String) {
let presentAlert = UIAlertController(title: title, message: message, preferredStyle: .alert)
let dismissButton = UIAlertAction (title: "Dismiss", style: .cancel, handler: {
(dateAlert: UIAlertAction) -> Void in
})
presentAlert.addAction(dismissButton)
self.present(presentAlert, animated: true, completion: nil)
}
}
问题是您使用的是 .partialCurl
呈现的视图控制器。不。您偶然发现了一个可怕的错误,即呈现的视图控制器有一个不可见区域,如果点击该区域,视图控制器就会消失。
虽然最简单的解决方案是完全避免 .partialCurl
,但我有一个精心设计且极其 hacky 的解决方法。这里是;将此代码放入您呈现的视图控制器中:
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
// workaround for curl bug
if let grs = self.view.gestureRecognizers {
for g in grs {
if NSStringFromClass(type(of:g)).hasSuffix("CurlUpTapGestureRecognizer") {
g.isEnabled = false
}
}
}
}
我是 Swift 和 Xcode 的新手,我构建了一个简单的双故事板应用程序,它有一个欢迎屏幕和一个按钮,用于触发第二个故事板的序列(为此我基于 UIVIewController 创建了自己的 class。第二个故事板有几个 UIPickerView 对象和几个 UIDatePickerView 对象和一个按钮。我在 didSelectRow 函数下有一些基本操作。但是当我构建和 运行 应用程序我得到一个奇怪的和看似随机的行为,在第一个选择器视图中选择一些东西将我带回到欢迎屏幕(放松序列?)。我没有输入任何代码来做到这一点!有什么想法吗?
import UIKit
class SecondViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
@IBOutlet weak var cityPicker: UIPickerView!
@IBOutlet weak var fromDatePicker: UIDatePicker!
@IBOutlet weak var toDatePicker: UIDatePicker!
@IBOutlet weak var nextThirdViewButton: UIButton!
// temporay array for tsting - Populate with sqlite db extract
var cityData = ["Please select city", "Beijing", "Calcutta", "Detroit", "Edinburgh", "Glasgow", "London", "Manchester", "New York", "Rio de Janerio", "Washington"]
// key variables and flags
var cityPicked = false
var fromTimePicked = false
var toTimePicked = false
var citySelected: String?
var fromDate: Date?
var toDate: Date?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
fromDatePicker.minimumDate = Date()
toDatePicker.minimumDate = Date()
fromDate = Date()
nextThirdViewButton.layer.cornerRadius = 15
nextThirdViewButton.layer.masksToBounds = true
cityPicker.delegate = self
cityPicker.dataSource = self
}
//PickerView functions
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return cityData.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return cityData[row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
//check user has selcted a city from pickerview and set flags accordingly
if self.cityData[row] != "Please select city" {
self.citySelected = self.cityData[row]
self.cityPicked = true
} else {
self.citySelected = "Please select city"
self.cityPicked = false
}
}
@IBAction func fromDatePicked(_ sender: Any) {
fromDate = fromDatePicker.date
if toTimePicked == true {
checkDates(fromDate: fromDate!, toDate: toDate!)
}
fromTimePicked = true
}
@IBAction func toDatePicked(_ sender: Any) {
toDate = toDatePicker.date
checkDates(fromDate: fromDate!, toDate: toDate!)
}
//Function to check if 'to date' is earlier than 'from date' and issue alert
func checkDates (fromDate: Date, toDate: Date) {
switch toDate.compare(fromDate) {
case .orderedDescending:
toTimePicked = true
case .orderedAscending:
presentAlert(title: "Date Alert", message: "You can't travel back in time!")
toTimePicked = false
default: return
}
}
// Function to move to third storyboard
@IBAction func nextThirdViewButton(_ sender: Any) {
if !(cityPicked && fromTimePicked && toTimePicked) {
presentAlert(title: "You're not ready yet", message: "Please complete choices")
} else {
performSegue(withIdentifier: "thirdViewSeque", sender: nil)
}
}
//reusable alert function
func presentAlert(title: String, message: String) {
let presentAlert = UIAlertController(title: title, message: message, preferredStyle: .alert)
let dismissButton = UIAlertAction (title: "Dismiss", style: .cancel, handler: {
(dateAlert: UIAlertAction) -> Void in
})
presentAlert.addAction(dismissButton)
self.present(presentAlert, animated: true, completion: nil)
}
}
问题是您使用的是 .partialCurl
呈现的视图控制器。不。您偶然发现了一个可怕的错误,即呈现的视图控制器有一个不可见区域,如果点击该区域,视图控制器就会消失。
虽然最简单的解决方案是完全避免 .partialCurl
,但我有一个精心设计且极其 hacky 的解决方法。这里是;将此代码放入您呈现的视图控制器中:
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
// workaround for curl bug
if let grs = self.view.gestureRecognizers {
for g in grs {
if NSStringFromClass(type(of:g)).hasSuffix("CurlUpTapGestureRecognizer") {
g.isEnabled = false
}
}
}
}