0

我构建了一个包含多个类的应用程序。其中一个名为“photoItem”的类包含一个图像项和一个方法。

我尝试在我的 appDelagate 中使用该方法,但没有奏效。

我的 photoitem.h 是:#import

@interface photoItem : NSObject
 {
     UIImage *imageView;
     NSString *photoNameLabel;
     NSString *photographerNameLabel;
     UIButton *viewPhoto;
 }
 @property(readonly) NSString *name;
 @property(readonly) NSString *nameOfPhotographer;
 @property(readonly) UIImage *imageItem;

 -(id)makePhotoItemWIthPhoto:(UIImage*)image name:(NSString*)photoName photographer:   (NSString*)photographerName;

@end

这是我的 photoitem.m:

#import "photoItem.h"

@implementation photoItem

@synthesize name;
@synthesize nameOfPhotographer;
@synthesize imageItem;

-(id)makePhotoItemWIthPhoto:(UIImage*)image name:(NSString*)photoName photographer:(NSString*)photographerName
{
    [self setName:photoName];
    [self setNameOfPhotographer:photographerName];
    [self setImageItem:image];
    return self;
}

-(void) setName:(NSString *)name
{
    photoNameLabel = name;
}  

-(void) setNameOfPhotographer:(NSString *)nameOfPhotographer
{
    photographerNameLabel = nameOfPhotographer;
}

-(void)setImageItem:(UIImage *)imageItem
{
    imageView = imageItem;
}
@end

我的应用程序是:

 #import "AppDelegate.h"
 #import "PersonListViewController.h"
 #import "RecentsViewController.h"
 #import "PhotoListViewController.h"
 #import "photoItem.h"

 @implementation AppDelegate

 @synthesize window = _window;

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
 {
     self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
     // Override point for customization after application launch.

     photoArray = [[NSMutableArray alloc] init];
    [photoArray addObject:[photoItem XXXXXX];

}

我想实现最后一行中的方法:[photoArray addObject:[photoItem XXXXXX] 但是xcode没有让我选择和使用这个方法。

问题是什么?

4

1 回答 1

1

就您的代码现在而言,由于以下原因,它无法正常工作: 1. 您的 photoArray 需要作为属性列出并合成 2. 您还没有分配/初始化 photoItem。

在界面 (.h) 中执行以下操作:

@property(nonatomic,strong) NSMutableArray *photoArray;

在 @implementation 指令之后的实现 (.m) 中,在您的 @synthesize 窗口之前或之后执行此操作:

 @synthesize photoArray;

此外,您应该首先使用一组大写字母命名类,以便您(和其他人)可以快速区分类和类的实例。您可以在其前面加上您公司的首字母或您的姓名。例如,如果您的名字是 John Smith,您可以将类命名为 JSPhotoItem。代码看起来像这样:

 photoArray = [[NSMutableArray alloc] init];
 JSPhotoItem *photoItem=[JSPhotoItem alloc] init];
  // perform any other initialization of the photoItem object

 [photoArray addObject:photoItem];

祝你好运

于 2012-05-30T20:14:49.083 回答