开发者

How do I call create a name of a button variable in a code, with a number in it?

I've got a int, that is changed in a previous method, that now has to be part of the name of a button variable. For example:

int numberFromLastOne;
numb开发者_运维问答erFromLastOne = 4;

I then want to get 'button4' to do something. Could I use something like this?

[[button@"%d", numberFromLastOne] doSomething:withSomethingElse];

I've never had to do something this before. Any ideas would be appreciated :)


I'm not exactly sure what you're trying to do, but if you have a series of buttons, and you're trying to retrieve one of them based on some integer value, you could use the tag property to do this.

Give the buttons numeric tags (in IB, or programmatically, however you're creating your UI). Then at runtime do something like:

int numberFromLastOne;
numberFromLastOne = 4;
UIButton* aButton = (UIButton*) [self.view viewWithTag:numberFromLastOne];

That assumes you're doing this from a view controller; you didn't say. You get the idea, look up the viewWithTag member.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜