【问题标题】:Replacing subviews with NSBox用 NSBox 替换子视图
【发布时间】:2012-07-05 20:45:40
【问题描述】:

我想使用 NSBox *dynamicSection 来用不同的视图替换框的内容,具体取决于从中选择的索引和 NSPopUpButton 控件。下面的方法接收 NSPopUPButton 作为对象,并使用大小写开关来动态设置框的视图和标题。

@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;
  }

}

}

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

谢谢!

【问题讨论】:

    标签: objective-c xcode4 nsbox


    【解决方案1】:

    您似乎没有将NSBox 添加为视图的子视图,我无法从应该添加的问题中看出它。

    其他问题:

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

    switch: 之前执行此操作:

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

    并在switch之后添加视图:

    [someView addSubview:dynamicSection];
    [dynamicSection release];
    

    【讨论】:

    • 感谢您的建议,我已经在switch语句之前和之后进行了更改。我需要在 NSBox 中创建一个自定义视图作为子视图吗?
    • 不,您需要将NSBox添加到视图。
    • 你能提供一个例子吗?我目前将 NSBox 定位在与 AppDelegate 关联的 Nib 的主视图中。
    • 你有 Aaron Hillegass 的Cocoa Programming for Mac OS X - 4ed 吗?第 31 章介绍了一个使用 NSBox 的视图交换示例。
    • 好的,谢谢。我刚刚找到了一个实例化视图控制器子类的单独示例: Sect1 = [[Sect1_View alloc] initWithNibName:@"Sect1_View" bundle:nil]; [dynamicSection setContentView:Sect1.view]; [动态部分发布];
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多