2012-04-20 55 views
0

我有一個視圖的項目。我正在通過程序繪製所有內容。所以,當我想另一個視圖(屏幕)添加到項目中,我創建一個類從UIViewController類繼承並實現方法Dynamiclly創建在iOS中查看

- (void)viewDidLoad 

然後,我想從我原來查看加載這個視圖,我這樣做:

ViewController.h

#import <UIKit/UIKit.h> 
#import "TestViewControllerClass.h" 

@interface ViewController : UIViewController <UITableViewDataSource> { 
} 

@property (strong,nonatomic) TestViewControllerClass *testView; 

@end 

ViewController.m

@implementation ViewController 

@synthesize testView; 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    testView = [[TestViewControllerClass alloc] init]; 
    [self.view addSubview:testView]; //crash here 

} 

然後在我的TestViewControllerClass.h

#import <UIKit/UIKit.h> 

@interface TestViewControllerClass : UIViewController 

@end 

而且TestViewControllerClass.m

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
} 

要檢查方法wiewDidLoad將被執行,我把有斷點,但沒有發生。事實上,我的應用程序崩潰(我把代碼註釋在哪裏)。

當崩潰我接收:- [TestViewControllerClass上海華]:無法識別的選擇發送到實例0x683aca0

回答

3

替換

[self.view addSubview:testView]; //crash here 

[self.view addSubview:testView.view]; 
+0

哦,天啊!謝謝! – Kuba 2012-04-20 12:34:02

2

使用此代碼.. 。

[self.view addSubview:testView.view]; 

希望,這將幫助你......

0

您可以加載兩種下面的UIViewController加載:

[self.view addSubview:testView.view]; 

(或)

使用presentmodalviewcontroller下面的代碼:

testView *popupView = [[testView alloc] initWithNibName:@"testView" bundle:nil]; 
    popupView.modalTransitionStyle = UIModalTransitionStyleCoverVertical; 
    [self presentModalViewController:popupView animated:YES]; 

dimiss modal view cntroller下面的代碼:

[self dismissModalViewControllerAnimated:YES]; 

謝謝..!

2

你在做什麼是你的設置作爲的ViewController的觀點,不是真正的視圖

testView = [[TestViewControllerClass alloc] init]; 
    [self.view addSubview:testView]; //crash here 

這顯然崩潰。假設你的視圖頭文件中聲明瞭一個視圖變量,使用

testView = [[TestViewControllerClass alloc] init]; 
    [self.view addSubview:testView.view];