0

誰かが私のアレイがクラッシュする理由を説明するのを手伝ってくれますか?

基本的に、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はあなたの助けに感謝します、乾杯。

4

2 に答える 2

5

例外を見ると、配列には3つのオブジェクトしか含まれていないことがわかります(... bounds [0..2] ...)。だから、私の推測では、あなたの4番目の画像は存在しないと思います...のスペルをチェックしてください[UIImage imageNamed:@"Logo_BirkBeck.png"]。この画像は本当に存在しますか?が返されnil、オブジェクトのリストがnil終了するため、配列には8つの画像ではなく3つの画像のみが含まれます。

補足:のような魔法の定数は使用しないでくださいlocalSelctorInt < 7。配列内の要素の実数に常に依存します(_myImageArray.count)。それは地獄への道です...

2番目の側面注:アンダースコアプレフィックスなしでivarを宣言しないでください。_myImageArrayivar名にはこのようなものを使用してください。

于 2012-09-03T09:00:27.537 に答える
1

配列を割り当てていないためだと思います

myImageArray = [[NSArray alloc] initWithObjects:
        [UIImage imageNamed:@"Logo_Arm.png"],
        [UIImage imageNamed:@"Logo_Fife.png"],
        [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];
于 2012-09-03T09:01:39.720 に答える