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 8194729
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T04:55:19+00:00 2026-06-07T04:55:19+00:00

I have a TextViewController which is a subclass of NSViewController which controls a View

  • 0

I have a TextViewController which is a subclass of NSViewController which controls a View containing a NSTextView

I attach an instance of TextViewController to an existing view with the following code

TextViewController *textViewer;

…

[[self view] addSubview:[textViewer view]]; // embed new TextView in our host view
[[textViewer view] setFrame:[[self view] bounds]];  // resize the controller's view to the host size
textViewer.delegate = self;
[split.window makeFirstResponder:textViewer];

This works quite well, and the TextViewController traps the keyDown Event to perform various actions.

I wanted to allow users to select text in the NSTextView to copy to clipboard.
Unfortunately clicking in the NSTextView makes this the FirstResponder and stops TextViewController from responding to key presses.

I can force the FirstResponder back to my TextViewController, but this seems like a kludge.

- (void)textViewDidChangeSelection:(NSNotification *)aNotification {
    [self.view.window makeFirstResponder:self];
}

I know I could subclass NSTextView to trap the keyDown Event, but this doesn’t seem much better.

I am sure there must be a more elegant way of doing this.

I added a subclass of NSTextView which just passes the keyDown to the controller

@protocol MyTextViewDelegate
- (BOOL)keyPressedInTextView:(NSEvent *)theEvent;
@end

@interface MyTextView : NSTextView
@property (assign) IBOutlet NSObject <MyTextViewDelegate> *delegate;
@end

…

@implementation MyTextView
@synthesize delegate;
- (void)keyDown:(NSEvent *)theEvent {
    if([self.delegate respondsToSelector:@selector(keyPressedInTextView:)]) {
        if([self.delegate keyPressedInTextView:theEvent])
            return;
        }
    [super keyDown:theEvent];
}
@end
  • 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-06-07T04:55:20+00:00Added an answer on June 7, 2026 at 4:55 am

    Subclassing NSTextView sounds like the best solution for the problem you described. You want a text view that behaves like normal to select, copy, Cmd-A, etc. except that it also responds to special key presses you’ve defined. This is a standard use of a subclass. Trying to have the view controller handle things by playing games with the first responder will give you problems in various edge cases like the one you discovered.

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

Sidebar

Related Questions

I have the following code, which is causing a leak, despite ARC being enabled
Have deployed numerous report parts which reference the same view however one of them
Have a look at the following code to find X^y. /* Find exponent in
i have created a framework out of my existing code base and tried using
have written this little class, which generates a UUID every time an object of
Have a procedure which looks like Procedure TestProc(TVar1, TVar2 : variant); Begin TVar1 :=
I have a navigation based application that have a uinavigataioncontroller containing uitableviewcontrollers. I want
I have a number of view controllers that I want to navigate; however, I
I have this code for send images on tweet. When I try the app,
I have a question about presenting the TWTweetComposerViewController as a modal view in iOS

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.