UITextField subview of UITableViewCell to become first responder?

后端 未结 4 908
挽巷
挽巷 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.

提交回复
热议问题