Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Yellowstone2012

macrumors regular
Original poster
Feb 3, 2011
108
0
I am trying to load a NSView as a subview in another NSView. Am I using the correct code?:

my.h file:
Code:
#import <Foundation/Foundation.h>

@interface HTMLCode : NSObject {
    
    //Main Window
    NSButton *linkGenerator;
    NSButton *imageGenerator;
    NSButton *videoGeneratorHTML5;
    NSView *mainView;
    
    //Link Generator
    NSView *hyperlinkGeneratorView;
}




//Main Window
@property (assign) IBOutlet NSButton *linkGenerator;
@property (assign) IBOutlet NSButton *imageGenerator;
@property (assign) IBOutlet NSButton *videoGeneratorHTML5;
@property (assign) IBOutlet NSView *mainView;

//Link Generator
@property (assign) IBOutlet NSView *hyperlinkGeneratorView;



- (IBAction)loadLinkView:(id)sender;

@end


my.m file:
Code:
#import "HTMLCode.h"

@implementation HTMLCode
@synthesize linkGenerator;
@synthesize imageGenerator;
@synthesize videoGeneratorHTML5;
@synthesize mainView;
@synthesize hyperlinkGeneratorView;

- (id)init
{
    self = [super init];
    if (self) {
        // Initialization code here.
    }
    
    return self;
}

[B]- (IBAction)loadLinkView:(id)sender {
    
    hyperlinkGeneratorView = [[NSView alloc] init];
    [mainView addSubview:hyperlinkGeneratorView];
    
}[/B]
@end

I'm wanting my "mainView" NSView to display the "hyperlinkGeneratorView" NSView.
 
I'm assuming you're posting this code because the subview isn't visible?

Where do you expect it to appear in your main view, and what size do you expect it to have? You haven't set the subview's frame. Try initialising the subview with its designated initialiser, initWithFrame:.
 
I'm assuming you're posting this code because the subview isn't visible?

Where do you expect it to appear in your main view, and what size do you expect it to have? You haven't set the subview's frame. Try initialising the subview with its designated initialiser, initWithFrame:.

I expect View2 to appear in View1. View2 is the same size as View1. When I click the button to display the subview, the subview isn't visible. (I expect it to be visible)

Is there a tutorial anywhere on this matter?

Thanks.
 
Code:
- (IBAction)loadLinkView:(id)sender

Okay, it looks like this method is adding the hyperlinkGeneratorView to the mainView. But is anything calling this method?

Code:
hyperlinkGeneratorView = [[NSView alloc] init];

You should use initWithFrame instead. Right now the view isn't positioned or sized. If you want it to be the same size as the parent view you can do:

Code:
hyperlinkGeneratorView = [[NSView alloc] initWithFrame: mainView.bounds ];

I don't know about tutorials, but here is a guide on the Cocoa view hierarchy. Should tell you how to add / remove views, how to size them, how frames and bounds relate, etc.

http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CocoaViewsGuide/WorkingWithAViewHierarchy/WorkingWithAViewHierarchy.html
 
Last edited:
Could you explain why all these views are IBOutlets? And why the properties are "assign" properties? Both are very strange decisions to make.

You should use IBOutlet for items that are created in a .nib or .xib file, which you don't do. Either create them in a .xib file, or don't make them IBOutlets.

If they are IBOutlets, then the properties should be "readonly". And if not, then the properties should not be "assign" but "retain".

BTW. Writing "self.hyperlinkGeneratorView" instead of "hyperlinkGeneratorView" costs nothing and makes it so much clearer when you read the code.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.