问题
I am having trouble with connecting buttons after I load a custom input view keyboard. My main screens are storyboard and my custom view is a xib.
The input view is a nib with the file owner pointing to its swift class. I am loading the input view with code in another view controller using:
extension UIView {
class func loadFromNibNamed(nibNamed: String, bundle : NSBundle? = nil) -> UIView? {
return UINib(
nibName: nibNamed,
bundle: bundle
).instantiateWithOwner(nil, options: nil)[0] as? UIView
}
}
Inside thie main view controller I'm creating the input view with:
var myInputView = UIView.loadFromNibNamed("keypadInputViewController", bundle: nil)
keypadInputViewController is the base name of the xib and its swift file and the classname in the swift file.
I attach this keyboard using:
self.widthBox.inputView = self.myInputView
The keyboard view then opens as a regular ios keyboard opens
The problem is as soon as I add connectors for buttons from the keypadInputViewController xib to its swift I'll get crash message. The message specifically referrs to any connections I've created. "this class is not key value coding-compliant for the key"
The xib contains a view with a dozen buttons.
I've double checked a number of items including:
- File Owner is set to keypadInputViewController
- ran clean in Xcode
- ran reset in iOS simulator
- manually deleted derived files in library
- deleted reference and re-added xib and swift file to project
- deleted all connections from xib to swift in both, file owner and view sections
- closed and reopened Xcode
- tried the beta 4 for Xcode 6.2
- I have opened xcode and created a test ios project, added a cocoa touch class and used the create xib checkbox. The test project fails with the same error as soon as I add button connections.
After searching for quite a long time I found that not setting owner to self would not allow button connections to function. So I changed the extension to:
extension UIView {
class func loadFromNibNamed(nibNamed: String, bundle : NSBundle? = nil) -> UIView? {
return UINib(
nibName: nibNamed,
bundle: bundle
).instantiateWithOwner(self, options: nil)[0] as? UIView
}}
I still cannot set the view to the file owner. The app crashes on compile if an outlet is connected, or on button press if an action is connected, with the error, "unrecognized selector sent to class"
I don't know if it is me or if it is Xcode. How do I go about getting this keyboard view to accept connections so I can move on? I'm thinking perhaps I didn't add something important to the class definations or am not loading this xib correctly. Ideas?
来源:https://stackoverflow.com/questions/28131214/custom-input-view-as-keyboard-for-textfield-in-swift-using-osx-10-9-and-xcode-6