I would like to know why I’m not getting IBOutlets connected in an App’s window

49 Views Asked by At

The results are pretty easy to replicate.

  1. Create a new Xcode project called TestWindow.
  2. XIB not storyboard.
  3. Objective-C
  4. Set the project’s scheme to turn off Metal API validation.
  5. Set Build Options to NO ARC under Objective-C options.
  6. Delete the Main Menu window.
  7. Create a new window object called GoWindow. And add a window to it.
  8. Set File’s Owner of the AppDelegate object in Placeholders to AppDelegate.
  9. Drag a Scrollable Text View from the Library to the Window
  10. Connect an IBOutlet NSWindow in AppDelegate to the window.
  11. Connect an IBOutlet NSScrollview in AppDelegate to the scrollview in the window.
  12. Connect an IBOutlet NSTextView in AppDelegate to the textView in the ScrollView in the window.
  13. In AppDelegate, create an IBAction method to makeWindow.
  14. Add a menu item called makeWindow to the AppDelegate.
  15. Wire the menu item to the IBAction in AppDelegate.
  16. Create a method in AppDelegate called makeWindowContinued.
  17. Add a breakpoint to an NSBeep(); inside the makeWindowContinued method.
  18. Add [self performSelector:@selector(makeGoWindowContinued) withObject:nil afterDelay:1]; to the AppDelegate method.
  19. Run the app.
  20. The breakpoint will trigger allowing you to see that the three outlets and not being set.
  21. Here is the resultant AppDelegate.h code:
#import <Cocoa/Cocoa.h>

@interface AppDelegate : NSObject <NSApplicationDelegate>
{
    IBOutlet NSWindow           *theDocumentWindow;
    IBOutlet NSScrollView       *theDocumentWindowScrollView;
    IBOutlet NSTextView         *theDocumentWindowTextView;

    NSWindowController          *myDocumentWindowController;
    
}

- (NSWindowController *)myDocumentWindowController ;
- (void)set_myDocumentWindowController:(NSWindowController *)t;
- (IBAction)makeWindow:(id)sender;
- (void)makeGoWindowContinued;

@end

And the resultant AppDelegate.m code:

#import "AppDelegate.h"

@interface AppDelegate ()

@property (strong) IBOutlet NSWindow *window;

@end

@implementation AppDelegate

- (NSWindowController *)myDocumentWindowController                                   {return myDocumentWindowController;}
- (void)set_myDocumentWindowController:(NSWindowController *)t                        {[t retain];    [myDocumentWindowController release];                    myDocumentWindowController = t;}

- (IBAction)makeWindow:(id)sender
{
    if (![self myDocumentWindowController]) {
        [self set_myDocumentWindowController:[[NSWindowController alloc] initWithWindowNibName:@"GoWindow"]];
    }
    [[[self myDocumentWindowController] window] setTitleWithRepresentedFilename:@"Untitled"];
    [[[self myDocumentWindowController] window] center];
    [[[self myDocumentWindowController] window] makeKeyAndOrderFront:self];
    [self performSelector:@selector(makeGoWindowContinued) withObject:nil afterDelay:1]; // wait for window to appear
}

- (void)makeGoWindowContinued
{
    NSBeep();
}

- (void)applicationWillTerminate:(NSNotification *)aNotification
{
    // Insert code here to tear down your application
}

- (BOOL)applicationSupportsSecureRestorableState:(NSApplication *)app
{
    return YES;
}

I'm not sure File's Owner is correct, maybe I need to set a delegate? Don't know.

0

There are 0 best solutions below