誰かが私のアレイがクラッシュする理由を説明するのを手伝ってくれますか?
基本的に、imageViewの画像を変更する2つのボタンがあります。
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
m:
更新方法:
-(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:]:インデックス3が境界を超えています[0 .. 2] '
それはおそらく本当に単純なことですが、idはあなたの助けに感謝します、乾杯。