根据选择的索引和NSPopUpButton控件,我想使用一个NSBox *Dynamic节将该框的内容替换为不同的视图。下面的方法接收NSPopUPButton作为对象,并使用case开关动态设置框的视图和标题。
@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;
}
}}
它正在识别正确的索引,并将标题打印到日志中,但是它在选择时不能正确地切换视图。有什么建议吗?
谢谢!
发布于 2012-07-05 20:55:37
您似乎并没有将NSBox添加到视图的子视图中,从这个问题上看不出应该在哪里添加它。
其他问题:
NSBox添加为子视图,则需要通过释放分配的来避免内存泄漏,因为视图将保留它。dynamicSection作为类中的一员。在switch之前这样做
dynamicSection = [[NSBox alloc] init];
[dynamicSection setTitle:[self returnSectionTitle:index]];并在switch之后添加视图
[someView addSubview:dynamicSection];
[dynamicSection release];https://stackoverflow.com/questions/11352303
复制相似问题