调用方法在 UIImageView 中显示图像数组时出错

Error when calling method to display am array of images in a UIImageView

我以编程方式每 30 秒在一个方法中创建一个 UIImageView,然后启动一个 NSTimer,它将使用该 UIImageView 显示图像数组 5 秒。但是,当调用该方法时,应用程序会崩溃。

应用行数:

-(void)CollectPoints{
    if(PlayTimer < 59){
        PlayTimer = PlayTimer + 1;
    }
    else{
        PlayTimer = 0;
        timeMinute+=1;
    }

    if(PlayTimer == 5 || (PlayTimer == 0 && timeMinute > 0)){
        bonusItemY = (575 - arc4random() % (515));
        bonusItemX = ((self.view.center.x + 20) + arc4random() % (80));
        UIImageView *bonusImg = [[UIImageView alloc] initWithFrame:CGRectMake(bonusItemX, bonusItemY, 50, 50)];
        bonusImg.image = [UIImage imageNamed:[bonusImgs objectAtIndex:bonusImgsNum]];
        //[self.view addSubview:bonusImg];
        bonusImgDraw = [NSTimer scheduledTimerWithTimeInterval:.1 target:self selector:@selector(bonusItems) userInfo:bonusImg repeats:YES];
    }
}

-(void)bonusItems:(UIImageView*)bonusImg{
    if(bonusImgsNum < 1){
        bonusImgsNum+=1;
    }
    else{
        bonusImgsNum = 0;
    }
    bonusImg.image = [UIImage imageNamed:[bonusImgs objectAtIndex:bonusImgsNum]];
}

错误显示:

[ViewController2 bonusItems]: unrecognized selector sent to instance 0x7f9279620c80
2016-05-02 18:28:39.676 Jumping-Jinn[2220:140492] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[ViewController2 bonusItems]: unrecognized selector sent to instance 0x7f9279620c80'
*** First throw call stack:
(
    0   CoreFoundation                      0x0000000103b5cd85 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x00000001032c3deb objc_exception_throw + 48
    2   CoreFoundation                      0x0000000103b65d3d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
    3   CoreFoundation                      0x0000000103aabcfa ___forwarding___ + 970
    4   CoreFoundation                      0x0000000103aab8a8 _CF_forwarding_prep_0 + 120
    5   Foundation                          0x0000000102eaa811 __NSFireTimer + 83
    6   CoreFoundation                      0x0000000103ab6074 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 20
    7   CoreFoundation                      0x0000000103ab5c21 __CFRunLoopDoTimer + 1089
    8   CoreFoundation                      0x0000000103a77b11 __CFRunLoopRun + 1937
    9   CoreFoundation                      0x0000000103a770f8 CFRunLoopRunSpecific + 488
    10  GraphicsServices                    0x000000010863aad2 GSEventRunModal + 161
    11  UIKit                               0x00000001045daf09 UIApplicationMain + 171
    12  Jumping-Jinn                        0x0000000102db9a5f main + 111
    13  libdyld.dylib                       0x00000001069fe92d start + 1
    14  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

失败行如下:

bonusImg.image = [UIImage imageNamed:[bonusImgs objectAtIndex:bonusImgsNum]];

你有两个问题。

  1. 您在创建计时器时传递了错误的选择器。
  2. 您的 bonusItems: 方法的签名不正确。

创建定时器时,需要使用:

bonusImgDraw = [NSTimer scheduledTimerWithTimeInterval:.1 target:self selector:@selector(bonusItems:) userInfo:bonusImg repeats:YES];

唯一的变化是在内部传递的方法名称中添加了冒号@selector()

定时器方法采用一个参数——触发它的定时器。因此,您的 bonusItems: 方法需要以:

开头
-(void)bonusItems:(NSTimer *)timer {

由于您想获得对 bonusImg 的引用,因此您将其作为计时器的 userInfo 传递。你可以通过定时器的userInfo属性在bonusItems:方法里面得到这个。所以你的方法变成:

-(void)bonusItems:(NSTimer *)timer {
    UIImageView *bonusImg = timer.userInfo;

    if (bonusImgsNum < 1) {
        bonusImgsNum+=1;
    } else {
        bonusImgsNum = 0;
    }

    bonusImg.image = [UIImage imageNamed:bonusImgs[bonusImgsNum]];
}