Does Not Have a Member 'Instantiatewithowner'

awakeFromNib not called when using instantiateWithOwner: method

Actually you need to customize in the code you showed 'other stuff here'. Since you recycle cells you need to insure the cell is configured correctly at each usage.

EDIT: I made a test project, and verified awakeFromNib is in fact sent to the class. This leads me to believe the root problem is that in your nib with the cell, you have not set the class of the cell to your CustomClass name. Its the only possible explanation.

EDIT2: So to reiterate:

  • in CustomTableViewCell.xib, the class of the object is set to 'CustomTableViewCell'

  • add some log messages in CustomTableViewCell.m - in the initWithCoder: if you have it and also in awakeFromNib (make sure of spelling!!!)

  • when you instantiate here 'cell = [topLevelItems objectAtIndex:0];', add a log before NSLog(@"Going to instantiate"), the afterwards a log:

    NSLog(@"Instantiate a %@ cell", NSStringFromClass([cell class]);

Something has to give here. What is your deployment target? In my test I used 5.1

Instantiate view from nib throws error

The loadViewFromNib() method you post looks fine, you're getting the bundle correctly and specifying the nib name as a string literal, so it should find the nib. As someone else commented, there's probably something wrong with your set up of the nib instead. Here are a few things to check:

  1. You should have AdvancedCellView set as the File's Owner Custom Class in the Identity Inspector.
  2. Check that the module is correct too - usually you just want it blank.
  3. The Custom Class for the View should not be set, just leave it as the default (UIView). If you have a more descriptive name than 'View' in the sidebar, it's probably wrong.
  4. Check that you only have one top level object in the nib. The sidebar should look like this, without any other entries, when you have the View tree collapsed.

Nib hierarchy with only one top level view

N.B. This answer is related to the tutorial that the OP linked to, and not the only correct way to set up a nib.

UIWindow? does not have member named bounds

The declaration of the window property in the UIApplicationDelegate protocol
changed from

optional var window: UIWindow! { get set } // beta 4

to

optional var window: UIWindow? { get set } // beta 5

which means that it is an optional property yielding an optional UIWindow:

println(UIApplication.sharedApplication().delegate.window)
// Optional(Optional(<UIWindow: 0x7f9a71717fd0; frame = (0 0; 320 568); ... >))

So you have to unwrap it twice:

let bounds = UIApplication.sharedApplication().delegate.window!!.bounds

or, if you want to check for the possibility that the application delegate has
no window property, or it is set to nil:

if let bounds = UIApplication.sharedApplication().delegate.window??.bounds {

} else {
// report error
}

Update: With Xcode 6.3, the delegate property is now also
defined as an optional, so the code would now be

let bounds = UIApplication.sharedApplication().delegate!.window!!.bounds

or

if let bounds = UIApplication.sharedApplication().delegate?.window??.bounds {

} else {
// report error
}

See also Why is main window of type double optional? for more solutions.

Why is my popup view is empty?

You need to load manually the xib associated to your view.
I use this extensions (found at this link) modified to support UIViewController and then I call it when needed.

extension UIViewController {
class func loadFromNibNamed(nibNamed: String, bundle : NSBundle? = nil) -> Self? {
return UINib(
nibName: nibNamed,
bundle: bundle
).instantiateWithOwner(nil, options: nil)[0] as? UIViewController
}
}

then

@IBAction func showPopup(sender: AnyObject) {
var x = PopupViewController.loadFromNibNamed("popup")
//do what you need with x
}

xib file button issues

myMenuVC is a UIView subclass, not a UIViewController subclass.

You can either create a new view controller and re-create this UIView interface on the view controller's view.

You can also present the alert controller on the application's root view controller from the myMenuVC UIView subclass directly:

UIApplication.sharedApplication().delegate?.window??.rootViewController?.presentViewController(alert, animated: true, completion: nil)

How to initialize/instantiate a custom UIView class with a XIB file in Swift

I tested this code and it works great:

class MyClass: UIView {        
class func instanceFromNib() -> UIView {
return UINib(nibName: "nib file name", bundle: nil).instantiateWithOwner(nil, options: nil)[0] as UIView
}
}

Initialise the view and use it like below:

var view = MyClass.instanceFromNib()
self.view.addSubview(view)

OR

var view = MyClass.instanceFromNib
self.view.addSubview(view())

UPDATE Swift >=3.x & Swift >=4.x

class func instanceFromNib() -> UIView {
return UINib(nibName: "nib file name", bundle: nil).instantiate(withOwner: nil, options: nil)[0] as! UIView
}


Related Topics



Leave a reply



Submit