开发者

Problem Exc_Arithmetic in Obj-C program for iphone

开发者 https://www.devze.com 2023-03-26 15:29 出处:网络
I am writing a program with a UIImageView. However, I am running into the error: Exc_Arithmetic. Here is my implementation:

I am writing a program with a UIImageView. However, I am running into the error: Exc_Arithmetic.

Here is my implementation:

#import "SalaatAppViewController.h"

@implementation SalaatAppViewController

@synthesize imageView;
@synthesize pauseButton;
@synthesize playButton;
@synthesize nextButton;

-(IBAction)pushPauseButton {
    [imageView stopAnimating];
}

-(IBAction)pushPlayButton {
    [imageView startAnimating];
}

- (IBAction)pushNextButton {

    imagesIndex = (imagesIndex + 1) % [images count];
    imageView.image = [images objectAtIndex:imagesIndex];

}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle


// Implement viewDidLo开发者_StackOverflow社区ad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    imageView.animationImages = [[NSArray arrayWithObjects:
                                 [UIImage imageNamed:@"1.png"],
                                 [UIImage imageNamed:@"2.png"],
                                 [UIImage imageNamed:@"3.png"],
                                  nil] retain];
    UIImage *image = [UIImage imageNamed: @"2.png"];
    [imageView setImage:image];
    imageView.animationDuration = 1.00;
    imageView.animationRepeatCount = 0;
    [imageView startAnimating];
    imagesIndex = 0;
    imageView.image = [images objectAtIndex:imagesIndex];
    [super viewDidLoad];
    [self.view addSubview:imageView];

}


- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

-(void) dealloc {
    [imageView release];
    [pauseButton release];
    [playButton release];
    [nextButton release];
    [super dealloc];

}

@end

My Header:

#import <UIKit/UIKit.h>

@interface SalaatAppViewController : UIViewController {
    UIImageView *imageView;
    UIButton *pauseButton;
    UIButton *playButton;
    UIButton *nextButton;
    NSUInteger images;
    NSUInteger imagesIndex;
}

@property (nonatomic, retain) IBOutlet UIImageView *imageView;
@property (nonatomic, retain) IBOutlet UIButton *pauseButton;
@property (nonatomic, retain) IBOutlet UIButton *playButton;
@property (nonatomic, retain) IBOutlet UIButton *nextButton;


-(IBAction)pushPauseButton;
-(IBAction)pushPlayButton;
-(IBAction)pushNextButton;

@end

The program runs, but with 9 warnings and 1 error. Also, when I push the Next Button, it crashes. Please Help!


The exception indicates that you tried to divide by 0. This expression does a modulus (%) function which returns remainder after division, probably [images count] is 0:

imagesIndex = (imagesIndex + 1) % [images count];


Ahmad your "images" property should be not NSUInteger but NSArray*. Define it in a following way:

NSArray *images;

It seems you replaced too much by following my previous guidance. =)


You never initialize images. Also, as you're calling the count method, it seems that it should be an NSArray and not an NSUInteger.

0

精彩评论

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