【问题标题】:UIButton click event crash after adding the viewcontroller's view as a sub view将viewcontroller的视图添加为子视图后UIButton点击事件崩溃
【发布时间】: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


【解决方案1】:

正如 maddy 所说,您需要子视图控制器来添加工作子视图。

看这里:Creating Custom Container View Controllers

但与他们合作并不容易。问问自己是否真的需要实施它。在大多数情况下,您不需要它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-25
    • 2011-10-04
    • 1970-01-01
    • 2014-01-20
    • 2012-07-20
    • 2013-11-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多