Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3661432
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T01:18:17+00:00 2026-05-19T01:18:17+00:00

Ok, basically I have a view with a series of slightly overlapping subviews. When

  • 0

Ok, basically I have a view with a series of slightly overlapping subviews. When a subview is clicked, it moves to the top (among other things), by a really simple two-liner:

-(void)mouseDown:(NSEvent *)theEvent {
  if (_selected) { // Don't do anything this subview is already in the foreground
    return;
  }
  NSView *superview = [self superview];
  [self removeFromSuperview];
  [superview addSubview:self positioned:NSWindowAbove relativeTo:nil];
}

This works fine and seems to be the “normal” way to do this.

The problem is that I’m now introducing some drag logic to the view, so I need to respond to -mouseDragged:. Unfortunately, since the view is removed from the view hierarchy and re-added in the process, I can’t have the view come to the foreground and be dragged in the same mouse action. It receives mouseDown:, but never receives mouseDragged: or any subsequent events. It only drags if I leave go of the mouse and click on the view again, since the second click doesn’t do any view hierarchy juggling.

Is there anything I can do that will allow the view to move to the foreground like this, AND continue to receive the subsequent -mouseDragged: and -mouseUp: events that follow?

I started along the lines of thinking of overriding -hitTest: in the superview and intercepting the mouseDown event in order to bring the view to the foreground before it actually receives the event. The problem here is, from within -hitTest:, how do I distinguish what type of event I’m actually performing the hit test for? I wouldn’t want to move the subview to the foreground for other mouse events, such as mouseMoved etc.

UPDATE | I’ve actually done this, in my superview, but it feels like a really bad code smell triggering an event from -hitTest: directly.

-(NSView *)hitTest:(NSPoint)aPoint {
    NSView *view = [super hitTest:aPoint];
    if ([view isKindOfClass:[EDTabView class]] && ![(EDTabView *)view isActive]) {
        // Effectively simulate two mouseDown events in sequence for this special case
        [view mouseDown:nil]; // Works because I know too much about the implementation
    }
    return view;
}

There are two things that feel wrong with this. The biggest is that I’m performing the action before -hitTest: has even returned, then performing as handled by AppKit after -hitTest: has completed. The second is that I don’t have an event to pass, so I’m passing nil. It works in this case since I know the event handler doesn’t actually process any of the event data, but it’s not great. The alternative was to move the entire logic into the superview, but the separation of concerns here feels even worse.

UPDATE | This -hitTest: hack is broken… it gets triggered when I’m not making a mouse click, such as when I’m dragging something over the top of the view. Still looking for a good solution to this problem.

An answer can either literally indicate how a view can leave and re-enter its superview during a drag operation, or (perhaps more elegant), an alternative way to move a view to the foreground other than [view removeFromSuperview]; [superview addSubview:view ...];.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T01:18:18+00:00Added an answer on May 19, 2026 at 1:18 am

    Ok, I went out on a whim and tried something illogical.

    Instead of this to move a view to the foreground:

    [theView removeFromSuperview];
    [theSuperview addSubview:theView positioned:NSWindowAbove relativeTo:nil];
    

    I simply dropped the removeFromSuperview line, wondering what AppKit actually does if you try to add a view to a superview twice (I assumed it provides protection against it happening).

    It works, at least under Snow Leopard, and it solves a lot of problems. The view being already present in the superview isn’t added, as the method name suggests, but it does get its position set without having to first remove it from the superview.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I basically have two subviews within my main view. I created each subview
I have a view where the user can change some settings, its basically a
I have a partial view that I want to basically take care of itself
Ok so basically I have a UITabBarController as my root view controller. I have
How can I achieve this? Basically I have first a view controller that lists
I have a view that basically just returns all records from a table, and
Basically I have a set of checkboxes that are dynamically created from view data
Since I basically have a model per view. I arrange my models in sub
I basically have 3 layers ( Window > Scene > View ) that each
Basically i have a view with just a table view on it and on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.