Is this code wrong?

Discussion in 'Mac Programming' started by Yellowstone2012, Jul 23, 2011.

  1. Yellowstone2012 macrumors regular

    Joined:
    Feb 3, 2011
    #1
    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.
     
  2. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #2
    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:.
     
  3. Yellowstone2012 thread starter macrumors regular

    Joined:
    Feb 3, 2011
    #3
    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.
     
  4. holmesf, Jul 26, 2011
    Last edited: Jul 26, 2011

    holmesf macrumors 6502a

    Joined:
    Sep 30, 2001
    #4
    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
     
  5. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #5
    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.
     
  6. admanimal macrumors 68040

    Joined:
    Apr 22, 2005
    #6
    This is only true on iOS. On OS X, using assign for IBOutlet properties is normal.
     
  7. Yellowstone2012 thread starter macrumors regular

    Joined:
    Feb 3, 2011
    #7
    Thanks for the help.. Will be looking into the docs.
     

Share This Page