我有一個類與UIImageView設置,我想添加到主viewcontrollers視圖,但它不工作。爲什麼我的ViewController不響應?
這裏是我的代碼,我敢肯定,有件事很明顯缺失:
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];
}
主視圖控制器中沒有任何更改..什麼是錯的?
我也曾嘗試將其與IB連接:
IBOutlet MyViewController *maincontroller;
但它仍然犯規迴應!我該怎麼辦?
我看不到主控制器的任何分配和初始化。也許你可以在ImageClass上爲主控制器創建一個屬性,並在調用[test draw]之前將其設置爲self; – Sarah 2011-05-16 20:08:01