代码之家  ›  专栏  ›  技术社区  ›  Abhinav

实现MVC时的问题

  •  0
  • Abhinav  · 技术社区  · 14 年前

    我在实现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 回复  |  直到 14 年前
        1
  •  1
  •   rano    14 年前

    是否已将UIViewController的视图设置为应用程序中AppDelegate附带的窗口视图的子视图 application:didFinishLaunchingWithOptions: ?

        2
  •  0
  •   Abhinav    14 年前