UIAlertController没有工作Xibs?

时间:2016-04-01 11:36:49

标签: objective-c xib uialertcontroller

在我的项目中,我正在使用xib,因为我想显示UIAlertController并执行一些操作。但是我收到了错误。

这是我的代码。

enter image description here

我的代码中有什么错误。

1 个答案:

答案 0 :(得分:1)

在UIVieController中添加xibView时,在ParentViewController中指定self。

_xibView *objxibView = [[xibView alloc]init];

objxibView.parentViewController = self;

在Xib的xibView.h文件中创建1个属性

@property (weak, nonatomic) UIViewController *parentViewController;

并在xibView.m文件中使用下面的代码 对于xib中的UIAlertController

{
    UIAlertController * alert = [UIAlertController
                                 alertControllerWithTitle:nil
                                 message:nil 
                                 preferredStyle:UIAlertControllerStyleAlert];

    UIAlertAction *actionOK = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil];

    [alert addAction:actionOK];
    [self.parentViewController presentViewController:alert
                                            animated:YES
                                          completion:nil];
}