【问题标题】:Comparing UIButtons that have UIImageViews比较具有 UIImageViews 的 UIButtons
【发布时间】:2013-09-17 12:43:40
【问题描述】:

我正在编写一个由 2 个UIImageViews 和 2 个UIButtons 组成的简单应用程序。 UIImageViews 位于UIButtons 的顶部。

我有 1 个 UIImage 随机出现在 UIImageView 上,并在 2 秒后消失。然后用户必须点击他们认为图像出现的按钮。

我打乱了UIImageView 数组并使用第一个元素(索引0)作为要显示的图像。洗牌时,我会跟踪哪个元素(即来自哪个索引。假设来自索引“n”)被放置在数组的索引 0 上。

但是,当按下按钮时,我会将按下的按钮的 id 与索引为 n 的按钮的 id 进行比较。 (因为那是随机UIImageView 的索引)。 但由于某种原因,它不起作用。 :(

这是我的代码:(我没有上传 .h 文件。它只包含声明。) 下面的代码不会产生任何错误/警告消息。它只是没有输出我想要的结果。

@interface ViewController ()
@property (strong, nonatomic) NSMutableArray* tempButton;
@property (strong, nonatomic) NSMutableArray *tempViews;
@property (strong, nonatomic) UIImage *myImage;
@end

@implementation ViewController



- (void)viewDidLoad
{
    [super viewDidLoad];

     _myImage = [UIImage imageNamed:@"hello"];
    _tempButton = [[NSMutableArray alloc] initWithObjects:_button1, _button2, nil];

    _tempViews = [[NSMutableArray alloc]initWithObjects:_image1, _image2, nil];


     [self displayonView];

}



-(void)displayToFindSymbol{
    [_toFindImage setImage:_myImage];
    _toFindImage.contentMode = UIViewContentModeScaleAspectFit;
}

- (IBAction)pressed:(id)sender {

    UIButton *result = (UIButton *)sender;
    if (result == _tempButton[_correctButton]) {
       NSLog (@"Correct");
    }
    else if (result != _tempButton[_correctButton]){
       NSLog (@"Incorrect");            

    }

}



-(NSMutableArray *)shuffleViews{
    NSUInteger count = _tempViews.count;
    int n;
    for (int i=count-1; i>=0; --i) {

        n = arc4random()  % (i + 1);


        [_tempViews exchangeObjectAtIndex:n withObjectAtIndex:i];
    }
    _correctButton = n;
    return _tempViews;

}

-(void)displayonView{

    NSMutableArray *tempArray;
    tempArray = [self shuffleViews]; // shuffle views

     _correctImage = [tempArray objectAtIndex:0];

    _correctImage.contentMode = UIViewContentModeScaleAspectFit;

    [_correctImage setImage:_myImage];

    NSTimer* myTImer;
    myTImer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(hideLabel) userInfo:nil repeats:NO];

}

-(void) hideLabel{
    _correctImage.hidden = YES;
    for (UIButton *each in self.tempButton) {
        each.enabled = YES;
    }
    [self displayToFindSymbol];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

【问题讨论】:

    标签: ios objective-c uiimageview uibutton uiimage


    【解决方案1】:

    改为使用标签,为两个按钮设置一个唯一的标签,并将图像的标签设置为图像所属的按钮,稍后,比较图像和按钮的标签以检查它们是否是是否相同。

    https://developer.apple.com/library/ios/documentation/uikit/reference/uiview_class/UIView/UIView.html#//apple_ref/occ/instp/UIView/tag

    【讨论】:

      【解决方案2】:

      我对 Objective-c 和 iOS 也有点陌生,但我认为你不应该这样比较:

      result == _tempButton[_correctButton]
      

      试试这个比较:

      [result isEqual:_tempButton[_correctButton]]
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-09-06
        • 2011-01-24
        • 1970-01-01
        • 2018-11-30
        • 2017-04-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多