views:

297

answers:

3

I have a UITextView on a View that becomes the first responder.

When I embed the UITextView inside of a UIScrollView in Interface Builder the UITextView is no longer the first responder. I am not sure what has changed?

I would like the UITextView to become the first responder.

- (void)viewDidLoad {
    [super viewDidLoad];
    [scrollView setContentSize:CGSizeMake(540,620)];
    composeTextView.delegate = self;    
    [composeTextView becomeFirstResponder];
}
A: 

Hey,

I've done the same, added some code and it works OK:

scrollView.contentSize = CGSizeMake(540,620);
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.showsVerticalScrollIndicator = NO;
scrollView.scrollsToTop = NO;
[composeTextView becomeFirstResponder];
Dror Sabbag
I added your code, and still no luck. Could it be IB? I have a UIView that contains a UIScrollView. Inside of that is a UITextView and some UIButtons. All controls are connected in IB.
Sheehan Alam
Hey, I've done everything again, and from Scratch, i'll attach it as a new answer so i could mark the Code.
Dror Sabbag
+1  A: 

This is my .h file:

#import <UIKit/UIKit.h>

@interface Untitled1ViewController : UIViewController <UITextFieldDelegate> {
    UIScrollView *scrollView;
    UITextField *composeTextView;
}

@property (nonatomic,retain) IBOutlet UIScrollView *scrollView;
@property (nonatomic,retain) IBOutlet UITextField *composeTextView;

@end

this is my .m file:

#import "Untitled1ViewController.h"

@implementation Untitled1ViewController
@synthesize scrollView;
@synthesize composeTextView;
- (void)viewDidLoad {
    [super viewDidLoad];
    [scrollView setContentSize:CGSizeMake(540,620)];
    composeTextView.delegate = self;    
    [composeTextView becomeFirstResponder];


}

in IB, i've connected the following: textField to composeTextView scrollView to scrollView and the textField delegate to the file's owner.

Try again and advise.

Dror Sabbag
Still no luck for me. Is your composeTextView inside of your scrollView?
Sheehan Alam
Also I'm using a UITextView and not a UITextField
Sheehan Alam
Thanks! This code works great. I realized my UITextView was not hooked up in IB. Small mistake, causing huge headaches.
Sheehan Alam
A: 

The text view takes scroll events and hence the scroll events are not passed on further in its responder chain. If you are not interested in scrolling the text view, disable the scrolling in textView (use nib or scrollEnabled property), now, scrollView will start taking scroll events.

Raj
Even if I disable scrolling for the textview, it does not become the first responder
Sheehan Alam
It does, I have verified. I am quite sure about this. If you disable scrolling in the textView, then the container - scrollView will start taking the events.
Raj
I have modified my code to composeTextView.scrollEnabled = NO; and [scrollView becomesFirstResponder]; I have also tried the former code and [composeTextView becomesFirstResponder] with no luck.
Sheehan Alam
Ok I mistook your question, call -becomeFirstResponder on the textView should work. Probably you have missed the outlet connections. put a breakpoint in your -viewDidLoad method and see if the compositTextView / scrollView are nil.
Raj
My outlets were not setup for the textView in IB. Thanks!
Sheehan Alam
As soon as you name an ivar as IBOutlet in interface file, put an assert statement in either -awakeFromNib or in -viewDidLoad method like: NSAssert (nil!=outletInstance, @"outletInstance is nil") so that the application traps any nil conditions for outlets. This is a good way to identify if outlets are nil.
Raj