0
votes

I have two NSTextViews. One that is on self.view and another one that is on a subview (i.e., FlippedView) of a scrollView that is part of self.view.

The NSTextView that is inside self.view (i.e., textViewInSelf) responds properly to mouse clicks and I can type and select text. The one on the FlippedView (i.e., textViewInFlippedView) does not respond to mouse. The cursor changes shape to an (I) ... meaning that is ready to type text, but nothing happens. (sample code below)

In another section of the program, I can type text fine, but when I want to drag and select part of the type text, nothing happens (no sample code for this).

Any ideas? Thanks in Advance.

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
It appears that the problem is in the statement [scrollView setDocumentView:flippedView]; in applicationDidFinishLaunching in AppDelegate.m. If I remove the statement, everything works fine ... but the many other things do not work. Any ideas? - Z. Zepos
Also, if I add [mainWindow setContentView:scrollView] the NSTextViews work fine but clicking on buttons and other elements inside the scrollView do no work (similar behavior if I delete the [scrollView setDocumentView:flippedView]; from AppDelegate.m). - Z. Zepos

1 Answers

0
votes

I had to set the content of the main window with view that contained all other views and I had to put the setup of the windows on awakeFromNib although, I am not sure whether this made any difference.

- (void)awakeFromNib {

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