aromajoin / material-showcase-ios

✨ An elegant way to guide your beloved users in iOS apps - Material Showcase.
https://aromajoin.com/
Other
362 stars 128 forks source link

Random crash due nil init #123

Open ppamorim opened 4 years ago

ppamorim commented 4 years ago

Hi, I was testing my app on the simulator and this happened:

Screenshot 2020-03-08 at 00 11 48 Screenshot 2020-03-08 at 00 11 22 Screenshot 2020-03-08 at 00 11 08

To start the showcase it's calling:

let showcase: MaterialShowcase = MaterialShowcase()
showcase.setTargetView(view: button)
showcase.show(completion: { [weak self] in
  //...
}

Version: 0.7.1

quangctkm9207 commented 4 years ago

Do you think that the materialShowcase.show() function was called too early that the target view had not laid out in UI yet? So, it was null at that time.

ppamorim commented 4 years ago

@quangctkm9207 button has been created and added as subview in the function loadView() by code and materialShowcase.show() was called in the viewDidAppear(_ animated: Bool) method. I know I know, very strange.

quangctkm9207 commented 4 years ago

Yes. It is quite strange because it happened randomly.

ppamorim commented 4 years ago

@quangctkm9207 I think the only way to sort this out is to rewrite the instances and use UIView? instead of UIView!.

quangctkm9207 commented 4 years ago

That would be a good suggestion. Based on your tests, how often does the crash occur?

AlexanderZubkov commented 4 years ago
/// Sets a UITableViewCell as target
@objc public func setTargetView(tableViewCell: UITableViewCell) {
    targetView = tableViewCell.contentView
    // for tableViewCell, we do not need target holder (circle view)
    // therefore, set its radius = 0
    targetHolderRadius = 0
}

/// Sets a UICollectionViewCell as target
@objc public func setTargetView(collectionViewCell: UICollectionViewCell) {
    targetView = collectionViewCell
    // for collectionViewCell, we do not need target holder (circle view)
    // therefore, set its radius = 0
    targetHolderRadius = 0
}
geclick commented 4 years ago

Hey, i wanted to show the showcase on a UIBarButtonItem and i ran into this crash. The reason is here:

if let view = (barButtonItem.value(forKey: "view") as? UIView)?.subviews.first { targetView = view }

view cannot be casted to UIView so this fails and targetView never get initialized. I solved this by setting a custom view(UIButton) to the UIBarButtonItem