Swift - 可选绑定不显示 UIAlertController

Swift - Optional Binding doesn't show UIAlertController

我创建了一个辅助函数来使用可选绑定检查 NSUserDefaults 的值。我想在执行 IBAction 中的任何代码之前使用此函数确认值是否存在。

    func checkProfile() {
    //optional binding to confirm that some strings (even empty string values), have been saved to user defaults. Will not face error when unwrapping the optionals on webView or MFMailComposer. Checking Title, FirstName, SecondName

    if let titleString: String = userDefaults.stringForKey("title") {
        //confirmed that there is a string value for titleString
        println("there is a string value for titleString \(titleString)")

        if let firstNameString: String = userDefaults.stringForKey("firstName") {
            //confirmed that there is a string value for firstName

            if let secondNameString: String = userDefaults.stringForKey("secondName") {
                //confirmed that there is a string value for secondName

            } else {
                //secondNameString missing, show alert
                var alert: UIAlertController = UIAlertController(title: "Profile data incomplete", message: "Please provide your 'Profile' data to continue.", preferredStyle: UIAlertControllerStyle.Alert)
                    alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: {
                    //"action in" casts this closure as a type of UIAlertAction which is required by the addAction property.
                    action in self.navigationController?.popToRootViewControllerAnimated(true)
                    //"return" provides the -> Void return type, as not including would return a type of closure (above)
                    return
                }))
                self.presentViewController(alert, animated: true, completion: nil)
            }
        } else {
            //FirstNameString missing, show alert
            var alert: UIAlertController = UIAlertController(title: "Profile data incomplete", message: "Please provide your 'Profile' data to continue.", preferredStyle: UIAlertControllerStyle.Alert)
            alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: {
                //"action in" casts this closure as a type of UIAlertAction which is required by the addAction property.
                action in self.navigationController?.popToRootViewControllerAnimated(true)
                //"return" provides the -> Void return type, as not including would return a type of closure (above)
                return
            }))
            self.presentViewController(alert, animated: true, completion: nil)
        }
    } else {
        //titleString missing, show alert
        var alert: UIAlertController = UIAlertController(title: "Profile data incomplete", message: "Please provide your 'Profile' data to continue.", preferredStyle: UIAlertControllerStyle.Alert)
        alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: {
            //"action in" casts this closure as a type of UIAlertAction which is required by the addAction property.
            action in self.navigationController?.popToRootViewControllerAnimated(true)
            //"return" provides the -> Void return type, as not including would return a type of closure (above)
            return
        }))
        self.presentViewController(alert, animated: true, completion: nil)
    }
}

如您所见,这是对 3 个存储的字符串值的相当直接的检查,return 一个 UIAlertController,如果其中任何一个丢失。

在 IBAction(用户单击按钮)开始时实现此功能时,它表现出一些奇怪的行为。

根据存储在 NSUserDefaults 中的不相关值,IBAction 将执行到另一个 ViewController 的 segue;或者它将实现另一个 class 的实例,并执行将生成 PDF 文件的功能。

当设置值以便 IBAction 最终执行 segue 时,我可以看到可选绑定按预期工作并且 return 如果 3 个存储值之一 = nil(之前执行 segue)。

但是,当设置为生成PDF时,似乎完全忽略了checkProfile()功能。 (甚至不会在任何时候打印到控制台)。

这导致稍后出现 运行 时间错误,因为我在展开我想要签入的可选值时发现 nil checkProfile() - 是否有任何原因导致此函数 运行 在执行 segue 之前正确,但在实施 class 实例之前不正确?目前,我认为这种行为没有合乎逻辑的理由。

谢谢

编辑

这是调用函数的 IBAction:

@IBAction func claimButtonPressed(sender: AnyObject){
//Saves more values to userDefaualts
userDefaults.setObject(dateOfJourneyTextField.text, forKey: "dateOfJourney")


checkTickets()
checkProfile()

//if function to check the ticket type
if userDefaults.stringForKey("ticketType") != "Annual" {
    //calls the function to create the PDF prior to attempting to load the Mail Composer View where it will be an attachment
    let composePaperClaim = ComposePaperClaim()
    composePaperClaim.generatePDF(self)

    //loads MFMailComposeViewContoller...

} else {
    //Does not require PDF, perform Segue to take to the WebView
    performSegueWithIdentifier("showWebViewSegue", sender: self)
}

}

它在调用 composePaperClaim.generatePDF(self) 时看到错误,因为它试图解包 userDefaults.stringForKey("title")!,我试图通过可选绑定建立它已经有一个值。

您正在调用此 checkProfile() 函数,但未对​​结果执行任何操作。该函数应该返回一个布尔值或其他东西以防止调用函数继续进行。你将得到的是在呈现的视图控制器之上的一堆推送视图控制器,然后进入某种奇怪的状态。

目前,无论 checkProfile 中发生什么情况,您都可以调用该 PDF 生成函数,只要类型不是年度即可。这大概会在警报顶部显示另一个视图控制器,您看不到它,然后您可能假设一个值存在于您在 checkProfile 函数中检查的其中一个事物中。