您好,我正在使用具有年龄限制的 PickerView 制作 ViewController。我使用了警报和年龄限制,但如果用户年龄不够大,我需要它拒绝访问下一个 ViewController。
像这样制作我的代码,我猜它在Else true
中,我需要更多代码,但我不确定。我希望好心人能帮助我:)
@IBAction func verificerKnap(sender: AnyObject) {
// Creating the age restriction for the datepicker
let dateOfBirth = datoPicker.date
let today = NSDate()
let gregorian = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
let age = gregorian.components([.Year], fromDate: dateOfBirth, toDate: today, options: [])
if age.year < 18 {
// Alert controller som sender en advarsel hvis personen er under 18år
let alertController = UIAlertController(title: "Age restriction", message:
"This app requires an age of 18+", preferredStyle: UIAlertControllerStyle.Alert)
alertController.addAction(UIAlertAction(title: "Okay", style: UIAlertActionStyle.Default,handler: nil))
self.presentViewController(alertController, animated: true, completion: nil)
} else {
true
最佳答案
您可以提供一个 UIAlertController,询问您是否年满 18 岁。链接 2 个按钮“确定”和“取消”并为它们添加代表:
let alertController = UIAlertController(title: "Default Style", message: "Are you 18 years or older?", preferredStyle: .Alert)
let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel) {
(action) in
print("User is not yet >18")
}
alertController.addAction(cancelAction)
let OKAction = UIAlertAction(title: "OK", style: .Default) {
(action) in
dispatch_async(dispatch_get_main_queue(), {
self.presentViewController(desired_view_controller, animated: true, completion: nil)
})
}
alertController.addAction(OKAction)
self.presentViewController(alertController, animated: true) {
// ...
}
关于xcode - 警报消息按钮,限制进一步进入下一个 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35963034/