User class that inherits UITextField not working (with user initialization) in Swift

So the name says it all. I am trying to create a class that explicitly inherits from UITextField. This is because I want to make some settings for UITextFields. My main goal is to create a custom initializer for the class, however I ran into a problem doing this.

Here is my code:

import UIKit

class CustomTextField: UITextField {

    //Class variables

    required init(coder decoder: NSCoder, other:String) {
        super.init(coder: decoder)
        self.layer.cornerRadius = 15
        println("Instantiated")
        println(other)

    }
}

But the compiler complains 'required' initializer 'init(coder:)' must be provided by subclass of 'UITextField'. Then I continue to apply the proposed fix for the problem and add the following code right below my required initialization code:

required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

My code above never prints "Instantiated"

? 2 ( -NSCoder). , (, Java, ).

!

!

+4
2

:

class CustomTextField: UITextField {

    init(frame: CGRect, arg1: CGFloat, arg2: String) {
        super.init(frame: frame)

        self.layer.cornerRadius = arg1
        print(arg2)
        print("Instantiated")
    }

    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

required init(coder) , .

, CustomTextField Identity Inspector, , :

class CustomTextField: UITextField {

    override func awakeFromNib() {
        super.awakeFromNib()

        layer.cornerRadius = 5
        layer.borderColor = UIColor.greenColor().CGColor
        layer.borderWidth = 1.0

        // Set other stuff like font color etc.

        print("Instantiated")
    }
}
+9

, , , .

import UIKit

class MyTextView: UITextView {

    var myValue:Int
    var myText:String


    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override init(frame: CGRect, textContainer: NSTextContainer?) {
        myText = String()
        myValue = Int()
        super.init(frame: frame, textContainer: textContainer)
    }

}
0

All Articles