2010-08-23 101 views
0

我在實現MVC時遇到問題。我創建了一個UIView子類並設計了我的自定義視圖。我創建了一個UIViewController子類,並將其視圖指向我的自定義視圖類。最後,我創建了一個NSObject子類併爲我的控制器創建了一個模型。當我運行應用程序時,我正在變黑屏幕......對此有何想法?實現MVC時出現問題

==我的模型==

- (id) init 
{ 
    imgArray = [[NSMutableArray alloc] initWithObjects: [UIImage imageNamed:@"scene.jpg"], 
     [UIImage imageNamed:@"scene1.jpg"], 
     [UIImage imageNamed:@"scene2.jpg"], nil]; 

    return self; 
} 

==我的控制器==

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    NSNotification 
    CGRect mainFrame = [[UIScreen mainScreen] applicationFrame]; 

    // Get the view 
    MVCView *myView = [[MVCView alloc] initWithFrame:mainFrame]; 

    // Get the data 
    MVCModel *myModel = [[MVCModel alloc] init]; 
    myView.myImgView.image = [myModel.imgArray objectAtIndex:0]; 

    //[self.view addSubview:myView]; 
    [myView setNeedsDisplay]; 
    self.view = myView; 
    self.title = @"MVC"; 
} 

==我查看==

- (id)initWithFrame:(CGRect)frame { 
    if ((self = [super initWithFrame:frame])) { 
     // Initialization code 
     CGRect imgFrame = CGRectMake(5, 20, 150, 150); 
     //myImgView.image = [UIImage imageNamed:@"scene.jpg"]; 
     myImgView.frame = imgFrame; 

     CGRect lblFrame = CGRectMake(5, 150, 50, 50); 
     myLabel.text = @"Nature is beautiful"; 
     myLabel.frame = lblFrame; 

     CGRect sldFrame = CGRectMake(5, 200, 50, 50); 
     mySlider.minimumValue = 0; 
     mySlider.maximumValue = 100; 
     mySlider.frame = sldFrame; 
    } 
    return self; 
} 

// Only override drawRect: if you perform custom drawing. 
// An empty implementation adversely affects performance during animation. 
- (void)drawRect:(CGRect)rect { 
    // Drawing code 

    CGPoint imgPoint = CGPointMake(10, 20); 
    [self.myImgView drawAtPoint:imgPoint]; 

    CGPoint lblPoint = CGPointMake(10, 160); 
    [self.myLabel drawAtPoint:lblPoint]; 

    CGPoint sldPoint = CGPointMake(10, 210); 
    [self.mySlider drawAtPoint:sldPoint]; 
} 

回答

0

我得到了問題......其實,我沒有分配內存給我的UIView子類組件,並試圖使用drawAtPoint方法。一旦我分配了內存並將它們添加爲我的視圖類的子視圖,我得到了正確的屏幕顯示。

1

已經設置您的UIViewController的視圖窗口視圖的子視圖,在您的AppDelegate附帶的application:didFinishLaunchingWithOptions:中?

+0

嗨Rano, 我使用的是基於導航的應用程序,所以我已將自定義視圖控制器推入堆棧。我得到正確內容的導航欄,但查看部分是黑色的。 – Abhinav 2010-08-23 06:33:10

+0

@Abhinav:你有沒有試圖在viewDidLoad中的控制器init方法中分配你的視圖? – rano 2010-08-23 07:53:22