实现MVC时出现问题

时间:2010-08-23 05:47:32

标签: iphone

我在实施MVC时遇到了一个问题。我创建了一个UIView子类并设计了我的自定义视图。我创建了一个UIViewController子类,并将其视图指向我的自定义视图类。最后,我创建了一个NSObject子类,并为我的控制器创建了一个模型。当我运行应用程序时,我得到一个黑屏......对此有什么想法吗?

==我的模特==

- (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];
}

2 个答案:

答案 0 :(得分:1)

您是否已将UIViewController的视图设置为application:didFinishLaunchingWithOptions:中AppDelegate附带的Window视图的子视图?

答案 1 :(得分:0)

我遇到了问题......实际上,我没有为我的UIView子类组件分配内存,而是尝试使用drawAtPoint方法。一旦我分配了内存并将其添加为我的视图类的子视图,我就会显示正确的屏幕。