Swift 4 Odd意外跳回较早的故事板 [英] Swift 4 Odd unexpected jump back to earlier storyboard
问题描述
我对Swift和Xcode还是陌生的,我构建了一个简单的两个故事板应用程序,该应用程序具有欢迎屏幕和一个按钮,可以触发第二个故事板的运行(为此,我基于UIVIewController创建了自己的类.第二个情节提要板上有几个UIPickerView对象a和几个UIDatePickerView对象以及一个按钮.didSelectRow函数下有一些基本操作.但是,当我构建并运行该应用程序时,我得到了一个奇怪的,看似随机的行为,在其中选择了一些内容.首先选择器视图将我带回到欢迎屏幕(放松的定语?).我还没有输入任何代码来做到这一点!有什么想法吗?
I’m very new to Swift and Xcode and I’ve built a simple two storyboard app which has a welcome screen and a button that triggers a seque to second storyboard (for which I have created its own class based on UIVIewController. Second storyboard has a couple of UIPickerView objects a and a couple of UIDatePickerView objects and a button. I have some basic actions under the didSelectRow function. However when I build and run the app I get an odd and seemingly random bevahiour where selecting something in the first picker view takes me back to the welcome screen (an unwind seque?). I haven’t put in any code to do that! Any ideas?
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
显示的视图控制器.别.您偶然发现了一个可怕的错误,即所显示的视图控制器中有一个不可见区域,如果轻按该区域,则会将其关闭.
The problem is that you are using a .partialCurl
presented view controller. Don't. You've stumbled on a terrible bug, namely that there is an invisible area of the presented view controller which, if tapped, dismisses the view controller.
尽管有一个最简单的解决方案是完全避免使用.partialCurl
,但我有一个精心设计且极其笨拙的解决方法.这里是;将此代码放入您显示的视图控制器中:
I have an elaborate and extremely hacky workaround, though the simplest solution is to avoid .partialCurl
entirely. Here it is; put this code into your presented view controller:
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 4 Odd意外跳回较早的故事板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!