我正在尝试使用有效的 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
发送到那里,创建自定义初始化程序,在那里创建新的 UIImageView
和 UIButton
。通过这种方式,您的按钮将引用自己的根,您的问题应该得到解决。
关于ios - 从另一个类添加 View 作为 presentViewController 导致 UIEvents 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44896265/