0

I want to create a text message page and want to see the keyboard on page load. This is what I have which doesn't work. Can you find what I'm doing wrong?

In my header file:

@interface taskViewController : ViewController
@property (nonatomic, retain) IBOutlet UITextField *taskDescription;

    - (void) viewWillAppear:(BOOL)animated;

@end

In my .m file:

#import "taskViewController.h"

@interface taskViewController ()
@end

@implementation taskViewController
    @synthesize taskDescription = _taskDescription;

    - (void) viewWillAppear:(BOOL)animated {
        [super viewWillAppear:animated];
        [_taskDescription becomeFirstResponder];
 }
@end
Adam Boostani
  • 5,999
  • 9
  • 38
  • 44
  • Not sure if this is your problem, but it might not be helping 1. You shouldn't define `viewWillAppear` in you `.h` 2. Don't call `super` 3. Change `_taskDescription` to `self.taskDescription` and move it to viewDidAppear – Flexicoder Mar 23 '14 at 10:52
  • How are you creating the layout for the ViewController? Is taskDescription defined in IB or Storyboard and then hooked up to the IBOutLet correctly? – Flexicoder Mar 23 '14 at 11:03

2 Answers2

0

Try adding

[self.view addSubview:_taskDescription];

before calling UIResponder. As Apple says in the UIResponder reference, object must be added to the view hierarchy before calling UIResponder.

So your -viewWillAppear: method will be:

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    [self.view addSubview:_taskDescription];
    [_taskDescription becomeFirstResponder];
}

More info at the UIResponder Reference

Alvaro Franco
  • 656
  • 1
  • 6
  • 17
  • He is doing this through a .xib file and so manually adding the subview is not at all recommended. The subview will be added at some point, that's why I recommend waiting until it is added through loading the .xib fully. – Infinity James Mar 23 '14 at 11:28
-1

In viewWillAppear: the tackDescription had obviously not yet fully loaded and may be nil.

My recommendation would be to move the call of [self.taskDescription becomeFirstResponder] to viewDidLoad.

In viewDidLoad you can know for sure that the entire view has loaded and everything had been added appropriately.

Infinity James
  • 4,667
  • 5
  • 23
  • 36