Reputation: 33297
I have the following init method which worked perfect before Swift 1.2.
init(items: NSArray, views: NSArray, showPageControl: Bool, navBarBackground: UIColor){
super.init()
In Swift 1.2 the line with super.init() raises the error:
Must call a designated initializer of the superclass 'UIViewController'
The problem is that I have only two possible super init methods to call the one is with a codec
and the other is with a nib
. Both of which I do not have access to in this init method.
See the full class here the line of the error is this.
How can I correct the init method to make it work with Swift 1.2?
class SLPagingViewSwift: UIViewController, UIScrollViewDelegate {
init(items: NSArray, views: NSArray, showPageControl: Bool, navBarBackground: UIColor){
super.init()
needToShowPageControl = showPageControl
navigationBarView.backgroundColor = navBarBackground
isUserInteraction = true
var i: Int = 0
for item in items{
if item.isKindOfClass(UIView.classForCoder()){
var v = item as UIView
var vSize: CGSize = v.isKindOfClass(UILabel.classForCoder()) ? self.getLabelSize(v as UILabel) : v.frame.size
var originX = (self.SCREENSIZE.width/2.0 - vSize.width/2.0) + CGFloat(i * 100)
v.frame = CGRectMake(originX, 8, vSize.width, vSize.height)
v.tag = i
var tap = UITapGestureRecognizer(target: self, action: "tapOnHeader:")
v.addGestureRecognizer(tap)
v.userInteractionEnabled = true
self.navigationBarView.addSubview(v)
self.navItems.addObject(v)
i++
}
}
if (views.count > 0){
var controllerKeys = NSMutableArray()
i = 0
for controller in views{
if controller.isKindOfClass(UIView.classForCoder()){
var ctr = controller as UIView
ctr.tag = i
controllerKeys.addObject(NSNumber(integer: i))
}
else if controller.isKindOfClass(UIViewController.classForCoder()){
var ctr = controller as UIViewController
ctr.view.tag = i
controllerKeys.addObject(NSNumber(integer: i))
}
i++
}
if controllerKeys.count == views.count {
self.viewControllers = NSDictionary(objects: views, forKeys: controllerKeys)
}
else{
var exc = NSException(name: "View Controllers error", reason: "Some objects in viewControllers are not kind of UIViewController!", userInfo: nil)
exc.raise()
}
}
}
}
Upvotes: 1
Views: 6554
Reputation: 535306
You are writing a view controller class for other people to use as part of their own app. If this view controller could have a nib file, then what you are doing would be wrong and would always have been wrong: it would be up to you to allow the caller to supply the nib file name as one of the parameters of your initializer, so that you could call init(nibName:bundle:)
and pass that value along.
But I gather that your view controller just uses the default empty automatic view, so this is not a problem in actual fact.
Therefore what you'll do is call init(nibName:bundle:)
, and since this view controller has no nib file, just pass nil
for the nib name. This in fact is what super.init()
was doing in the earlier version of Swift - it was calling init(nibName:bundle:)
with nil
values for you. So nothing is really lost or changed.
Upvotes: 5