开发者

How to access a UIButton using a tag and change its image?

I need to change the images on a matrix of UIButtons, and the only thing I know of to address the buttons, is the tag. But I can not find a way to actually us this identifier. The buttons are created programmatically during viewDidLoad.

Here is the code for creating the buttons:

#define N_ROWS  4
#define N_COLS  3

    int N_IMG = 0;
    for (int a = 0; a < N_COLS; a++) {
        for (int j = 0; j < N_ROWS; j++) {


            UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
            aButton.frame = CGRectMake(a * 65.0 + 25, j * 65.0 + 15, 10.0, 10.0);
            aButton.tag = j + a * N_ROWS + 1;
            [aButton setBackgroundColor:[UIColor redColor]];

            N_IMG = N_IMG++;
            [self.v开发者_JAVA百科iew addSubview:aButton]; 
            number_sorted =  1;

        }
    }

Here is the code for setting the image:

- (IBAction)set_image:(id)sender {

    #define N_ROWS  4
    #define N_COLS  3

        int N_IMG = 0;
        for (int a = 0; a < N_COLS; a++) {
            for (int j = 0; j < N_ROWS; j++) {
                uibutton aButton.tag == (j + a * N_ROWS + 1) 
                setImage:[UIImage imageNamed:[puzzles objectAtIndex:N_IMG]]
                            forState:UIControlStateNormal];
            N_IMG = N_IMG++;

            }
        }
    }

This is the code where the truble starts: uibutton aButton.tag == (j + a * N_ROWS + 1)

Who can I set this up to work?


Short Answer:

RE: "How to access a UIButton using a tag..."

UIButton *tmpButton = (UIButton *)[self.view viewWithTag:tmpTag];

RE: "...and change its image?"

[tmpButton setImage:[UIImage imageNamed:@"MyGreatImage.png"] forState:UIControlStateNormal];

.

Long Answer:

RE: *"This is the code where the truble starts: uibutton aButton.tag == (j + a * N_ROWS + 1)"*

Yes, I can see the trouble. The line you are using is for setting a button's tag, not for getting the button from the tag.

To get a button from a known tag do this:

// define the tag index
int tmpTag = 123;//replace "123" with your own logic, i.e. (j + a * N_ROWS + 1)

// get the button with the given tag
UIButton *tmpButton = (UIButton *)[self.view viewWithTag:tmpTag];

// assign the image
tmpImage = [UIImage imageNamed:@"MyGreatImage.png"];
[tmpButton setImage:tmpImage forState:UIControlStateNormal];

BONUS CODE: At this stage you can also add or remove actions associated with your button.

//Remove all actions associated the button
[aButton removeTarget:nil action:NULL forControlEvents:UIControlEventAllEvents];

//Assign a new button action: using the exact selector name ("myMethodName")
[aButton addTarget:self action:@selector(myMethodName:) forControlEvents:UIControlEventTouchUpInside];

//Assign a new button action: using a calculated selector name 
//(suppose I have a bunch of methods with the prefix "myNumberedMethodName_" followed by an index.
int tmpSomeNumber = 12;
SEL tmpSelector = NSSelectorFromString ([NSString stringWithFormat:@"myNumberedMethodName_%i:",tmpSomeNumber);
// don't forget to include the ":" symbol in the selector name
[aButton addTarget:self action:tmpSelector forControlEvents:UIControlEventTouchUpInside];

NOTE: "viewWithTag" normally returns a View object. A Button is a special type of View with special properties such as image. So to have it return a Button object instead of the more generic View object, we initialize it as a Button by casting it using (UIButton *) in the definition.

But If all you want is to change the button's opacity, then you don't have to cast it as a button. You can simply initialize it as a generic View:

// set opacity of a button
float tmpOpacity = 0.5;//half-visible
UIView *tmpView = [self.view viewWithTag:tmpTag];//get the view object associated with button's tag (remember, a button IS a view)
[[tmpView layer] setOpacity:tmpOpacity];

See also Button with Tag.


I don't really understand what you are trying to do, but why can't you store your UIButton objects (ie in an NSArray object), so you can access them later (in your second loop)?

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜