【发布时间】:2015-08-05 04:59:46
【问题描述】:
我想添加几个ViewController 的视图作为我另一个viewcontroller 的子视图。所以我在我的父母viewController里面做了这样的事情。
-(void)MakeDisplayArea
{
vwDisplayArea=[[UIView alloc] initWithFrame:CGRectMake(0, 0, w, h-scrolTab.frame.size.height)];
[self.view addSubview:vwDisplayArea];
}
-(void)ProfClick :(id)sender
{
[self MakeDisplayArea];
ProfileViewController *profviewcontroller=[[ProfileViewController alloc] initWithNibName:@"ProfileViewController" bundle:nil];
profviewcontroller.view.frame=vwDisplayArea.frame;
[profviewcontroller.view setBackgroundColor:[UIColor clearColor]];
[vwDisplayArea addSubview:profviewcontroller.view];
}
然后在 profViewcontroller ViewDidLoad 方法中,我正在生成一个按钮并像这样设置目标。
UIButton *btn=[[UIButton alloc] initWithFrame:CGRectMake(10, 60, 100, 30)];
[btn setTitle:@"Button Test" forState:UIControlStateNormal];
[self.view addSubview:btn];
[btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
-(IBAction)btnClick :(id)sender
{
NSLog(@"button clicked-------");
}
我可以看到该按钮出现在我作为子视图加载的 profViewcontroller 上,但是当我单击该按钮时,应用程序崩溃了。在我的第一个viewcontroller 上添加viewcontroller 作为subview 的正确方法是什么。请帮帮我。
谢谢
【问题讨论】:
-
阅读
UIViewController中关于实现容器视图控制器的文档。除了添加子视图之外,您还需要添加子视图控制器。 -
崩溃日志给你什么错误?
标签: ios objective-c uiviewcontroller uibutton addsubview