Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

How to display a new view from a nib ?

how to

display a new view from a nib in the main window after clicking on a button.


When I click a the button named for example "goToGokyo", the link to my procedure

runs correctly because I see my NSLog message "goToGokyo button pressed".

My nib, which name is "GokyoViewController" does exist.


I did as follow for the moment but I'm stuck now, my nib :



- (IBAction)goToGokyo:(id)sender

{

NSLog(@"goToGokyo button pressed");


//[self stopSound];


if (gokyoViewController == nil)

{

GokyoViewController *aGokyoViewController = [[GokyoViewControlleralloc]

initWithNibName:@"GokyoViewController" bundle:nil];

self.gokyoViewController = aGokyoViewController;

//[aGokyoViewController release];

}

[self GokyoView:gokyoViewController animated:YES completion: nil];

}



Thanks in advance for your advice.

Cheers

Posted on Jan 21, 2014 2:21 AM

Reply
17 replies

Jan 21, 2014 8:45 AM in response to Donald Laborde

Oh ok, bad formulation sorry !


For example, the excellent Apple's sample code "NibLoading" included in "CocoaTipsAndTricks".

The main window has two buttons, when I click on one of theses buttons, the app show a new

window as follow :


- (IBAction)btnLoadThirdWindowWithController:(id)sender {

// Using a window controller is even easier, and generally better for management for code.

if (_myWindowController == nil) {

_myWindowController = [[MyWindowController alloc] initWithWindowNibName:@"ThirdWindow"];

}

[_myWindowController.window makeKeyAndOrderFront:nil];

}


Now suppose I make a new nib file as View and instead of displaying a new window, I would like to

display that new view in the same main window instead of what it was at start. How to do that please ?


Note : sorry for my bad english (mother language french). Hope I will be understood now ?

Jan 21, 2014 10:52 AM in response to red_menace

Not exactly what I did :


- (IBAction)btnLoadMyViewController:(id)sender

{

if (myViewController == nil)

{

myViewController = [[MyViewController alloc] initWithNibName:@"MyView"];

}


//???;

}


Use of undelcared identifier 'myViewController'

No visible @interface for 'MyViewController' declares the selector 'initWithNibName:'


But, in the NibLoadingAppDelegate.h I do:


@property (strong) IBOutlet NSViewController *myViewController;

@property (weak)IBOutletMyViewController *view;

and in the NibLoadingAppDelegate.m I do:

#import "MyViewController.h"

Jan 21, 2014 10:56 PM in response to red_menace

And the problem is maybe in "NibLoadingAppDelegate.h" :


#import <Cocoa/Cocoa.h>


@class MyWindowController;


@interface NibLoadingAppDelegate : NSObject <NSApplicationDelegate>


@property (strong) IBOutlet NSWindow *window;

@property (weak) IBOutlet NSWindow *secondWindow;

@property (strong) IBOutlet MyWindowController *myWindowController;

@property (weak) IBOutlet NSView *view;


- (IBAction)btnManualSecondWindowLoadClick:(id)sender;

- (IBAction)btnLoadThirdWindowWithController:(id)sender;

- (IBAction)btnLoadMyViewController:(id)sender;


@end


then NibLoadingAppDelegate.m


- (IBAction)btnLoadMyViewController:(id)sender

{

NSViewController *viewController = [[NSViewController alloc] initWithNibName:@"MyView" bundle:nil];

NSView *view = [viewController view];

[_window setContentView:view];

//[myView addSubview:view];

}


nothing hapend when I click on my button

Jan 22, 2014 10:34 AM in response to red_menace

I did that now :


- (IBAction)btnLoadMyViewController:(id)sender

{

NSViewController *viewController = [[NSViewController alloc] initWithNibName:@"MyView" bundle:nil];

NSView *view = [viewController view];

[_myWindowController.windowsetContentView:view];

//view addSubview:view];

//[self.myView addSubview:view];

}


Using

"_myWindowController.windowsetContentView:view];"


or


"[self.myViewaddSubview:view];"


does not an error but does nothing !


Would you help me one more times please ?


Thanks in advance

How to display a new view from a nib ?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.