0

I have different xib files with NSViewController attached to them. (Screenshot below)

enter image description here

One of xib file called StartMenuViewController which has a button. I want to click that button and change the view to DetectingUSBViewController.(Screenshot below) The IBAction of that button is in StartMenuViewController.m file.

enter image description here

And I use AppController.m to control my main xib view.(NSWindow + NSView) (Screenshot below) enter image description here

When the application runs, I try to initialize the StartMenuViewController fist by doing the following thing in my AppController.m file.

-(void)awakeFromNib{   
    [self initialize];
}

-(void) initialize
{
    @autoreleasepool {
        //mainViewController is a NSViewController and _mainView is a NSView which connect with Custom View in main xib
        self.mainViewController = [[[StartMenuViewController alloc]initWithNibName:StartMenuView bundle:nil]autorelease];
        [_mainView addSubview:[_mainViewController view]];
    }
}

It works fine and it will show the StartMenuViewController.xib on the window at first, but I do not know how to change the view after clicking the button(FIND USB DRIVE). I want the current view changes to DetectingUSBViewController.xib.

Cœur
  • 37,241
  • 25
  • 195
  • 267
YU FENG
  • 888
  • 1
  • 12
  • 29

3 Answers3

0

Simplest way possible, assuming you have tied your USB button properly in, do the following :

- (IBAction)usbButton:(UIButton *)sender {

    DetectingUSBViewController *second = [[DetectingUSBViewController alloc] initWithNibName:@"DetectingUSBView" bundle:nil];
    [self presentViewController:second animated:YES completion:nil];

}
kgdesouz
  • 1,966
  • 3
  • 16
  • 21
0

load the DetectingUSBViewController in startMenuViewController as DetectingUSBViewController* v1 = [[ViewCont1 alloc] initWithNibName:@"ViewCont1" bundle:nil]; now add or replace the view as [v1 view] in view where you want to add/replace.

user23790
  • 563
  • 3
  • 21
0
  1. You need to hook up your button to send an IBAction
  2. You need a 'View for DetectingUSBViewController.xib' => one way (iOS like) is to use a ViewController. Subclass NSViewController and then alloc init a DetectingUSBViewController
  3. Add the view. Don't present the VC (as there is no such thing in OSX)

//button click action
- (IBAction)usbButton:(UIButton *)sender {
     //! Retain the VC
     Self.detectingUSBViewController = [[DetectingUSBViewController alloc] initWithNibName:@"DetectingUSBView" bundle:nil];

     //add the view
     [_mainView addSubview:[_detectingUSBViewController view]];

}

Daij-Djan
  • 49,552
  • 17
  • 113
  • 135
  • The _mainView is in AppController class rather than StartViewController class. How to call _mainView in here? Thanks – YU FENG Jun 01 '13 at 20:38