无法将'Authorize.ClubsViewController'(0x109c70cc8)类型的值转换为'Authorize.NewViewController'(0x109c70df8)

时间:2017-07-03 07:01:19

标签: ios swift xcode

@IBOutlet weak var menuButton: UIButton!

@IBOutlet weak var clubButton: UIButton!

@IBOutlet weak var announcemnetsButton: UIButton!

@IBOutlet weak var eventButton: UIButton!

let transition  = CircularTransition()

override func viewDidLoad() {
    super.viewDidLoad()

    menuButton.layer.cornerRadius = menuButton.frame.size.width / 2
   clubButton.layer.cornerRadius = menuButton.frame.size.width / 2
    announcemnetsButton.layer.cornerRadius = menuButton.frame.size.width / 2
    eventButton.layer.cornerRadius = menuButton.frame.size.width / 2
    // Do any additional setup after loading the view.

}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    let secondVC = segue.destination as! NewViewController
    secondVC.transitioningDelegate = self
    secondVC.modalPresentationStyle = .custom
    let thirdVC = segue.destination as! ClubsViewController
    thirdVC.transitioningDelegate = self
    thirdVC.modalPresentationStyle = .custom
    let fourthVC = segue.destination as! AnnouncementsViewController
    fourthVC.transitioningDelegate = self
    fourthVC.modalPresentationStyle = .custom
    let fifthVC = segue.destination as! EventsViewController
    fifthVC.transitioningDelegate = self
    fifthVC.modalPresentationStyle = .custom
}

我正在运行此代码,但我一直收到错误,我做错了什么?我相信一切都是正确的,但我不断收到SIGABRT错误。

2 个答案:

答案 0 :(得分:0)

每个Segue都有一个identifier。您必须为identifier

设置segue

prepare中,您没有查看segue identifier。由于您正试图强制转换segue.destination控制器四个不同的控制器,这是错误的。

请参阅how to set segue identifier

根据您的segue identifier

更改代码
 override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "NewVC" {
            let secondVC = segue.destination as! NewViewController
            secondVC.transitioningDelegate = self
            secondVC.modalPresentationStyle = .custom
        } else if segue.identifier == "ClubVC" {
            let thirdVC = segue.destination as! ClubsViewController
            thirdVC.transitioningDelegate = self
            thirdVC.modalPresentationStyle = .custom
        } else if segue.identifier == "AnnouncementVC" {
            let fourthVC = segue.destination as! AnnouncementsViewController
            fourthVC.transitioningDelegate = self
            fourthVC.modalPresentationStyle = .custom
        } else if segue.identifier == "EventVC" {
            let fifthVC = segue.destination as! EventsViewController
            fifthVC.transitioningDelegate = self
            fifthVC.modalPresentationStyle = .custom
        }
    }

答案 1 :(得分:0)

您似乎将相同的segue目标转换为许多不同类型的VC,但显然不能同时将它们全部转换为。

我认为你打算做的是检查segue目的地是否属于特定类型(在这种情况下你不能用!强制解包):

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if let secondVC = segue.destination as? NewViewController {
         secondVC.transitioningDelegate = self
         secondVC.modalPresentationStyle = .custom
    } else if let thirdVC = segue.destination as? ClubsViewController {
         thirdVC.transitioningDelegate = self
         thirdVC.modalPresentationStyle = .custom
    } else if let fourthVC = segue.destination as? AnnouncementsViewController {
         fourthVC.transitioningDelegate = self
         fourthVC.modalPresentationStyle = .custom
    } else if let fifthVC = segue.destination as? EventsViewController {
         fifthVC.transitioningDelegate = self
         fifthVC.modalPresentationStyle = .custom
    }
}

在这种情况下,使用segue标识符可能更明智。

然后再次考虑到你总是在执行相同的操作而不管VC是什么,也许值得将它转换为通用VC(即as! UIViewController并对其执行操作而不是区分它们? / p>