I come from Obj-C and I'm struggling on doing something super basic in Swift!
I have a custom UICollectionViewCell:
class CustomCell: UICollectionViewCell
{
// Outlets
// ***************************
#IBOutlet weak var button: UIButton!
// Init
// ***************************
required init?(coder aDecoder: NSCoder)
{
super.init(coder:aDecoder)
setup()
}
override init(frame: CGRect)
{
super.init(frame: frame)
setup()
}
func setup()
{
button.backgroundColor = .white
}
}
The cell is loaded from an external .xib file, so init(coder:) is called for the initialization but my button is not ready.
If I change to button?.backgroundColor the app doesn't crash but obviously nothing happen.
I can call my setup() function in layoutSubviews() and it works, but it's definitely not the right place to be.
How do I solve this massive problem? lol
Edit
Probably I have to call setup() from awakeFromNib(), right?
I usually don't use external .xib, I'm not familiar with them
Edit: Sorry It seems youe edited your question before my answer, it seems as you load it from XIB, then you can run the awakeFromNib which will be called when you register a nib using this method:
Apple Source UICollectionView
Apple Source UITableView
--- old post below ---
In Xcode 6 you have to provide additional init(coder:) initializer in
classes like RDCell, which is the subclass of UICollectionViewCell.
This initializer is called instead of init(frame:) when the class gets
initialized from a storyboard or a xib file. That’s not our case, but
we still need to provide init(coder:). We can use the solution
provided to us by Xcode. In Issue Navigator click on an error that
says “'required' initializer 'init(coder:)' must be provided by
subclass of 'UICollectionViewCell'“,
Source
Related
I want to add a tab recognizer to a custom .xib file view in my ios swift app. Here's the code from the owner class of the .xib file:
import UIKit
class WordLabel: UILabel {
#IBOutlet weak var wordLabel: UILabel!
#IBOutlet var wordFrame: UIView!
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
//I added the tab recognizer here
wordLabel.isUserInteractionEnabled = true
let gestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(labelClicked(_:)))
wordLabel.addGestureRecognizer(gestureRecognizer)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
func commonInit() {
Bundle.main.loadNibNamed(K.wordLabelNibName, owner: self, options: nil)
addSubview(wordFrame)
wordFrame.frame = self.bounds
wordFrame.autoresizingMask = [.flexibleWidth, .flexibleHeight]
}
//should happen when label is tapped
#objc func labelClicked(_ sender: Any) {
print("UILabel clicked")
}
}
When I ran the project on a phone simulator, there were no errors.
App Running On The Simulator
But when I clicked the labels that showed up, the message was not printed onto the console (meaning that the action was not triggered). What am I doing wrong?
Thank you for your help.
github link to my project:
https://github.com/ShaungYi/PolyLibrum/blob/main/PolyLibrum/View/BookReader/WordLabel/WordLabel.swift
When you use WordLabel custom UILabel with xib, init(frame:) constructor is not called. init(coder:) is called instead. Because of that, your gesture recognizer doesn't work. You must to move the gesture recognizer assignment to commonInit() method.
I solved the problem on my own. It seems that I set the owner class of the .xib file to inherit from a UILabel. When I changed the superclass to the generic UIView, everything worked out perfectly! I don't really understand how this fixed the problem, but my theory is that previously, the owner class relegated the UIGesture event to the UILabel class instead of itself, thus not triggering the bound gesture recognizer. Now That I diverted that to the owner class, there's no such problem.
So for anyone else who has my problem- set the owner class' superclass to the generic UIView if possible?
Thanks again to Furkan Kaplan for his help.
When I create a subclass of UIView or UIViewController with a stored property, Xcode will not compile my project unless I include an implementation of required init?(coder aDecoder: NSCoder). Currently, I have the following implementation to shut the compiler up:
required init?(coder aDecoder: NSCoder) {
fatalError()
}
I understand why I'm required to include this initializer; my subclass needs to conform to the NSCoding protocol because its superclass conforms to it, and this initializer is part of the NSCoding protocol so it needs to work with my class, i.e. initialize all of my class's stored properties (which the superclass version of the initializer won't do).
I imagine that a correct implementation would look something like this:
class MyView: UIView {
let label: UILabel
override init(frame: CGRect) {
label = UILabel()
super.init(frame: frame)
}
required init?(coder aDecoder: NSCoder) {
if let label = aDecoder.decodeObject() as? UILabel {
self.label = label
} else {
return nil
}
super.init(coder: aDecoder)
}
override func encode(with aCoder: NSCoder) {
aCoder.encode(label)
super.encode(with: aCoder)
}
}
However, considering that my application has over 50 custom views and view controllers, correctly implementing this function in every custom view and view controller is a lot of work.
So, I'm wondering if it's necessary to implement this initializer correctly, or if I can just leave it throwing a fatal error. In other words, will this initializer ever be called if I don't call it in my own code? I think I read that it might be called by a Storyboard, but my app doesn't use any Storyboards.
This initialiser will be called if an instance of your view is used in a storyboard scene.
It is up to you whether to create a functioning initialiser or not, but it should mostly be a matter of copying code from init(frame:)
It provides an NSCoder instance as a parameter, which you need only if you are using iOS serialization APIs. This is not used often, so you can ignore it. If you are curious to learn, serialisation converts an object in a byte stream that you can save on disk or send over the network.
During the initalization of a view controller, you usually allocate the resources that the view controller will need during its lifetime. So, this include model objects or other auxiliary controllers, like network controllers.
I was trying to work with custom tableview cells for the tableview in my project. Right now I have got the tableview working using the custom cell class that I have created. I am using XIB for the cell.
But now the problem is that I want to add a gradient layer & an extra label as properties to my cell class and I want to get them initialize. I am writing the code for these properties in the init() like
override init(){
//Code to initialize the properties of cell
.....
super.init()
}
And I have also have the required init() which is like this
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
But its not working its just giving an error at required init().
I know how to get this done in Objective-C but have no idea how to do this in swift. Please give me some pointers on how to get this done.
EDIT : Yeah I did try initializing the properties in the awakeFromNib() where I got error saying I can't assign to property in self. And in init(coder:) also I was unable to initialize the properties.
Thanks in advance :)
Something wrong in your code. I create new project, create custom UITableViewCell via nib, and put such code, and it works fine:
override func awakeFromNib() {
super.awakeFromNib()
self.textLabel.text = "testing cells";
self.detailTextLabel?.text = "text";
}
Did you register cell's nib in table view?
self.tableView.registerNib(UINib(nibName: "TableViewCell", bundle: nil), forCellReuseIdentifier: "Cell");
Also did configure right the nib in Interface Builder? Does you change UITableViewCell class to your custom class?
Here is my view class
class V_TakePhoto:UIView{
var _takePhotoCallback:(iImage)->Void?
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
_takePhotoCallback = nil
}
#IBAction func takePhoto(sender: AnyObject) {
println("Here we go!")
}
func initWithCameraCallback((iImage)->Void)
{
}
}
This class is a UIView subclass. In the interface builder I selected the ViewController class, and then selected its View object. I assigned V_TakePhoto to this view object.
In the ViewController class, which I assigned to C_TakePhoto class, I want to init the V_TakePhoto class.
As you can see, I want it to have a callback variable that it gets passed at run time. However, because the view is already getting initialized from the interface builder, init(coder) is getting called first.
As it stands right now it seems hacky that I need to have 2 init functions. One where interface builder calls it, then again when my ViewController inits the view with its callback. Also I will have a number of variables, and I need to pre-init them in the init(coder) call then RE-init them again when the ViewController calls the 'true' init on the V_PhotoClass. Seems very hacky to me, there must be a clean 'correct' way to do this.
Can you suggest a cleaner way to handle a situation where you have variables and need to init a view despite there being an init(coder) call from the interface builder?
I would suggest creating a function in V_TakePhoto and call it in both V_TakePhoto's init(coder) and ViewController's viewDidLoad(), something like :
In V_TakePhoto :
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
specialInit()
}
func specialInit() {
// some of your view initialization
}
In your View Controller :
#IBOutlet weak var takePhotoView: V_TakePhoto!
override func viewDidLoad() {
super.viewDidLoad()
// this method is called after the view controller has loaded its view hierarchy into memory.
takePhotoView.specialInit() // RE-init
}
When inheriting from UIControl my app crashes as soon as I add a property to my UIControl Class and a hit test is performed (EXC_BAD_ACCESS) => E.g. mouse over the Control:
class ReloadButton: UIControl {
var stopRotating: Bool = true
init(coder aDecoder: NSCoder!)
{
super.init(coder: aDecoder)
}
init(frame aRect: CGRect){
super.init(frame: aRect)
}
}
If I remove the property stopRotating it won't crash. If I change the inheritance to UIButton instead of UIControl the crash won't happen.
Is there a specific function which needs to be added to UIControl to handle hit tests?
UPDATE: I created a minimalistic sample project on github: https://github.com/Aranir/hit_test
With the beta 5 Xcode the error messages have become more explicit.
Apparently the method
override init(){
super.init()
}
needed to be implemented for it to work. This is now also necessary if the class inherits from UIButton