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

发布于 2025-01-02 19:26:21 字数 1725 浏览 1 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

薄荷→糖丶微凉 2025-01-09 19:26:21

NSMutableArray 不应该是 IBOutlet ..

将其更改

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

@property (nonatomic, retain) NSMutableArray *infoDescription;

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

将其放入 ViewDidLoad 中

infoDescription = [[NSMutableArray alloc] init];

NSMutableArray should not be an IBOutlet ..

Change this

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

to

@property (nonatomic, retain) NSMutableArray *infoDescription;

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 = [[NSMutableArray alloc] init];
堇色安年 2025-01-09 19:26:21

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

infoDescription = [[NSMutableArray alloc] init];

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

Your infoDescription array is probably nil. Put this before you add items to it:

infoDescription = [[NSMutableArray alloc] init];

Also, infoDescription doesn't need to be declared as an IBOutlet. IBOutlets are only for properties that you bind in Interface Builder (usually views).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文