Why aren't my views appearing?
I'm following a very basic book tutorial on Mac development. We're just creating a really simple interface. All I'm trying to do is drag a label on to the view, and run it and have that label displayed. However, I'm using the newest version of Xcode, and I'm doing just that-just dragging a label on to th开发者_开发知识库e view, typing in some text, saving it, then running it. However, the app window is empty when I run it. Why is my label not appearing? Even any buttons that I've dragged are not appearing. Is there something special I have to do in this new Xcode that I'm missing here?
Make sure you drag the label, buttons, etc. into a view that you're putting into a window that you're ordering in.
What you're probably seeing on startup is the window in the main-menu nib. That window, by default, is empty. It will remain so until you explicitly put something into it, either in code (with addSubview:
messages to its content view or an existing subview thereof) or in the nib (by adding the label, buttons, etc. to the window's content view, or an existing subview thereof, instead of another view in another nib).
If you want to add the label, buttons, etc. to a view in another nib, then you must load that nib and then add the view you get from it as a subview of one of the views in the window. Or, create a window in that nib instead, but you still need to load the nib and make sure that window gets ordered in.
Also:
I'm following a very basic book tutorial on Mac development. … I'm doing just that-just dragging a label on to the view, typing in some text, saving it, then running it.
If the book was written for Interface Builder, then yes, this is something Xcode 4 does differently. Xcode 4 killed off the separate Interface Builder application, integrating nib editing into itself.
When you hit ⌘R in Interface Builder, that would run the Cocoa Simulator, which would present the interface objects in your nib. That's what the book (if it was written for Interface Builder) is telling you to do: Hit ⌘R in Interface Builder, not in Xcode.
In Xcode, pressing ⌘R runs your app. In Xcode 4 and 5, you can simulate the nib by choosing “Simulate Document” from the Editor menu while editing a nib. That will launch the Cocoa Simulator and preview the contents of your nib in it.
(Also, don't confuse the Cocoa Simulator in Interface Builder with the iOS Simulator in the iOS SDK. The iOS Simulator is for actually running the app on a Mac rather than an iOS device; the Cocoa Simulator is for previewing nib contents without running the app.)
精彩评论