繁体   English   中英

Objective-C setImage forState:UIControlStateNormal无法正确格式化图像

[英]Objective-C setImage forState:UIControlStateNormal not formatting image properly

我是Objective-c的新手,请耐心等待。 我正在2013年冬季的斯坦福大学“ iPhone / iPad编程”课程中编写一些代码作为练习。基本上,我需要做的是将图像设置为按钮的UIControlStateNormal按钮。 不幸的是,我还不能发布图片以使堆栈溢出,但是当我尝试将按钮的图像设置为UIControlState正常时,按钮的图像到处都是并且比实际按钮大。 对于其他UIControlState选项,带有图像的按钮的行为符合预期。 共有16个按钮。

我不确定自己在做什么错。 我在名为“ updateUI”的方法中在应用程序的ViewController中的for循环中设置了按钮图像。 这是代码:

#import "CardGameViewController.h"
#import "PlayingCardDeck.h"
#import "CardMatchingGame.h"

@interface CardGameViewController ()
@property (weak, nonatomic) IBOutlet UILabel *scoreLabel;
@property (strong, nonatomic) IBOutletCollection(UIButton) NSArray *cardButtons;
@property (weak, nonatomic) IBOutlet UILabel *flipsLabel;
@property (nonatomic) int flipCount;
@property (strong, nonatomic)CardMatchingGame* game;
@property (weak, nonatomic) IBOutlet UILabel *lastFlipLabel;
@property (weak, nonatomic) NSArray *lastFlip;
@property (weak, nonatomic) IBOutlet UISegmentedControl *gameControl;
@end

@implementation CardGameViewController
//lazy instantiation of the game
-(CardMatchingGame *)game
{
    if(!_game)
    {
        _game = [[CardMatchingGame alloc] initWithCardCount:self.cardButtons.count usingDeck:[[PlayingCardDeck alloc]init]];
    }
    return _game;
}
//set the properties of the card buttons
-(void)setCardButtons:(NSArray *)cardButtons
{
    _cardButtons = cardButtons;
    [self updateUI];
}
//method to update the UI
-(void)updateUI
{
    UIImage* cardback = [UIImage imageNamed:@"cardback.png"];//loads in the pic for the cardback
    //go through all the card buttons and update
    for(UIButton *cardButton in self.cardButtons)
    {
        Card* card = [self.game cardAtIndex:[self.cardButtons indexOfObject:cardButton]];//get the card
        [cardButton setTitle:card.contents forState:UIControlStateSelected];
        [cardButton setTitle:card.contents forState:UIControlStateSelected|UIControlStateDisabled];
        cardButton.selected = card.isFaceUp;
        cardButton.enabled = !card.isUnplayable;
        cardButton.alpha = card.isUnplayable ? 0.2 : 1.0;
        if(cardback)
        {
             [cardButton setImage:cardback forState:UIControlStateNormal];
        }
        [cardButton setTitle:@"" forState:UIControlStateNormal];
        self.scoreLabel.text = [NSString stringWithFormat:@"Score: %d",self.game.score];
    }
    self.lastFlip = self.game.lastFlip;//get the result of the last flip from the model
    [self.gameControl setTitle:@"2-Card \n Game" forSegmentAtIndex:0];
    [self.gameControl setTitle:@"3-Card \n Game" forSegmentAtIndex:1];
}
- (IBAction)flipCard:(UIButton *)sender
{
    [self.game flipCardAtIndex:[self.cardButtons indexOfObject:sender]];
    self.flipCount++;
    [self updateUI];
}
//setter for flipCount, update to the UI
-(void)setFlipCount:(int)flipCount
{
    _flipCount = flipCount;
    self.flipsLabel.text = [NSString stringWithFormat:@"Flips: %d",self.flipCount];
}
//setter for lastFlips, update to the UI
-(void)setLastFlip:(NSArray *)lastFlip
{
    _lastFlip = lastFlip;
    //append the last flips together to display the status
    NSString* cardString = [[NSString alloc]init];
    cardString = [self.lastFlip componentsJoinedByString:@" and "];
    if(self.lastFlip.count==1 && ![cardString isEqualToString:@""])
    {
        self.lastFlipLabel.text = [NSString stringWithFormat:@"You just flipped the %@", cardString];
    }
    else if(self.lastFlip.count!=1 && self.game.lastMatch)
    {
        self.lastFlipLabel.text = [NSMutableString stringWithFormat:@"You just matched the %@ \n for %d points!",cardString, self.game.lastScore];
    }
    else if(self.lastFlip.count!=1 && !self.game.lastMatch)
    {
        self.lastFlipLabel.text = [NSString stringWithFormat:@"Oohh, sorry the %@ don't match. %d points for you!", cardString,  self.game.lastScore];
    }
}
//reset the game
- (IBAction)resetGame:(UIButton *)sender
{
    self.game = [[CardMatchingGame alloc]initWithCardCount:self.cardButtons.count usingDeck:[[PlayingCardDeck alloc]init]];
    [self.game reset];
    self.lastFlipLabel.text = @"";//reset the text so it's blank
    self.flipCount = 0;
    [self updateUI];
}


@end

尝试关闭自动布局-在Xcode的故事板视图的右侧面板中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM