Reputation: 1
So, I am trying to make a shared method in my Brain class which will check the label on a pressed button (buttons of items are in different classes) and then accordingly add a image to a ShoppingList view minding the order of adding (first image added goes to position 0,0, second to position 80,0 etc.).
I have Breakfast class which represents breakfast ingredients and I wanna add a photo of a Tea to another view called ShoppingList.
I wrote a method in Brain that adds an image to a imageView and returns an imageView which is then locally passed to the button pressed.
It builds but when I press the button Tea, application crashes.
Here is my code:
Brain.h
@interface Brain : NSObject {
@private
UIImage *image;
UIImageView *imageView;
}
@property (retain) UIImageView *imageView;
@property (retain) UIImage *image;
- (id)performOperation:(NSString *)operation;
@end
Brain.m
@implementation Brain
@synthesize imageView;
@synthesize image;
- (UIImageView *)performOperation:(NSString *)operation
{
if ([operation isEqual:@"Tea"]) {
image = [UIImage imageNamed:@"Tea_photo.jpg"];
imageView = [[UIImageView alloc]initWithImage:image];
imageView.frame = CGRectMake(0, 0, 80, 80);
return imageView;
//[shoppingList.view addSubview:imageView];
//[imageView release];
}
else return 0;
}
@end
Breakfast.h
@interface Breakfast : UIViewController {
IBOutlet ShoppingList *shoppingList;
Brain *brain;
}
- (IBAction)addItemToShoppingList:(UIButton *)sender;
- (IBAction)goToShoppingList;
- (IBAction)goBack;
@end
Breakfast.m
@implementation Breakfast
- (Brain *)brain
{
if (!brain) brain = [[Brain alloc] init];
return brain;
}
- (IBAction)addItemToShoppingList:(UIButton *)sender
{
NSString *operation = [[sender titleLabel] text];
UIImageView *imageView = [[self brain] performOperation:operation];
[shoppingList.view addSubview:self.brain.imageView];
//UIImageView *imageView = [[UIImageView alloc]initWithImage:image];
//imageView.frame = CGRectMake(0, 0, 80, 80);
//[shoppingList.view addSubview:imageView];
//[imageView release];
}
- (IBAction)goToShoppingList
{
[self presentModalViewController:shoppingList animated:NO];
}
- (IBAction)goBack
{
[self dismissModalViewControllerAnimated:NO];
}
@end
PLEASE HELP, its for my thesis.
Upvotes: 0
Views: 289
Reputation: 6954
I have not gone through your code. But on the basis of your description I can suggest you to create a protocol
Upvotes: 0
Reputation: 26400
IBOutlet ShoppingList *shoppingList;
Why make this IBOutlet
? Isn't ShoppingList
a class that you created. Not that this solves your crash. For that u need to post the crash log....
And also in you code I can't see any allocation
for shoppingList
so how can you be able to use it. You need to allocate
the object in the class Brain
otherwise it doesn't make any sense.
Upvotes: 1