Help with UITextField Delegate

Discussion in 'iOS Programming' started by Colonial94, Sep 28, 2008.

  1. Colonial94 macrumors newbie

    Sep 28, 2008
    I am trying to develop an app which allows the user to add records to a UITableView. I have most everything set up, but I have run into a problem when trying to implement the UITextFieldDelegate which I am trying to use for the data entry. Here's the full setup, any help or pointers would be appreciated as I've been wrestling with this for a few hours:

    When the user clicks the add button it loads a new UITableView (grouped) to be used for data entry. The cells in this UITableView are custom cells which I developed in Interface Builder - each cell includes a label & a UITextField. After a good amount of research I figured out how to get the custom cells to load correctly.

    The Label & TextField are linked up to a customCellClass in XCode which allows me to customize the loaded data at runtime. Up to this point, everything seems to be working. The problem is with the TextFieldDelegate setup:

    I want to be able to adjust the TableView to show the appropriate cells when the keyboard is activated, so I am trying to setup the TableViewController as the delegate for the TextFieldDelegate. In order to do this, I set up the TableViewController as the FileOwner for the customCell in IB and linked the TextFieldDelegate up to the FileOwner. In Xcode I updated the TableViewController declaration to include the <UITextFieldDelegate>, but the delegate methods still aren't being called.

    Trying another approach, when I switch the delegate to be the customCellClass, using the same approach in IB & xcode, the delegate methods do get called, but then I'm having a tough time figuring out how to access the TableView to adjust it accordingly.

    I'm probably overlooking something basic here, but have been wrestling with it for a few hours and would love any pointers if anyone can help.
  2. PhoneyDeveloper macrumors 68040


    Sep 2, 2008
    I'm impressed. Probably no one else here has built TableView cells in IB.

    In principle setting up the ViewController as the textviewdelegate should work. You definitely want the delegate to be a controller, not a view.

    If it's set up as above, after the nib loads what is the value of the textview's delegate property? If it's not set or it's set to something else than the viewcontroller then something is wrong in IB. If it is correctly set to the viewcontroller but the delegate methods aren't being called then either your delegate methods aren't correct or there is some fundamental issue with this setup that won't work.

    Another option is to have another viewcontroller in the nib. This viewcontroller would be the delegate and you would have an outlet for this controller in your main ViewController.
  3. Colonial94 thread starter macrumors newbie

    Sep 28, 2008
    got it working - sort of

    PhoneyDev - thanks for the reply. For some reason the delegate is not getting set correctly when I attempt to use the view controller as the delegate. It instead gets set to a temporary view controller that I am using to load the cell into the main TableView. After some testing, i've validated that this is only happening when I try to use the TableView Controller as the delegate and not when I try to use the TableViewCell itself, so I don't think the problem is in interface builder - it must be in how I am loading the cells.

    In any case, your suggestion pointed me to the problem, so thanks. For now, I am programatically setting the delegate once the nib has loaded & it seems to be working. I will probably end up setting up a separate view controller as it seems to be a bit cleaner to me, but at least it's working now.

    Thanks again.
  4. cemorris macrumors regular

    Oct 13, 2004
    Do it in code

    I would assume that you are creating your custom view cells in code. In that case you need to set the delegate for each cell you create to "self". I am guessing that interface builder is not smart enough to set the delegate seperatly for each new cell you create. I believe interface builder will only create one instance for you at runtime. The problem is you need many of these cells and creating them in code is probably not setting the delegate correctly. I have some custum view cells, but I am not using interface builder to do them as I wanted to re-use them across multiple view controllers.

    Hope this helps
  5. Colonial94 thread starter macrumors newbie

    Sep 28, 2008
    I actually did create the cells in interface builder. You may be correct, that interface builder isn't smart enough to set each of the delegates correctly - i'm still not sure exactly why that part isn't working, but when I override the delegate (still using the xib file to load the actual cell), it is working correctly. I'm able to resuse these cells multiple times, I just need to manually set the delegate in each case - at least due to my set up.
  6. PhoneyDeveloper macrumors 68040


    Sep 2, 2008
    Not smart enough? IB has been setting delegates correctly for a very long time. The setup described by Colonial is a bit different from the way most people are doing this but there's nothing about it that shouldn't work.
  7. PhoneyDeveloper macrumors 68040


    Sep 2, 2008
    OK, I built a little test app that loads table view cells from a nib and places them into a tableview. The cell has a textfield in it and it sets the viewcontroller as its delegate.

    Colonial, what's the difference between what I did and what you're doing?

    Attached Files:

Share This Page