ios - 从另一个类添加 View 作为 presentViewController 导致 UIEvents 不工作

标签 ios objective-c uiview uiviewcontroller

我正在尝试使用有效的 UIElement 构建一个简单的库。我想要做的是,从一个类实例创建 UIViewController 对象,并使用 presentViewController 方法将新的 ViewController 推送到当前 VC 堆栈。

我可以看到 UIElements 已成功添加到堆栈中,但 GestureRecognizer 和 UIButton 的目标不起作用。当我检查 ViewDebug 时,这些设置是 <NSNull null> .

这是我正在创建 UI 并放置在当前 View 堆栈上的类方法。

-(void)displayAd{

    dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){
        //Background Thread
        NSData * imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:fullpageCampaign.mainImage]];

        dispatch_async(dispatch_get_main_queue(), ^(void){
            //Run UI Updates
            fullPageView = [[UIViewController alloc] init];
            fullPageView.view.frame = CurrentVC.view.bounds;
            fullPageView.view.backgroundColor = [UIColor redColor];
            UIImageView *staticImageView = [[UIImageView alloc] init];
            staticImageView.frame = CurrentVC.view.frame;

            UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapDetected)];
            singleTap.numberOfTapsRequired = 1;
            [staticImageView addGestureRecognizer:singleTap];
            staticImageView.userInteractionEnabled = YES;
            [fullPageView.view addSubview:staticImageView];

            staticImageView.image = [UIImage imageWithData:imageData];
            [CurrentVC.view addSubview:fullPageView.view];
            //[fullPageView didMoveToParentViewController:self];
            [CurrentVC presentViewController:fullPageView animated:YES completion:^{
                NSLog(@"Tagon Ads is about to showing.");
                UIButton *closeButton = [self createButtonWithAssetName:@"tagonAssets.bundle/close_button" TargetMethod:@"closeModal" andView:staticImageView];
                [staticImageView addSubview:closeButton];
                [CurrentVC.view bringSubviewToFront:closeButton];
            }];
        });
    });
}

CurrentVC 是我通过库的方法作为参数发送的当前 viewController,以便在其上添加新的 viewController 堆栈。

最佳答案

closeModal Action 在哪里?可能与您的 currentVC 属于同一类。如果是这样,您的 closeButton 引用了 currentVC 但您已经从那里转到了 fullPageView。所以,你的按钮失去了他的引用。

只需创建新的 Controller ,将 imageData 发送到那里,创建自定义初始化程序,在那里创建新的 UIImageViewUIButton。通过这种方式,您的按钮将引用自己的根,您的问题应该得到解决。

关于ios - 从另一个类添加 View 作为 presentViewController 导致 UIEvents 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44896265/

相关文章:

ios - GPUImage + Touch 模糊效果

iphone - 列出目录下的文件,不包括隐藏文件,并且不遍历子文件夹

ios - Objective-c 中的条件注释标签

ios - SwiftUI - 表单中的 NavigationLink 单元格在详细信息弹出后保持突出显示

ios - 使用默认值初始化 ViewModel

c++ - UIView animateWithDuration :animations:completion: blocks don't comile in an . mm 文件

iPhone 接口(interface)尺寸

ios - 在 tableHeaderView 中使用自动布局

ios - 在 viewDidLayoutSubviews 中以编程方式创建 UIView

ios - UINavigationController subview 不滚动