PDA

View Full Version : how to change button title using object id in IB?




Sergio10
Apr 26, 2009, 09:00 AM
hi,
Is it possible to change title using object id of the control?
Is any ways to do this?

Thanks



BlackWolf
Apr 26, 2009, 09:15 AM
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

Sergio10
Apr 26, 2009, 09:29 AM
so how to change title by tag?
Could please write a sample?

tahnks

kainjow
Apr 26, 2009, 10:01 AM
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.

Sergio10
Apr 26, 2009, 10:42 AM
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

BlackWolf
Apr 26, 2009, 11:12 AM
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

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.

PhoneyDeveloper
Apr 26, 2009, 03:28 PM
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];

Sergio10
Apr 26, 2009, 04:49 PM
Thanks for reply!!

What should I put instead of "self.view" in this code?:

UIButton* button = (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.

PhoneyDeveloper
Apr 26, 2009, 05:05 PM
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.

Sergio10
Apr 26, 2009, 07:11 PM
Ohh, I understood.

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

PhoneyDeveloper
Apr 26, 2009, 07:56 PM
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.

BlackWolf
Apr 27, 2009, 09:51 AM
Ohh, I understood.

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

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