简体   繁体   English

UIView使用代理呈现UIViewController

[英]UIView presenting UIViewController with delegate

I have a UIViewController that pops up a custom UIView that needs to be able to present another UIViewController ( BarcodeScanViewController ). 我有一个UIViewController ,它弹出一个自定义UIView ,它需要能够显示另一个UIViewControllerBarcodeScanViewController )。

When the user is done with the BarcodeScanViewController , data is passed back to the UIView to update a label. 当用户完成BarcodeScanViewController ,数据将传递回UIView以更新标签。

How can I present the BarcodeScanViewController from the UIView with the a navigation bar so I close it if necessary? 如何通过导航栏显示UIViewBarcodeScanViewController ,以便在必要时将其关闭?

The below code kinda works. 下面的代码还可以。 It present the BarcodeScanViewController , but it doesn't actually do anything. 它显示BarcodeScanViewController ,但实际上不执行任何操作。 Its just a black view. 它只是一个黑视图。

Inside Custom UIView 内部自定义UIView

- (void) startScan {

    BarcodeScanViewController * bsvc = [[BarcodeScanViewController alloc] init];
    bsvc.delegate = self;

    UIViewController *currentTopVC = [self currentTopViewController];
    [currentTopVC presentViewController:bsvc animated:YES completion:nil];

}

I also receive an warning 我也收到警告

Assigning to 分配给

'id < BarcodeScanViewControllerDelegate > ' from incompatible type 'CustomView * const__strong' 来自不兼容类型'CustomView * const__strong'的'id <BarcodeScanViewControllerDelegate>'

when I try to assign the delegate. 当我尝试分配代表时。

I have never called UIViewController from a UIView before. 我以前从未从UIView调用过UIViewController。 I know that only a VC can call another VC. 我知道只有一个VC可以调用另一个VC。 So I tried to create VC to use a presenter. 因此,我尝试创建VC以使用演示者。

What am I doing wrong? 我究竟做错了什么?

Okay, there were a bunch of smaller things. 好的,有一堆小东西。 I forked your repo and fixed it so that you can see how it works. 我分叉了您的存储库,并对其进行了修复,以便您可以了解其工作原理。 See here . 这里

In short: You did present the scanner VC, but as I said you can't simply put it "over" the presenting view controller (your ViewController class). 简而言之:您确实介绍了扫描仪VC,但是正如我说的,您不能简单地将其放在介绍的视图控制器(您的ViewController类)上。 As soon as any presentation transitions (ie animations) are done, the view of that ViewController is removed from the view hierarchy. 一旦完成任何演示转换(即动画),就会从视图层次结构中删除该ViewController的视图。 So what you saw as a black screen was just the window background (which is black). 因此,您看到的黑屏只是窗口背景(黑色)。 The scanner's view was transparent. 扫描仪的视图是透明的。 Furthermore, the scanner expected you to provide a view to render the camera into (and buttons) via an IBOutlet. 此外,扫描仪希望您提供一个视图,以通过IBOutlet将摄像机渲染为(和按钮)。 Since you didn't instantiate the controller from a storyboard, you didn't set any, ie the view used for that was nil and thus the frame the scanner wanted to render the video in had a size of (0, 0). 由于您没有从情节提要中实例化控制器,因此未设置任何内容,即用于该视图的视图为nil,因此扫描仪要渲染视频的帧的大小为(0,0)。 I fixed that for now, hopefully you can now see how presenting a viewcontroller is supposed to work. 我暂时修复了该问题,希望您现在可以看到呈现视图控制器的工作方式。 To achieve an actual popup (with "underlying views of previous view controllers") is a bit harder on iOS, but possible in several ways. 在iOS上,要实现一个实际的弹出窗口(带有“先前视图控制器的基础视图”)会有些困难,但是可以通过多种方式实现。 I can elaborate further later if you want. 如果您愿意,我可以在后面详细说明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM