0

谁能帮我解释为什么我的阵列崩溃了。

基本上我有两个按钮可以更改 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]”

这可能是非常简单的事情,但我感谢您的帮助,干杯。

4

2 回答 2

5

如果您查看您的异常,它表示您的数组仅包含 3 个对象 ( ... bounds [0..2] ...)。所以,我的猜测是你的第四张图片不存在......检查 . 的拼写[UIImage imageNamed:@"Logo_BirkBeck.png"]。这张图真的存在吗?它返回nil,对象列表被nil终止,因此,您的数组确实只包含 3 个图像而不是 8 个图像。

旁注:不要使用像localSelctorInt < 7. 始终依赖数组 ( _myImageArray.count) 中的实数元素。这是一条通往地狱的路……

第二边注:不要声明没有下划线前缀的 ivars。_myImageArray对 ivar 名称使用类似的东西。

于 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 回答