谁能帮我解释为什么我的阵列崩溃了。
基本上我有两个按钮可以更改 imageView 中的图像。
H:
@interface CaseStudySecondPageViewController : UIViewController
{
//scroller and back and forward buttons for custom control.
__weak IBOutlet UIScrollView *scroller;
__weak IBOutlet UIButton *back;
__weak IBOutlet UIButton *forward;
//app delegate to return the selected case study from the other controller.
AppDelegate *del;
//variables for displaying the case studies
NSArray *myImageArray;
NSInteger localSelctorInt;
}
//setup a IBOutlet to allow the image to be changed.
@property (weak, nonatomic) IBOutlet UIImageView *logoImage;
@end
米:
更新方法:
-(void)Updater
{
[logoImage setImage:[myImageArray objectAtIndex:localSelctorInt]];
}
上一个和下一个按钮:
//returns to previous image if back button is clicked.
-(void) back:(id)sender
{
if (localSelctorInt > 0)
{
localSelctorInt--;
}
else
{
localSelctorInt = 0;
}
[self Updater];
}
//returns to next image if forward button is clicked. increase 7 if array size changes.
//1 removed from int as in starts at 1 and array starts at 0.
-(void) forward:(id)sender
{
if (localSelctorInt < 7)
{
localSelctorInt++;
}
else
{
localSelctorInt = 7;
}
[self Updater];
}
最后我的图像数组声明在:
- (void)viewDidLoad
{
[super viewDidLoad];
//setup the delegate to allow access to the int which was modified on the page before.
del = [[UIApplication sharedApplication] delegate];
//assign a local variable to the int from the previous page.
localSelctorInt = *(del.selectorInt);
//create back and forward buttons as UIButtons first.
[back addTarget:self action:@selector(back: ) forControlEvents:UIControlEventTouchUpInside];
[forward addTarget:self action:@selector(forward:) forControlEvents:UIControlEventTouchUpInside];
//pass the ui buttons into ui bar items.
UIBarButtonItem *UIBack = [[UIBarButtonItem alloc] initWithCustomView:back];
UIBarButtonItem *UIForward = [[UIBarButtonItem alloc] initWithCustomView:forward];
//add these to an array (notice item"s").
self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:UIForward, UIBack, nil];
//initialize the array with all of the images for the case studies logos.
myImageArray = [NSArray arrayWithObjects:
[UIImage imageNamed:@"Logo_Arm.png"],
[UIImage imageNamed:@"Logo_Fife"],
[UIImage imageNamed:@"Logo_Findel.png"],
[UIImage imageNamed:@"Logo_BirkBeck.png"],
[UIImage imageNamed:@"Logo_NHS_Dudley.png"],
[UIImage imageNamed:@"Logo_NHS_Kensignton.png"],
[UIImage imageNamed:@"Logo_Yorkshire_Water.png"],
[UIImage imageNamed:@"Logo_Uni_Hertfordshire.png"],
nil];
//call update method once to load the first image selected from the previous page.
[self Updater];
}
现在我的应用程序在尝试索引第 4 个图像(数组中的第 3 个)时崩溃:
* 由于未捕获的异常“NSRangeException”而终止应用程序,原因:“* -[__NSArrayI objectAtIndex:]: index 3 beyond bounds [0 .. 2]”
这可能是非常简单的事情,但我感谢您的帮助,干杯。