xcode - 警报消息按钮,限制进一步进入下一个 View Controller

标签 xcode swift swift2

您好,我正在使用具有年龄限制的 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) {
// ...
}

基于:http://nshipster.com/uialertcontroller/

关于xcode - 警报消息按钮,限制进一步进入下一个 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35963034/

相关文章:

python - 为 Kivy-ios 编译自定义模块

ios - 如何在 UITextview 上的单击事件中停止键盘外观?

php - iOS 应用程序接收格式正确的 JSON,但仍然返回为不以数组或对象开头

arrays - Swift:使用枚举的原始值访问 UIButton 和常量数组中的字符串是个好主意吗?

ios - 在 iOS 中创建这样的自定义对话框?

ios - 图像 Assets 不适用于通用应用程序中的尺寸类别

iphone - 使用 bit.ly 缩短网址

swift - 在扩展函数中处理零计数 - 用于 image.results 中的结果

ios - Swift 2 - iOS - 分派(dispatch)回原始线程

ios - 创建 Realm 列表的语法