2012-04-20 56 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];