简体   繁体   English

添加 UINavigationController 时崩溃 - swift

[英]Crash when adding a UINavigationController - swift

Crash when adding a UINavigationController.添加 UINavigationController 时崩溃。 I'm using this library and sample code:我正在使用这个库和示例代码:

https://github.com/appssemble/appstore-card-transition https://github.com/appssemble/appstore-card-transition

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let viewController = storyboard.instantiateViewController(withIdentifier: "type2") as! Type2ViewController

// Get tapped cell location
let cell = collectionView.cellForItem(at: indexPath) as! CardCollectionViewCell
cell.settings.cardContainerInsets = UIEdgeInsets(top: 8.0, left: 16.0, bottom: 8.0, right: 16.0)
cell.settings.isEnabledBottomClose = bottomDismiss

transition = CardTransition(cell: cell, settings: cell.settings)
viewController.settings = cell.settings
viewController.transitioningDelegate = transition
viewController.subtitle = "Bottom dismissible"
viewController.background = UIImage(named: "type2-bg-bottom")

viewController.modalPresentationStyle = .custom   
presentExpansion(viewController, cell: cell, animated: true)

Xcode Error logs: Xcode 错误日志:

Class AppstoreTransition.CardPresentationController overrides the -traitCollection getter, which is not supported. If you're trying to override traits, you must use the appropriate API.
Could not cast value of type 'UINavigationController' (0x1049b1338) to 'AppstoreTransition.CardsViewController' (0x1049b1308).
2019-06-29 15:39:24.077448+0200 TechSpec[10118:1200941] Could not cast value of type 'UINavigationController' (0x1049b1338) to 'AppstoreTransition.CardsViewController' (0x1049b1308).

First:第一的:

Force unwrapping values (using !) is in general bad practice and can (and should) be avoided most times.强制展开值(使用!)通常是不好的做法,大多数时候可以(并且应该)避免。 Force unwrapping guarantees to totally crash your app if somehow there is nothing to unwrap (nil).如果以某种方式没有要解包的内容(零),则强制解包保证您的应用程序完全崩溃。

Instead you should use something like if let or guard let, eg相反,您应该使用 if let 或 guard let 之类的东西,例如

guard let cell = collectionView.cellForItem(at: indexPath) as? CardCollectionViewCell else { debugPrint("uh-oh nil in here 🤓"; return }

or with an if let:或使用 if let:

if let cell = collectionView.cellForItem(at: indexPath) as? CardCollectionViewCell {
    // your other code
} else {
    // handling the case when your cell is not of type CardCollectionViewCell
}

My guess for fixing your bug:我想修复你的错误:

You did not set your custom class (inside your storyboard) to CardCollectionViewCell.您没有将自定义类(在故事板内)设置为 CardCollectionViewCell。 See the example of a personal project below to find the right input field in Xcode.请参阅下面的个人项目示例,以在 Xcode 中找到正确的输入字段。 The example is for a TableView, but for your CollectionView it is the same.该示例适用于 TableView,但对于您的 CollectionView,它是相同的。

  • You have to select your custom cell您必须选择自定义单元格
  • Type in your custom class name输入您的自定义类名
  • Press Enter.按 Enter。

Otherwise, your cell will be a UICollectionViewCell by default.否则,您的单元格将默认为 UICollectionViewCell。

屏幕截图 xcode 故事板,设置自定义类

In case you initialised your storyboard-cell via code, check if you really did initialize a cell of your custom type and not a UICollectionViewCell.如果您通过代码初始化了故事板单元,请检查您是否确实初始化了自定义类型的单元而不是 UICollectionViewCell。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM