带有UIImage和NSString对象的NSMutableArray无法使用这些对象

时间:2012-02-07 10:35:12

标签: iphone uiimageview nsmutablearray

我是xCode和objective-c的新手,并尝试使用不同的类来熟悉该语言。

我试图使用NSMutableArray来显示几张图片和相应的文本片段来讲述图片。我有一个segmentedControl来切换几个选项。使用UIImageView显示图片,并使用UITextView对应的文本。

但由于某种原因,它们无法正常加载?图片和文字未显示。我做错了什么?

@interface TestingViewController : UIViewController{
    UIImageView *pic;
    UITextView *description;
    UISegmentedControl *choice;
    NSMutableArray *infoDescription;
}

@property (nonatomic, retain) IBOutlet UIImageView *pic;
@property (nonatomic, retain) IBOutlet UITextView *description;
@property (nonatomic, retain) NSMutableArray *infoDescription;
@property (nonatomic, retain) IBOutlet UISegmentedControl *choice;

- (IBAction) segmentedControllIndexChanged;

@end

- (void)viewDidLoad
{
   [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
      infoDescription = [[NSMutableArray alloc] init];

}

UIImage *miko = [UIImage imageNamed:@"miko.jpg"];
NSString *mikoString = @"Info about Miko";
UIImage *alex = [UIImage imageNamed:@"alex.jpg"];
NSString *alexString = @"Info about Alex";

[infoDescription addObject:miko];
[infoDescription addObject:mikoString];
[infoDescription addObject:alex];
[infoDescription addObject:alexString];

- (IBAction)segmentedControllIndexChanged{

switch (choice.selectedSegmentIndex) {
    case 0:
       [pic setImage: miko];
        [pic setImage: [infoDescription objectAtIndex:0]];
       [description setText: [[infoDescription objectAtIndex:1] stringValue]];    
        break;
    case 1:
      [pic setImage: [infoDescription objectAtIndex:2]];
      [description setText: [[infoDescription objectAtIndex:3] stringValue]];
        break;
    default:
        break;
}   

}

感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

您的infoDescription数组可能为零。在添加项目之前将其放入:

infoDescription = [[NSMutableArray alloc] init];

此外,infoDescription不需要声明为IBOutlet。 IBOutlets仅适用于在Interface Builder中绑定的属性(通常是视图)。

答案 1 :(得分:0)

NSMutableArray不应该是IBOutlet ..

更改此

@property (nonatomic, retain) IBOutlet NSMutableArray *infoDescription;

@property (nonatomic, retain) NSMutableArray *infoDescription;

并确保您合成它.. 并且该程序无法正常工作,因为您的数组未初始化且未向其添加对象。

将此内容放入ViewDidLoad

infoDescription = [[NSMutableArray alloc] init];