2013-10-04 89 views
0

之間協調我是相當新的iOS開發,我不能夠環繞一個概念我的頭..怎樣的UIViewController和UIView的

我有兩個UIViewController類 - ViewControllerSecondViewController。最初,文件的ViewController類被加載,其中僅包含UIButton。當點擊此按鈕時,SecondViewController開始起作用,它需要加載由我的UIView類創建的視圖,該類名爲ViewClassViewClass只包含一個UIButton

單擊ViewController xib上的按鈕,我所得到的只是一個黑屏,我不明白爲什麼loadView和viewDidLoad方法會被調用很多次。(輸出如下所示)!

這裏是我的代碼..

ViewController.m

@implementation ViewController 
@synthesize secondViewController = _secondViewController; 

-(id) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if(self) 
    { 
     NSLog(@"init of ViewController called"); 
     self.secondViewController = [[SecondViewController alloc] init]; 
    } 
    return self; 
} 

- (IBAction)buttonPressed:(id)sender { 
    [self presentViewController:self.secondViewController animated:YES completion:NULL]; 
    NSLog(@"presented"); 
} 

SecondViewController.m

-(void)loadView 
{ 
    NSLog(@"loadView Called"); 
    self.myView = [[ViewClass alloc] init]; 
    [self.view addSubview:self.myView]; 
} 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     NSLog(@"init of SVC called"); 
    } 
    return self; 
} 

-(void)viewWillAppear:(BOOL)animated 
{ 
    NSLog(@"view WIll appear called"); 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    NSLog(@"view did load called"); 
} 

ViewClass.m

- (id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 
     NSLog(@"init OF View Called"); 
     UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
     button.titleLabel.text = @"Heyy"; 
     [self addSubview:button]; 
    } 
    return self; 
} 

,這就是我得到的控制檯..

2013-10-04 11:03:08.454 MVCPractice1[30957:a0b] Cannot find executable for CFBundle 0xa894b30 </Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator7.0.sdk/System/Library/AccessibilityBundles/CertUIFramework.axbundle> (not loaded) 
2013-10-04 11:03:08.503 MVCPractice1[30957:a0b] init of ViewController called 
2013-10-04 11:03:08.504 MVCPractice1[30957:a0b] init of SVC called 
2013-10-04 11:03:09.773 MVCPractice1[30957:a0b] loadView Called 
2013-10-04 11:03:09.774 MVCPractice1[30957:a0b] init OF View Called 
2013-10-04 11:03:09.775 MVCPractice1[30957:a0b] view did load called 
2013-10-04 11:03:09.776 MVCPractice1[30957:a0b] loadView Called 
2013-10-04 11:03:09.777 MVCPractice1[30957:a0b] init OF View Called 
2013-10-04 11:03:09.778 MVCPractice1[30957:a0b] view did load called 
2013-10-04 11:03:09.779 MVCPractice1[30957:a0b] loadView Called 
2013-10-04 11:03:09.779 MVCPractice1[30957:a0b] init OF View Called 
2013-10-04 11:03:09.780 MVCPractice1[30957:a0b] view did load called 
2013-10-04 11:03:09.781 MVCPractice1[30957:a0b] loadView Called 
2013-10-04 11:03:09.781 MVCPractice1[30957:a0b] init OF View Called 
2013-10-04 11:03:09.782 MVCPractice1[30957:a0b] view did load called 
2013-10-04 11:03:09.783 MVCPractice1[30957:a0b] view WIll appear called 
2013-10-04 11:03:09.783 MVCPractice1[30957:a0b] loadView Called 
2013-10-04 11:03:09.784 MVCPractice1[30957:a0b] init OF View Called 
2013-10-04 11:03:09.785 MVCPractice1[30957:a0b] view did load called 
2013-10-04 11:03:09.785 MVCPractice1[30957:a0b] loadView Called 
2013-10-04 11:03:09.786 MVCPractice1[30957:a0b] init OF View Called 
2013-10-04 11:03:09.787 MVCPractice1[30957:a0b] view did load called 
2013-10-04 11:03:09.787 MVCPractice1[30957:a0b] presented 

回答

2

loadView工作是設定self.view到一個視圖。當您收到loadView消息時,self.view爲零。從某處獲取視圖(通過在代碼中創建視圖或加載筆尖)並將該視圖存儲在self.view中是您的工作。

loadView方法必須設置self.view到一個視圖返回前,和不得設置self.viewself.view吸氣之前通話噸。試試這個:

-(void)loadView { 
    NSLog(@"loadView Called"); 
    self.view = [[ViewClass alloc] init]; 
} 
+0

行..我..但我仍不能看到我添加的viewClass類的按鈕。我仍然得到一個空白的黑色屏幕 – Shradha

0

也請在viewDidLoad中的初始化當你正在處理的UIViewController這將是一個很好的做法。

-(id) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if(self) 
    { 
     NSLog(@"init of ViewController called"); 

    } 
    return self; 
} 
- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    NSLog(@"view did load called"); 
    self.secondViewController = [[SecondViewController alloc] init]; 
} 

也改變的loadView方法搶劫mayoff解釋..

+0

好..完成..但即使在更改後,我點擊ViewController按鈕後得到一個空白屏幕..理想情況下,我應該得到一個視圖,說一個按鈕,說「Heyy」 – Shradha