首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用NSBox替换子视图

用NSBox替换子视图
EN

Stack Overflow用户
提问于 2012-07-05 20:45:40
回答 1查看 1.1K关注 0票数 1

根据选择的索引和NSPopUpButton控件,我想使用一个NSBox *Dynamic节将该框的内容替换为不同的视图。下面的方法接收NSPopUPButton作为对象,并使用case开关动态设置框的视图和标题。

代码语言:javascript
复制
@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;
  }

}

}

它正在识别正确的索引,并将标题打印到日志中,但是它在选择时不能正确地切换视图。有什么建议吗?

谢谢!

EN

回答 1

Stack Overflow用户

发布于 2012-07-05 20:55:37

您似乎并没有将NSBox添加到视图的子视图中,从这个问题上看不出应该在哪里添加它。

其他问题:

  1. 一旦将分配的NSBox添加为子视图,则需要通过释放分配的来避免内存泄漏,因为视图将保留它。
  2. 您可能不需要将dynamicSection作为类中的一员。
  3. 重复的代码太多了:

switch之前这样做

代码语言:javascript
复制
dynamicSection = [[NSBox alloc] init];
[dynamicSection setTitle:[self returnSectionTitle:index]];

并在switch之后添加视图

代码语言:javascript
复制
[someView addSubview:dynamicSection];
[dynamicSection release];
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11352303

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档