how to change button title using object id in IB?

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

  1. macrumors regular

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

  2. macrumors regular

    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

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

  4. Moderator emeritus


    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

    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.
  6. macrumors regular

    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


    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

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

    Thanks for reply!!

    What should I put instead of "self.view" in this code?:
    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


    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

    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


    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

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

Share This Page