1

I would like to use an NSBox *dynamicSection to replace the content of the box with a different view depending on the index selected from and NSPopUpButton control. The method below receives the NSPopUPButton as an object, and uses a case switch to dynamically set the view and title for the box.

@interface AppDelegate : NSObject <NSApplicationDelegate> {
IBOutlet NSTextField *dynamicTitle;
NSMutableString *title;
NSBox *dynamicSection;
NSView *Sect1_View;
NSView *Sect2_View;
NSView *Sect3a_View;
NSView *Sect3b_View;
NSView *Sect3c_View;
NSView *Sect4_View;
}
@property (assign) IBOutlet NSWindow *window;
@property (assign) IBOutlet NSBox *dynamicSection;
@property (assign) IBOutlet NSPopUpButton *menuOptions;

}

@implementation {

- (IBAction)menuSelected:(NSPopUpButton *)sender {

NSInteger index = [sender indexOfSelectedItem];
NSLog(@"Selected button index is %ld", index);



switch (index) {
    case 0:
        dynamicSection = [[NSBox alloc] init];
        [dynamicSection setTitle:[self returnSectionTitle:index]];
        [dynamicSection setContentView:Sect1_View];
         NSLog(@"%@",[self returnSectionTitle:index]);
        break;
    case 1:
        dynamicSection = [[NSBox alloc] init];
        [dynamicSection setTitle:[self returnSectionTitle:index]];
        [dynamicSection setContentView:Sect2_View];
        break;
    case 2:
        dynamicSection = [[NSBox alloc] init];
        [dynamicSection setTitle:[self returnSectionTitle:index]];
        [dynamicSection setContentView:Sect3a_View];
        break;
    case 3:
        dynamicSection = [[NSBox alloc] init];
        [dynamicSection setTitle:[self returnSectionTitle:index]];
        [dynamicSection setContentView:Sect3b_View];
        break;
    case 4:
        dynamicSection = [[NSBox alloc] init];
        [dynamicSection setTitle:[self returnSectionTitle:index]];
        [dynamicSection setContentView:Sect3c_View];
        break;
    case 5:
        dynamicSection = [[NSBox alloc] init];
        [dynamicSection setTitle:[self returnSectionTitle:index]];
        [dynamicSection setContentView:Sect4_View];
        break;

    default:
        break;
  }

}

}

It is recognizing the correct index, and printing the title to the log, however it does not correctly switch the view upon selection. Any suggestions?

Thanks!

1 Answers1

0

You don't appear to be adding the NSBox as a subview of the view and I cannot tell from the question where it should be added.

Other issues:

  1. You need to avoid a memory leak by releasing the allocated NSBox once you have added it as a subview, as the view will retain it.
  2. You probably don't need to hold dynamicSection as an ivar of the class.
  3. You have too much repeated code:

Do this before the switch:

dynamicSection = [[NSBox alloc] init];
[dynamicSection setTitle:[self returnSectionTitle:index]];

and add the view after the switch:

[someView addSubview:dynamicSection];
[dynamicSection release];
trojanfoe
  • 120,358
  • 21
  • 212
  • 242
  • Thanks for the advice, I have made the changes before and after the switch statement. Do I need to create a custom view inside the NSBox as a subview? – user1505130 Jul 05 '12 at 21:11
  • No, you need to add the `NSBox` **to** a view. – trojanfoe Jul 05 '12 at 21:12
  • Would you be able to provide an example? I currently positioned the NSBox inside the main view in Nib associated with the AppDelegate. – user1505130 Jul 05 '12 at 21:19
  • Have you got Aaron Hillegass's `Cocoa Programming for Mac OS X - 4ed`? Chapter 31 presents a view swapping example that uses an `NSBox`. – trojanfoe Jul 05 '12 at 21:22
  • Ok great, thanks. I just found a separate example which instantiates the view controller subclass: Sect1 = [[Sect1_View alloc] initWithNibName:@"Sect1_View" bundle:nil]; [dynamicSection setContentView:Sect1.view]; [dynamicSection release]; – user1505130 Jul 05 '12 at 21:39