NSMutableArray,带有UIImage和NSString对象,无法使用这些对象
我是 xCode 和 Objective-C 的新手,并尝试使用不同的类来熟悉该语言。
我试图使用 NSMutableArray 来显示几张图片和相应的文本片段来讲述图片。我有一个分段控件可以在多个选择之间切换。使用 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;
}
感谢
您的帮助。
I'm new to xCode and objective-c, and trying to use different classes to familiarize myself with the language.
I was trying to use an NSMutableArray to show several pictures and a corresponding textsnippet to tell about the picture. I have a segmentedControl to switch between several choices. Using an UIImageView to show a picture, and a UITextView the corresponding text.
But for some reason they will not load properly? The picture and the text isn't shown. What am I doing wrong?
@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;
}
}
Thanks for any help out there.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
NSMutableArray 不应该是 IBOutlet ..
将其更改
为
并确保合成它..
并且该程序无法运行,因为您的数组未初始化并且未向其中添加对象。
将其放入 ViewDidLoad 中
NSMutableArray should not be an IBOutlet ..
Change this
to
and make sure you synthesize it..
and also the program isn't working since your array is not initialized and is not adding objects to it.
put this in ViewDidLoad
您的 infoDescription 数组可能为零。将其放在向其添加项目之前:
此外,infoDescription 不需要声明为 IBOutlet。 IBOutlet 仅适用于您在 Interface Builder 中绑定的属性(通常是视图)。
Your infoDescription array is probably nil. Put this before you add items to it:
Also, infoDescription doesn't need to be declared as an IBOutlet. IBOutlets are only for properties that you bind in Interface Builder (usually views).