Using "foreach"

Discussion in 'iOS Programming' started by MickeyT, May 9, 2010.

  1. MickeyT macrumors member

    Joined:
    Apr 26, 2010
    Location:
    Newcastle, United Kingdom
    #1
    Hello all

    I have a view with several textfields on it that I want to test for nullstrings when a user presses a button.

    Currently I have several if statements that check this, but I was wondering how you would use the "for each" looping to cycle through them all instead and cut down on code.

    Does the view conform to a hierarchical structure so that, for example, the textfields are contained within the view? If this is the case then I perceived it to be something like:

    foreach(UITextView in UIView) {

    ............code here......

    }

    Is this correct - how would it actually be written?

    Many thanks for any help.

    Cheers.
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    UIView is not a container (directly) so you can't do that. You could use foreach to iterate of the the subviews property but you'd have to check each item to see if it was a UITextView. The compiler won't automatically do that for you.
     
  3. willh1234 macrumors newbie

    Joined:
    Nov 5, 2009
    #3
    Instead of doing that jst do something like this:

    Code:
    for (UItextField *textField in myTextFieldView)
    {
        //do whatever here
    }
    
    Although if your view contains anything other than text fields youll want to change UITextField *textField to UIView *view and then make sure you only do the actions for the text fields.
     
  4. MickeyT thread starter macrumors member

    Joined:
    Apr 26, 2010
    Location:
    Newcastle, United Kingdom
  5. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #5
    What is myTextFieldView?
     

Share This Page