UITextField subview of UITableViewCell to become first responder?

后端 未结 4 898
挽巷
挽巷 2020-12-13 20:44

I have a core data application which uses a navigation controller to drill down to a detail view and then if you edit one of the rows of data in the detail view you get take

相关标签:
4条回答
  • 2020-12-13 20:56

    After speaking with the Apple Dev Support Team, I have an answer!

    What you need to do is to create an offscreen UITextField in -(void)loadView; and then set it as first responder then on the viewDidLoad method you can set the UITextField in the UITableViewCell to be first responder. Heres some example code (remember I'm doing this in a UITableViewController so I am creating the tableview as well!

    - (void)loadView
    {
        [super loadView];
    
        //Set the view up.
        UIView *theView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        self.view = theView;
        [theView release];
    
        //Create an negatively sized or offscreen textfield
        UITextField *hiddenField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, -10, -10)];
        hiddenTextField = hiddenField;
        [self.view addSubview:hiddenTextField];
        [hiddenField release];
    
        //Create the tableview
        UITableView *theTableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] bounds] style:UITableViewStyleGrouped];
        theTableView.delegate = self;
        theTableView.dataSource = self;
        [self.view addSubview:theTableView];
        [theTableView release];
    
        //Set the hiddenTextField to become first responder
        [hiddenTextField becomeFirstResponder];
    
        //Background for a grouped tableview
        self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    }
    
    - (void)viewDidAppear:(BOOL)animated {
        [super viewDidAppear:animated];
        //Now the the UITableViewCells UITextField has loaded you can set that as first responder
        [theTextField becomeFirstResponder];
    }
    

    I hope this helps anyone stuck in the same position as me!

    If anyone else can see a better way to do this please say.

    0 讨论(0)
  • 2020-12-13 21:02

    Try do it in viewDidAppear method, works for me.

    0 讨论(0)
  • 2020-12-13 21:13

    have you tried using the uinavigationcontroller delegate methods?:

    navigationController:willShowViewController:animated:

    0 讨论(0)
  • 2020-12-13 21:14

    I think the obvious solution is to create the textfield in the init method of the view controller. That is usually where you configure the view because a view controller does require a populated view property.

    Then you can set the textfield as first responder in viewWillAppear and the keyboard should be visible as the view slides in.

    0 讨论(0)
提交回复
热议问题