试图了解如何使用xib文件

时间:2012-01-26 06:43:11

标签: objective-c interface-builder

我有一个自定义视图,我在xib文件中使用。我加载视图并将其添加到窗口。它添加视图就好了,因为我可以在视图中看到标签的默认文本,但是当我尝试使用方法调用更改标签时,它不会更改文本。

自定义视图不是任何花哨的东西,只是绘制圆形,透明的背景。

NotificationView.h

#import <Cocoa/Cocoa.h>

@interface NotificationView : NSView

@property (weak) IBOutlet NSTextField *primaryLabel;
@property (weak) IBOutlet NSTextField *secondaryLabel;
@property (weak) IBOutlet NSTextField *identifierLabel;

@end

NotificationView.m

@implementation NotificationView
@synthesize primaryLabel;
@synthesize secondaryLabel;
@synthesize identifierLabel;


- (id) initWithFrame:(NSRect)frameRect
{
    self = [super initWithFrame:frameRect];
    if (self)
    {
        return self;
    }
    return nil;
}


- (void)drawRect:(NSRect)dirtyRect
{
    NSColor *bgColor = [NSColor colorWithCalibratedWhite:0.0 alpha:0.6];
    NSRect rect = NSMakeRect([self bounds].origin.x + 3, [self bounds].origin.y + 3, [self bounds].size.width - 6, [self bounds].size.height - 6);

    NSBezierPath *path = [NSBezierPath bezierPathWithRoundedRect:rect xRadius:5.0 yRadius:5.0];
    [path addClip];

    NSShadow *shadow = [[NSShadow alloc] init];
    [shadow setShadowColor:[NSColor redColor]];
    [shadow setShadowBlurRadius:2.0f];
    [shadow setShadowOffset:NSMakeSize(0.f, -1.f)];
    [shadow set];

    [bgColor set];
    NSRectFill(rect);

    [super drawRect:dirtyRect];
}

@end

在xib中,我将自定义视图设置为NotificationView类型。我在视图中添加了3个标签,并将它们连接到上面的IBOutlets。 (我按住Ctrl键并单击并从标签拖动到.h文件以建立连接。)

我正在加载视图并使用以下方法将其添加到窗口中。它通过一系列窗口进行查看,如果找到现有匹配则使用该窗口,否则会创建一个新窗口。

- (void) popupNotificationWithTag:(NSString *)tag fade:(double)msFade lineOne:(NSString *)lineOneText lineTwo:(NSString *)lineTwoText
{
    NotificationWindow *notificationWindow;
    NotificationWindow *tmpWindow;
    NSEnumerator *enumerator;

    // Walk the notification windows in the array
    enumerator = [self.notificationWindows objectEnumerator];
    if(enumerator)
    {
        while((tmpWindow = [enumerator nextObject]))
        {
            if([tmpWindow.tag isEqualToString:tag])
            {
                notificationWindow = tmpWindow;
            }
        }
    }

    // Make a new notification window
    if (!notificationWindow)
    {
        int width = [[NSScreen mainScreen] frame].size.width;
        int height = [[NSScreen mainScreen] frame].size.height;

        notificationWindow = [[NotificationWindow alloc] initWithRect:NSMakeRect(width - 420, height - 130, 400, 100)];
        NSNib *nib = [[NSNib alloc] initWithNibNamed:@"Notification" bundle: nil];
        NSArray *objects;
        [nib instantiateNibWithOwner:self topLevelObjects:&objects];

        for (id obj in objects) {
            if ([[obj class] isSubclassOfClass:[NSView class]])

                [notificationWindow setContentView:obj];
        }

        [notificationWindow setTag:tag];         
        [self.notificationWindows addObject:notificationWindow];
    }

    // Display window
    [notificationWindow makeKeyAndOrderFront:nil];
    [notificationWindow display];
    notificationWindow.fadeOut = msFade;
    [notificationWindow setPrimaryText:lineOneText];
    [notificationWindow setSecondaryText:lineTwoText];
    [notificationWindow setIdentifierText:tag];
}

窗口类是 NotificationWindow.h

#import <Foundation/Foundation.h>

@interface NotificationWindow : NSWindow

@property (nonatomic, strong) NSString *tag;
@property (nonatomic) double fadeOut;

- (id)initWithRect:(NSRect)contentRect;
- (void) setPrimaryText:(NSString *)text;
- (void) setSecondaryText:(NSString *)text;
- (void) setIdentifierText:(NSString *)text;

@end

NotificationWindow.m

#import "NotificationWindow.h"
#import "NotificationView.h"

//===========================================================================================================================
// Private call properties and methods
//===========================================================================================================================
@interface NotificationWindow()

@property (nonatomic,strong) NSTimer *timerFade;

- (void) timerFadeFired;

@end


//===========================================================================================================================
//===========================================================================================================================


@implementation NotificationWindow

//===========================================================================================================================
// Property Getters and Setters
//===========================================================================================================================
@synthesize tag = _tag;
@synthesize fadeOut = _fadeOut;

@synthesize timerFade = _timerFade;


//===========================================================================================================================
// Public methods
//===========================================================================================================================
- (id)initWithRect:(NSRect)contentRect
{
    if (self = [super initWithContentRect:contentRect 
                                styleMask:NSBorderlessWindowMask 
                                  backing:NSBackingStoreBuffered 
                                    defer:NO]) {
        [self setLevel: NSScreenSaverWindowLevel];
        [self setBackgroundColor: [NSColor clearColor]];
        [self setAlphaValue: 1.0];
        [self setOpaque: NO];
        [self setHasShadow: NO];
        [self setIgnoresMouseEvents: YES];
        [self setCollectionBehavior:NSWindowCollectionBehaviorCanJoinAllSpaces];
        [self orderFront: NSApp];
        self.fadeOut = -1;

        // Start our timer to deal with fadeing the window
        self.timerFade = [NSTimer scheduledTimerWithTimeInterval:0.001
                                                          target:self
                                                        selector:@selector(timerFadeFired)
                                                        userInfo:nil
                                                         repeats:YES];

        return self;
    }

    return nil;
}


- (BOOL) canBecomeKeyWindow
{
    return YES;
}

- (void) display
{
    [super display];
    [self setAlphaValue:1.0];
}

- (void) setPrimaryText:(NSString *)text
{
    NotificationView *view = self.contentView;
    view.primaryLabel.stringValue = text;
}


- (void) setSecondaryText:(NSString *)text
{
    NotificationView *view = self.contentView;
    view.secondaryLabel.stringValue = text;
}


- (void) setIdentifierText:(NSString *)text
{
    NotificationView *view = self.contentView;
    view.identifierLabel.stringValue = text;
}


//===========================================================================================================================
// Private methods
//===========================================================================================================================
- (void) timerFadeFired
{
    [self orderFront:NSApp];
    if (self.fadeOut > 0)
    {
        self.fadeOut--;
    }
    else if (self.fadeOut == 0)
    {
        if (self.alphaValue > 0)
            self.alphaValue -= 0.002;
        else
            self.fadeOut = -1;
    }
}


@end

所以我假设我将标签连接到IBOutlets时做错了,但我无法弄清楚是什么。我想我可以在代码中创建视图,但我试图做好并使用界面构建器。

我在XCode 4.2.1。

0 个答案:

没有答案