如何在Swift中调用performSegueWithIdentifier

时间:2015-08-07 00:43:39

标签: ios swift

我创建了一个prepareForSegue方法,我试图通过使用performSegueWithIdentifier方法创建的按钮来调用它。当我加载模拟器并且它没有给我一个完整的错误消息时,应用程序崩溃了。有人能引导我朝着正确的方向前进吗?

import Foundation
import UIKit
import Alamofire
import FBSDKCoreKit
import FBSDKShareKit
import FBSDKLoginKit

class PageContentViewController: UIViewController {

    @IBOutlet weak var logoImageView: UIImageView!
    @IBOutlet weak var contentLabel: UILabel!
    @IBOutlet weak var backgroundImageView: UIImageView!


    @IBOutlet weak var pageControl: UIPageControl!

    @IBOutlet weak var facebookButton: UIButton!

    var index : Int = 0
    var logoFile: String = ""
    var content: String = ""
    var backgroundFile: String = ""

    let facebookReadPermissions = ["public_profile", "email", "user_friends"]

    override func viewDidLoad() {
        super.viewDidLoad()
        pageControl.currentPage = index

        facebookButton.hidden = (index == 3 ) ? false : true
        pageControl.hidden = (index == 3) ? true: false

        logoImageView.image = UIImage(named: logoFile)
        contentLabel.text = content
        backgroundImageView.image = UIImage(named: backgroundFile)
    }



    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {

        if let destinationController = segue.destinationViewController as? PaymentSubViewController
            where segue.identifier == "payment" {
                // Do something with `destinationController`
        }
    }


    @IBAction func test(sender: AnyObject) {

           self.performSegueWithIdentifier("payment", sender: self)

    }




    @IBAction func fbTouched(sender: AnyObject) {
        FBSDKLoginManager().logInWithReadPermissions(self.facebookReadPermissions, handler: { (result:FBSDKLoginManagerLoginResult!, error:NSError!) -> Void in
            if error != nil {
                //According to Facebook:
                //Errors will rarely occur in the typical login flow because the login dialog
                //presented by Facebook via single sign on will guide the users to resolve any errors.

                // Process error
                FBSDKLoginManager().logOut()
            } else if result.isCancelled {
                // Handle cancellations
                FBSDKLoginManager().logOut()
            } else {
                let fbToken = result.token.tokenString

                Alamofire.request(Router.FacebookAuth(fbToken)).validate(statusCode: 200 ..< 300).responseJSON(completionHandler: { (request, response, JSON, error) in
                    if let json = JSON as? Dictionary<String, AnyObject> {
                        if let token = json["token"] as? String {
                            Router.OAuthToken = token
                            self.performSegueWithIdentifier("showHomeFeed", sender: self)
                        }
                    }
                })
            }
        })
    }

}

2 个答案:

答案 0 :(得分:1)

由于您使用destinationViewController强制解包as!,如果该值为nilPaymentSubViewController,则应用会崩溃。

实现这一点的更好方法是使用可选绑定(if let)和条件转发(as?):

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {

    if let destinationController = segue.destinationViewController as? PaymentSubViewController
        where segue.identifier == "payment" {
            // Do something with `destinationController`
    }
}

当然,这可以阻止崩溃,但不会回答为什么segue.destinationViewControllernil或其他类型的问题。确保在界面构建器中正确配置了segue,并且目标视图控制器在身份检查器选项卡中的 Class 值实际上具有segue.destinationViewController

答案 1 :(得分:0)

I had a map object on the storyboard and I did not add an outlet for the object which was creating an error message.