Swift 3.3+Xcode9.3使用YPDrawSignatureView遇Thread 1: signal SIGABRT错误
Hey Johnny, let's work through that SIGABRT error you're hitting—this almost always boils down to a setup/connection issue rather than core code logic. Let's go through the most likely fixes step by step:
1. Fix the Custom View Setup in Storyboard
This is probably the root cause: you mentioned adding a UIImage control, but YPDrawSignatureView is a custom UIView subclass, not a UIImageView. Here's how to correct this:
- Open
Main.storyboard, delete the UIImageView you added. - Drag a plain
UIViewonto your view controller's canvas instead. - Select this new UIView, open the Identity Inspector (right-hand panel, third tab), and set its
ClasstoYPDrawSignatureView. Make sure theModuledropdown is set to your project's target (if you have multiple targets). - Reconnect this custom view to your
signatureViewIBOutlet inViewController.swift(right-click the outlet in code and drag to the view in storyboard to re-establish the link).
Also, double-check for broken IBOutlets: right-click signatureView in your code—if you see any red-highlighted connections, delete them and re-link properly.
2. Verify YPDrawSignatureView is Added to Your Target
If Xcode isn't compiling the YPDrawSignatureView.swift file, it'll throw a runtime error when trying to instantiate the class:
- Select the
YPDrawSignatureView.swiftfile in your project navigator. - Open the File Inspector (right-hand panel, first tab).
- Under Target Membership, ensure your main project target is checked. If it's not, tick the box and recompile.
3. Correct Delegate Setup
You implemented YPSignatureDelegate, but you need to assign the delegate to your signature view to avoid potential crashes or missing callbacks:
Add this line to your viewDidLoad() method:
signatureView.delegate = self
Also, confirm that the YPSignatureDelegate protocol is marked with @objc (check the library's code):
@objc protocol YPSignatureDelegate: NSObjectProtocol { // Delegate methods here }
Without @objc, Swift 3.3 might not properly recognize the delegate methods, leading to runtime issues.
4. Check Swift Version Compatibility
YPDrawSignatureView might have Swift 4+ syntax that doesn't play nice with Swift 3.3. If you see compile errors in the library file, adjust these common differences:
- Replace
@objcMemberswith explicit@objcmarkers on individual methods/properties. - Fix parameter label changes (Swift 3 uses external labels more strictly than later versions).
- Ensure all
UIKitimports are present and correct.
Corrected Sample Code
Here's a cleaned-up version of your view controller code with proper setup:
import UIKit class ViewController: UIViewController, YPSignatureDelegate { @IBOutlet weak var signatureView: YPDrawSignatureView! @IBOutlet weak var clearButton: UIButton! @IBOutlet weak var saveButton: UIButton! @IBOutlet weak var signaturePreviewImage: UIImageView! // For displaying saved signature override func viewDidLoad() { super.viewDidLoad() // Set up signature view properties and delegate signatureView.delegate = self signatureView.strokeColor = .black signatureView.lineWidth = 2.0 } // Clear signature action @IBAction func clearSignatureTapped(_ sender: UIButton) { signatureView.clear() signaturePreviewImage.image = nil } // Save signature action @IBAction func saveSignatureTapped(_ sender: UIButton) { if let signatureImage = signatureView.getSignature() { signaturePreviewImage.image = signatureImage // Add code here to save/upload the image if needed } } // Implement delegate method (optional, if required by the protocol) func signatureDidChange(_ view: YPDrawSignatureView) { print("Signature updated") } }
Start with step 1 first—mixing up the view type is the most common reason for this SIGABRT error in your setup.
内容的提问来源于stack exchange,提问作者Johnny

