Unable to enter text in programmatically created UITextField

I've created what seems to be the simplest possible programmatically-created iOS UI - just a view with a single UITextField placed in it. However, when I try to type in the text field, nothing happens - what am I missing in order to make this UI functional?

My application consists of only an AppDelegate and a ViewController. My main function calls into UIApplicationMain using my AppDelegate, and I have only overridden two functions: AppDelegate's didFinishLaunchingWithOptions, and ViewController's viewDidLoad function.

My AppDelegate just creates a single window, sets the root view controller, and makes it the key window:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.rootViewController = [[ViewController alloc] init];

    [self.window makeKeyAndVisible];

    return YES;
}

And my ViewController just creates a text field and adds it to the view:

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Creates a text field somewhere near-ish to the center of the screen
    UITextField* text = [[UITextField alloc] initWithFrame:CGRectMake(50, 100, 200, 50)];

    text.borderStyle = UITextBorderStyleRoundedRect;
    text.returnKeyType = UIReturnKeyDone;

    // Set the view background as white just to make sure we're in the right place...
    self.view.backgroundColour = [UIColor whiteColor];

    [self.view.addSubview:text];
}

Some things which I have tried without any effect:

  • Overriding loadView from ViewController as well as viewDidLoad and creating the view manually in that function
  • Bringing the text field to the front with [self.view bringSubviewToFront:text]
  • Setting all the enabled properties I can find - [text setUserInteractionEnabled true], [self.view setUserInteractionEnabled true], text.enabled = true

Also possibly related - if I force the keyboard to become active with [text becomeFirstResponder]; immediately after adding the text field, the keyboard does show up, but no keypresses have any effect and an assert shows up in the debugger log when typing: [Assert] View <(null):0x0> does not conform to UITextInput protocol. If default text is set in the text field, I'm able to tap it to bring up the keyboard, but again run into this assert and an inactive keyboard.

What am I missing in my attempt to create a simple text field programmatically in an iOS app? And, for bonus points, is there any good way to debug the assertion that fires?

Replies

I created a plain vanilla objC project.

From what you show I had to change:

  • remove
[self.window makeKeyAndVisible];
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
//    [self.window makeKeyAndVisible];  // << removed
    return YES;
}
  • correct the addSubview statement by removing dot before addSubview
    [self.view addSubview:text];
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    // Creates a text field somewhere near-ish to the center of the screen
     UITextField* text = [[UITextField alloc] initWithFrame:CGRectMake(50, 100, 200, 50)];

     text.borderStyle = UITextBorderStyleRoundedRect;
     text.returnKeyType = UIReturnKeyDone;

     // Set the view background as white just to make sure we're in the right place...
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:text];
    
}

It works OK.

The same works OK in Swift. In a plain VC.

You could try to:

  • option-clean build folder (one never knows!)

-set the delegate for the textField (but that should not be needed)

  • toggle keyboard from hardware / Software, just to see (cmd-K and cmd-shift-K)
  • remove those 2 lines to simplify even more:
    text.borderStyle = UITextBorderStyleRoundedRect;
    text.returnKeyType = UIReturnKeyDone;