how to change button title using object id in IB?

Discussion in 'iPhone/iPad Programming' started by Sergio10, Apr 26, 2009.

  1. macrumors regular

    #1
    hi,
    Is it possible to change title using object id of the control?
    Is any ways to do this?

    Thanks
     
  2. macrumors regular

    #2
    afaik not.
    like anything else when you want to connect IB with xcode, you have to make an IBOutlet and you can then access your UI-Element in your code.

    btw: you COULD use tags btw, but I think you cannot use the id
     
  3. macrumors regular

    #3
    so how to change title by tag?
    Could please write a sample?

    tahnks
     
  4. Moderator emeritus

    kainjow

    #4
    You would have to loop through all the subviews in the button's superview (possibly recursively) and find a control that is a button and has the tag you're looking for. Unless you have some weird setup of your UI that requires this, don't. Use an IBOutlet.
     
  5. macrumors regular

    #5
    could you please write code. i have 7 buttons with tags: 1, 2, 3 ...

    how to change title of the button with tag 3 ?

    please help me.
    thanks
     
  6. macrumors regular

    #6
    like he said: you loop through the subviews array of the UIView where all those buttons lie on. then you simply check the tag-property of each subview if it is "3" ... if it is, then that is your button and you can do with it whatever you want.

    I don't think anyone here will give you the code so you can just copy&paste it, because you should be able to do these things on your on. look at for-loops in objective-c or how to go through arrays in general.
     
  7. macrumors 68030

    PhoneyDeveloper

    #7
    The Object ID in IB is only part of IB, it isn't revealed in code.

    You set the tag in IB and use it to access the view in code. In your View controller you do something like this

    Code:
    UIButton* button = (UIButton *)[self.view viewWithTag:myTag];
    [button setTitle:myTitle forState:UIControlStateNormal];
    
     
  8. macrumors regular

    #8
    Thanks for reply!!

    What should I put instead of "self.view" in this code?:
    PHP:
    UIButtonbutton = (UIButton *)[self.view viewWithTag:myTag];
    [
    button setTitle:myTitle forState:UIControlStateNormal];
    As I understood pointer to my view where should to find? Is it correct? Please help with this issue.
     
  9. macrumors 68030

    PhoneyDeveloper

    #9
    First, look at the documentation for viewWithTag:, that may clear things up for you.

    But, as I said, if this code runs in your view controller then self.view is correct. It just needs to be a view that is at any higher level in the view hierarchy than the view you're looking for, a container view. Normally self.view is the container view for all other views for a view controller.
     
  10. macrumors regular

    #10
    Ohh, I understood.

    But I don't use any View. I placed my controls (UIButton, UILabel) in UIWindow. How to solve this problem?
     
  11. macrumors 68030

    PhoneyDeveloper

    #11
    UIWindow is a UIView. Use your window.

    In most cases one would have a view that gets added to the window and that would be the main container. This would make switching between view easier. But if you didn't do it that way then just use the window.
     
  12. macrumors regular

    #12
    you shouldn't do that anyway, use a UIViewController ... and ysou should learn objective-c basics btw!
     

Share This Page