NSTextView不响应鼠标选择

时间:2016-08-19 22:28:03

标签: cocoa nstextview

我有两个NSTextViews。一个在self.view上,另一个在scrollView的子视图(即FlippedView)上,是自我视图的一部分。

self.view中的NSTextView(即textViewInSelf)可以正确响应鼠标点击,我可以输入并选择文本。 FlippedView上的那个(即textViewInFlippedView)不响应鼠标。光标将形状更改为(I)...意味着已准备好键入文本,但没有任何反应。 (以下示例代码)

在程序的另一部分中,我可以输入文本,但是当我想拖动并选择部分类型文本时,没有任何反应(没有示例代码)。

有什么想法吗?在此先感谢。

AppDelegate.h

#import <Cocoa/Cocoa.h>
#import "myViewController.h"

@interface AppDelegate : NSObject <NSApplicationDelegate> {
    FlippedView *flippedView;
}
@property (assign) IBOutlet NSWindow *window;
@property (strong) IBOutlet NSViewController *myViewController;
@property (weak) IBOutlet NSView *myCustomView;
@property(readwrite, strong, nonatomic) IBOutlet NSScrollView* scrollView;
@property(readwrite, strong, nonatomic) IBOutlet FlippedView *objCodeView;

@end

AppDelegate.m

#import "AppDelegate.h"

@interface AppDelegate ()
@end

@implementation AppDelegate
@synthesize scrollView;
@synthesize flippedView;

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    // Insert code here to initialize your application
    _myViewController = [[CodeObjViewController alloc] initWithNibName:@"myViewController" bundle:nil];
    [_myCustomView addSubview:[_myViewController view]];
    [[_myViewController view] setFrame:[_myCustomView bounds]];
    [scrollView setDocumentView:objCodeView];
}
-(BOOL) isFlipped {
    return YES;
}
@end

myViewController.h

    @interface FlippedView : NSView <NSTextViewDelegate, NSTextDelegate>
    @end


    @interface CodeObjViewController : NSViewController <NSTextViewDelegate, NSTextDelegate> {
        NSWindow *mainWindow;
        FlippedView *flippedView;
        NSScrollView *scrollView;

    }
    @end

myViewController.m

#import "myViewController.h"
#import "AppDelegate.h"

@implementation FlippedView
- (id)initWithFrame:(NSRect)frame {

    self = [super initWithFrame:frame];
    return self;
}
- (BOOL)isFlipped {
    return YES;
}
@end

@interface myViewController ()

@end

@implementation myViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    // Connect with the AppDelegate to get reference to the views there
    mainWindow = [[[NSApplication sharedApplication] windows] objectAtIndex:0];
    AppDelegate *theAppDelegate = (AppDelegate*) [NSApplication sharedApplication].delegate;
    flippedView = theAppDelegate.flippedView;
    scrollView = theAppDelegate.scrollView;
    NSRect rect = scrollView.frame;
    rect.size.height = flippedView.frame.size.height;
    flippedView.frame = rect;

    [self.view addSubview:flippedView positioned:NSWindowAbove relativeTo:nil];

    // THIS IS WORKING
    NSTextView *textViewInSelf = [self createTextView:100 :500 :100 :30 :[NSColor greenColor] :[NSFont systemFontOfSize:12] :[NSColor redColor] :NSCenterTextAlignment: self.view];

    // THIS IS NOT WORKING
    NSTextView *textViewInFLippedView = [self createTextView:100 :30 :100 :30 :[NSColor blueColor] :[NSFont systemFontOfSize:12] :[NSColor redColor]  :NSCenterTextAlignment: flippedView];

    [[self.view window] makeFirstResponder: textViewInFLippedView];

}
//--------------------------------------------------------------------------------
-(BOOL) isFlipped {
    return YES;
}
//-------------------------------------------
-(NSTextView *) createTextView : (CGFloat) x : (CGFloat) y : (CGFloat) w : (CGFloat) h : (NSColor *) bgColor : (NSFont *) font : (NSColor *) fontColor : (int) alignment : (NSView *) parentView {

    NSTextView *tf = [[NSTextView alloc] initWithFrame:CGRectMake(x, y, w, h)];
    tf.delegate = self;
    tf.automaticSpellingCorrectionEnabled = NO;
    tf.backgroundColor = bgColor;
    tf.editable = YES;
    tf.selectable = YES;
    tf.alignment = alignment;
    tf.font = font;
    tf.textColor = fontColor;
    if(parentView)
        [parentView addSubview:tf];
    return tf;
}  
@end

1 个答案:

答案 0 :(得分:0)

我必须使用包含所有其他视图的视图设置主窗口的内容,我必须将窗口的设置放在awakeFromNib上,但我不确定这是否有所不同。

- (void)awakeFromNib {

    NSView *windowView = [[NSView alloc]initWithFrame:_window.frame];
    [windowView addSubview:controlView];
    [windowView addSubview:scrollView];
    [_window setContentView:windowView];
}