Xcode 4: Creating a Uiview Xib, Not Properly Connecting

Xcode 4: Creating a UIView xib, not properly connecting

This might not work for your specific issue, but occasionally I get that error when working with newly created nibs. Deleting and recreating the nibs and View Controllers with the same names as before didn't resolve the issue, but relaunching Xcode did.

Why when create new UIView class, can't create a XIB together?

If I create a new UIViewController class, I could also checked the box to Also create XIB file. However, why when I create UIView, the check box is disabled, and I have to manually create a XIB to link the two (XIB and the UIView) together? [Emphasis mine.]

Because the relationship between a view controller and a nib is totally different from the relationship between a view and a nib. UIView and nibs do not "go together" in any magic or important way, as do a UIViewController and its view nib.

  • With a view controller, if there is a nib with the same name as the view controller class, and if the File's Owner in that nib is typed as the class of the view controller, and if the view outlet of the File's Owner is pointed at the top-level UIView in the nib, the view controller can load its view automatically from the nib. That is a complicated arrangement, and it is doubtful that you would know how to configure it correctly (and it's a lot of work even if you do know how), so the template offers to configure it for you. This is a standard, important, automatic relationship.

  • But with a view and a nib there is no such standard automatic relationship, and there is no complexity. If you want a certain view in a certain nib to be of a certain UIView subclass, you just say so in its Identity inspector, and kaboom you're done. So just do that and move on.

Xcode 4: Creating a UIView xib, not properly connecting

This might not work for your specific issue, but occasionally I get that error when working with newly created nibs. Deleting and recreating the nibs and View Controllers with the same names as before didn't resolve the issue, but relaunching Xcode did.

Xcode 4: Creating a UIView xib, not properly connecting

This might not work for your specific issue, but occasionally I get that error when working with newly created nibs. Deleting and recreating the nibs and View Controllers with the same names as before didn't resolve the issue, but relaunching Xcode did.

Cannot connect IBOutlet of UIView declared in protocol to a xib entity

A protocol only lays out a blueprint as to what needs to be in a class. Declaring the IBOutlet in the protocol does not magically add this property to any class that adheres to the protocol.

You have to add the IBOutlet UIView* to the interface of SomeClass, then you will be able to connect it.

Additionally, it makes no sense at all to declare an IBOutlet in a protocol.
A protocol should declare an interface or API - the means of implementation should be up to the class adhering to a protocol. Therefore it is questionable to declare properties at all, but you should rather declare methods (getters or setters).

In your case that means, that you would add this to your protocol:

- (UIView*)view;

and your class SomeClass can choose to implement this via a property like this:

@property (nonatomic, strong) IBOutlet UIView* view;

XIB not outletting to class

A couple of things to try:

  • Clean project using Product > clean
  • Close/reopen Xcode
  • Delete the Derived Data folder (Preferences > Locations and clicking the gray arrow by the Derived Data folder, then delete)
  • Delete AddEventView, remove reference (do not move to trash). Then add it back again.

Can't connect IBOutlet in Interface Builder

I did a combination of the things in this thread and I finally had success.

In Interface builder (not xcode)

  1. File->Reload All Class Files
  2. File->Read Class Files (select MyClass.h)
  3. Reconnect File's Owner by

    a. Setting the Class to "MyClass"

    b. Reconnecting the View to the File's Owner's View

Everything is back to normal now. Weird.

Hope this helps more than it confuses ;-)

programmatically customize UIView in xib

awakeFromNib will be called only if call UINib like this: https://stackoverflow.com/a/25513605/846780

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