点击可可中的NSRect打开NSWindow

时间:2009-05-31 14:58:36

标签: cocoa mouseevent

在我的程序中,我能够确定是否在某个NSRect中进行了鼠标点击。如何通过单击此NSRect打开新的NSWindow?

2 个答案:

答案 0 :(得分:2)

如果要显示现有窗口(使用Interface Builder创建),只需在窗口对象上调用makeKeyAndOrderFront
如果要以编程方式创建新窗口,可以找到答案here

答案 1 :(得分:0)

要处理事件,您需要在NSView或NSViewController子类中实现NSResponder的相关方法。例如,您可以实现mouseDown:-mouseUp:来处理鼠标点击(以相当简单的方式),如下所示:

- (void) mouseDown: (NSEvent *) event
{
    if ( [event type] != NSLeftMouseDown )
    {
        // not the left button, let other things handle it
        [super mouseDown: event];
        return;
    }

    NSPoint location = [self convertPoint: [event locationInWindow] fromView: nil];
    if ( !NSPointInRect(location, self.theRect) )
    {
        [super mouseDown: event];
        return;
    }

    self.hasMouseDown = YES;
}

- (void) mouseUp: (NSEvent *) event
{
    if ( (!self.hasMouseDown) || ([event type] != NSLeftMouseUp) )
    {
        [super mouseUp: event];
        return;
    }

    NSPoint location = [self convertPoint: [event locationInWindow] fromView: nil];
    if ( !NSPointInRect(location, self.theRect) )
    {
        [super mouseDown: event];
        return;
    }

    self.hasMouseDown = NO;

    // mouse went down and up within the target rect, so you can do stuff now
}