开发者

How to pragmatically create and load multiple views in a Cocoa Desktop Application?

I've been trying to create multiple custom views generating xibs and connecting controllers to them. i have tried things like

[window AddSubView:view0]

开发者_如何学运维and

[window setContentView:view0]

but i still don't get the new view displayed.

i think I'm doing it wrong since creating the xib file.

what is the right way from creating the xib files and the controllers to displaying custom views?

Thanks for your help.


[window AddSubView:view0]

Windows aren't views in Cocoa, and nothing responds to AddSubView: anyway. (Remember that selectors are case-sensitive.)

[window setContentView:view0]

That is valid, but you'd want to make sure that view0's frame has the same size as window's content rectangle.

but i still don't get the new view displayed.

One possibility is that you are not actually talking to a window. You probably either haven't hooked up your window outlet or haven't loaded the nib with that window in it yet or, if window isn't an outlet and the window doesn't exist in a nib, you haven't created it in code yet. Either way, your window variable is nil, so you are sending messages such as addSubview: to nil, which does nothing.

If you have proven with NSLog or the debugger that you do, in fact, have a window to send messages to, and you still have the problem, make sure you are ordering the window in. If the window is in a nib, you may want to turn on “Visible at Launch” (which actually means “Visible at Nib Load”); otherwise, order the window in yourself by sending it a makeKeyAndOrderFront: message.


We'll after some... hard researching i got things working the way i want

here's my code


- (IBAction)showFirstView:(id)sender{
    theDetailViewController = [DetailViewController new];
    [theDetailViewController initWithNibName:@"DetailView" bundle:nil];
    NSView *splitRightView = [[theSplitView subviews] objectAtIndex:1];
    NSView *aDetailView = [theDetailViewController view];
    [aDetailView setFrame:[splitRightView bounds]];
    [aDetailView setAutoresizingMask:(NSViewWidthSizable | NSViewHeightSizable)];
    [splitRightView addSubview:aDetailView];
    NSLog(@"%@",(NSString *)splitRightView);
}

what i was doing wrong was creating a class as NSView instead of NSViewController and trying to load the view just like that but you need to create a NSViewController and set this as the File's Owner class of the custom view XIB and then set the outlet VIEW to point at your custom view.

Thanks ya'll 4 your help.


Aaron Hillegass - Cocoa Programming for Mac OS X (3rd edition), Chapter 29. View Swapping

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜