Domanda

I am fixing some broken code that an old coder did and he creates a button programmatically and puts it in a table (bad code if you ask me!) He creates a button like this:

- (UIButton *)recentButton {
    if (recentButton == nil) {
        recentButton = [[UIButton alloc] init];
    }

    UIImage *bgimage = [UIImage imageNamed:@"button_large.png"];
    [recentButton setBackgroundImage:bgimage forState:UIControlStateNormal];
    [self.view addSubview:recentButton];



    return recentButton;
}

The issue I have is within these lines which I added:

UIImage *bgimage = [UIImage imageNamed:@"button_large.png"];
[recentButton setBackgroundImage:bgimage forState:UIControlStateNormal];
[self.view addSubview:recentButton];

This code does not seem to add the background image to the button, the code does not fail to compile, it compiles fine just the background is not correct and is not being added to the button. I would consider myself a newbie at iOS programming so any help would be amazing! I was unable to find any help on stackoverflow which helped this to work Many thanks in advance, Anthony

È stato utile?

Soluzione 3

Found out that a tableview was holding all of the buttons and was overriding every bit of that code I had written, I changed the tableview code and now it works, thanks to everyone for their help :)

I changed the code to check what was being added, if a button was being added then to set the background image and set the text color to white, else to do what it did before. I will post the code later as I am typing from a PC right now and not the Mac.

Edit: Here is the code:

if ([cellControl isKindOfClass:[UIButton class]]) {
        static NSString *kCellButton = @"CellButton";
        cell = [tableView dequeueReusableCellWithIdentifier:kCellButton];

        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCellButton] autorelease];

            cell.backgroundView =[[UIImageView alloc] initWithImage:[[UIImage imageNamed:@"button_large"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0]];
            cell.textLabel.textColor = [[[ConfigManager sharedInstance].skin valueForKey:@"tableCellHeadingTextColourButton"] toUIColor];


            cell.textLabel.textAlignment = UITextAlignmentCenter;
            cell.textLabel.font = [UIFont boldSystemFontOfSize:19];
        }

        cell.textLabel.text = [cellData objectForKey:kCellTitleKey];

    }

Once I added this it worked fine, I added the background image within the cellview because I have may buttons so the code is much more efficient this way.

Altri suggerimenti

Try initializing your button like this:

recentButton = [UIButton buttonWithType:UIButtonTypeCustom];

check with your image image. because it is case sensitive. check your resource folder once again

UIButton *btnClear = [[UIButton alloc] init];
btnClear = [UIButton buttonWithType:UIButtonTypeCustom];

btnClear.frame = CGRectMake(115, 200, 90, 40);
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top