2012-02-07 50 views
0

我是新来xCode和objective-c,并尝试使用不同的类来熟悉我的语言。NSMutableArray,与UIImage和NSString对象,无法使用对象

我试图用一个NSMutableArray来显示几张图片和一个相应的textsnippet来告诉图片。我有一个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; 
} 

}

感谢您的帮助那里。

回答

0

NSMutableArray的不应该是一个IBOutlet ..

更改此

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

@property (nonatomic, retain) NSMutableArray *infoDescription; 

,并确保你合成它.. ,也是程序ISN因为你的数组没有被初始化,也没有添加对象。

把这个在viewDidLoad中

infoDescription = [[NSMutableArray alloc] init]; 
+0

实现你所说的话,但现在我只是SIGABORT得到。 – Miko 2012-02-07 10:53:08

+0

使用ARC?在控制台中是否有任何错误描述? – Shubhank 2012-02-07 10:56:57

+0

我第一次使用图片和文本开始,但是这是在interfacebuilder中完成的,当我按下segmentedControll时出现SIGABRT。 – Miko 2012-02-07 11:16:36

0

您的infoDescription数组大概为零。把它添加到它之前:

infoDescription = [[NSMutableArray alloc] init]; 

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