I have a class set up with a UIImageView and I want to add that to the main viewcontrollers view, but it doesnt work.
Here is my code, Im sure theres something obvious missing:
MyViewController.h:
#import <UIKit/UIKit.h>
#import "ImageClass.h"
@class ImageClass;
@interface MyViewController : UIViewController {
}
@end
MyViewController.m:
#import "MyViewController.h"
@implementation MyViewController
- (void)viewDidLoad {
ImageClass *test = [[ImageClass alloc] init];
[test draw];
[super viewDidLoad];
}
ImageClass.h:
#import <UIKit/UIKit.h>
#import "EnkelBildeKlasseViewController.h"
@class MyViewController;
@interface ImageClass : NSObject {
MyViewController *maincontroller;
UIImageView *image;
}
@property (nonatomic, retain) UIImageView *image;
-(void)draw;
@end
ImageClass.m:
#import "ImageClass.h"
#import "MyViewController.h"
@implementation ImageClass
@synthesize image;
- (id)init {
if ((self = [super init])) {
}
return self;
}
-(void)draw {
maincontroller.view.backgroundColor = [UIColor redColor];
image = [[UIImageView alloc] init];
image.backgroundColor = [UIColor greenColor];
image.frame = CGRectMake(100, 100, 100, 100);
[maincontroller.view addSubview:image];
}
Nothing in the main viewcontroller changes.. What is wrong?
I have also tried to connect it with IB:
IBOutlet MyViewController *maincontroller;
But it still doesnt respond! What should I do?
You need to do your drawing in drawRect:, and you need to make imageClass a UIView subclass, and add it as a subview. See this tutorial: http://littlecomputers.net/2010/?page_id=41