添加UINavigationController时崩溃-Swift

时间:2019-06-29 13:58:00

标签: ios swift storyboard

添加UINavigationController时崩溃。 我正在使用此库和示例代码:

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

# see if the file to attach is there and if not make $attachments an empty array
if (Test-Path -Path '.\data.csv' -PathType Leaf) { $attachments = @('.\data.csv') } else { $attachments = @() }

# pipe the attachments array to the cmdlet
$attachments | Send-MailMessage -From 'User01 <user01@fabrikam.com>' -To 'User02 <user02@fabrikam.com>', 'User03 <user03@fabrikam.com>' -Subject 'Sending the Attachment' -Body 'Forgot to send the attachment. Sending now.' -Priority High -DeliveryNotificationOption OnSuccess, OnFailure -SmtpServer 'smtp.fabrikam.com'

Xcode错误日志:

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)

1 个答案:

答案 0 :(得分:0)

第一

强行解开值(使用!)通常是不好的做法,大多数时候可以(并且应该)避免使用。强制解包保证会完全崩溃您的应用程序(如果不存在某种情况,则无需解包)。

相反,您应该使用if let或guard let之类的东西,例如

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

或带有if让:

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

我对修正错误的猜测:

您没有将自定义类(在故事板内部)设置为CardCollectionViewCell。请参阅下面的个人项目示例,以在Xcode中找到正确的输入字段。该示例适用于TableView,但适用于您的CollectionView。

  • 您必须选择自定义单元格
  • 输入您的自定义课程名称
  • 按Enter键。

否则,默认情况下,您的单元格将是UICollectionViewCell。

screenshot xcode storyboard, setting custom class

如果通过代码初始化情节提要单元,请检查是否确实初始化了自定义类型的单元格而不是UICollectionViewCell。