【问题标题】:Sending 'void' to parameter of incompatible type 'UIViewController'将“void”发送到不兼容类型“UIViewController”的参数
【发布时间】:2014-11-19 08:48:51
【问题描述】:

我用 .xib 创建了一个模态 UIView,当我点击按钮时,我想显示这个 UIView。但是我在 presentViewController 中有一个错误:将“void”发送到不兼容类型“UIViewController”的参数。感谢您的回答。

- (void)modal {
    modalViewController *mvc = [[modalViewController alloc] initWithNibName:@"modalViewController" bundle:nil];
    UIView * modal = [[UIView alloc] initWithFrame:CGRectMake(self.view.center.x/2, self.view.center.y/2, 240, 320)];
    modal.backgroundColor = [UIColor whiteColor];
    [modal addSubview:mvc.view];
    [self.view addSubview:modal];
}

- (IBAction)showBTN:(id)sender {

    [self presentViewController:[self modal] animated:YES completion:nil];

}

【问题讨论】:

    标签: objective-c ios7 uiview uiviewcontroller modalviewcontroller


    【解决方案1】:

    presentViewController:animated:completion: 第一个参数是UIViewController 的实例,而不是void

    您尝试做的事情没有意义,因为您已经将 modalViewController 中的视图添加到当前 UIViewControllers 视图中。所以不用打presentViewController:animated:completion:

    这会将视图添加到您当前的视图层次结构中:

    - (IBAction)showBTN:(id)sender {
        [self modal];
    }
    

    还有一点需要注意的是,类应该以大写字母开头,所以你的modalViewController 类实际上应该命名为ModalViewController

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-07
      相关资源
      最近更新 更多