Windows and views and controls, oh my!
Windows and views and controls, oh my!
- Subject: Windows and views and controls, oh my!
- From: William Squires <email@hidden>
- Date: Mon, 05 Sep 2011 10:32:22 -0500
Okay, I'm having a tizzy here. Most of the '...learn MacOS X...' programming books gloss over the nitty-gritty of actually creating and displaying various windows, either assuming they're going to be document windows (as part of a document-based app), or they're just there as part of an example of some other topic.
While I can create the UI easily enough in IB, what I don't understand is how to actually load, instantiate, and display the window from my view controller object.
If it were REALbasic, I would create a window subclass (wndMain, say), put my UI on it, then - from somewhere else - I would do:
...
Dim w as wndMain
w = New wndMain() // like [[wndMain alloc] init];
w.myProp1 = "Foo" // Declared as a Public property of type String
w.myProp2 = 3 // ditto, but declares as an Integer
... // any other property setups...
w.Display() // calls wndMain::Display
If (w.ButtonClicked() = 1) Then
// User accepted (clicked OK button)
Else
// User cancelled (click Cancel button)
MsgBox "Aborted."
End If
w = nil // GC called here, in ObjC would be [w release]; followed by w = nil;
...
Sub wndMain::Display
Me.Setup() // Private method to set the control's visual state based on myProp1, myProp2, etc...
Me.ShowModal() // or Me.Show, for a non-modal window
End Sub
and...
wndMain
Public Property myProp1 As String
Public Property myProp2 As Integer
Protected Property fButtonClicked As Integer
// Effectively an @property (nonatomic, readonly) NSInteger fButtonClicked
Public Function ButtonClicked() As Integer
Return fButtonClicked
End Function
Private Sub wndMain::Setup()
lblFoo.Caption = myProp1
popBar.ListIndex = myProp2 // presumably, the 4th item in the popup menu
...
End Sub
where wndMain would be implemented as the view controller, and the actual 'window' would be in IB. I realize the view controller has to load the nib, but how do I:
1) make sure the window isn't displayed until all it's views (controls) have had a chance to initialize their default (visual) state properly
2) actually display the window, either modally, or not.
3) if modal, how does my client code make sure it doesn't return from the operation until the user closes the window (by clicking the close box, or by dismissing it in code from an IBAction tied to an NSButton whose caption is "Done" or some such.)
4) Pass information to/from the client code and the view controller?
5) Prevent the window from closing (if non-modal) if the contents are 'dirty'?
I prefer to actually make each window and its view controller as a pair, rather than rely on the provided window in MainWindow.xib; this way I'll have to actually know how to tie a .xib and a view controller together, rather than rely on whatever may be in the provided MainWindow.xib in the blank project
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden