APRULE
APRULE

Reputation: 115

inheriting a method from other class

I built an application that contain several classes. one of the classes named "photoItem" and that class contain an image item and a method.

I tried to uses that method in my appDelagate but it didn't work.

my photoitem.h is: #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

here is my 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

and my appdelagate is:

 #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];

}

I want to implement the method in the last line: [photoArray addObject:[photoItem XXXXXX] but xcode didnt let me choose and use this method.

what is the problem?

Upvotes: 0

Views: 75

Answers (1)

timthetoolman
timthetoolman

Reputation: 4623

As your code stands now, it is not working for the following reasons: 1. Your photoArray needs to be listed as a property and synthesized 2. You have not alloc/init a photoItem yet.

In the interface (.h) do this:

@property(nonatomic,strong) NSMutableArray *photoArray;

In the implementation (.m) after the @implementation directive do this, either before or after your @synthesize window:

 @synthesize photoArray;

Also, you should name classes with a Set of capital letters first, so that you( and others) can quickly differentiate between classes and instances of classes. You could preface it with the initials of your company or your name. For example, if your name was John Smith you could name the class JSPhotoItem. code would look something like this:

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

 [photoArray addObject:photoItem];

Good luck

T

Upvotes: 1

Related Questions