简体   繁体   English

如何使用子视图卸载 uiView

[英]How to unload uiView with subview

I am having tough time figuring out how to unload my UIView.我很难弄清楚如何卸载我的 UIView。

So Here is what I am doing.所以这就是我在做什么。

I have a UI View class我有一个 UI 视图类

@protocol JitsiViewDelegate <NSObject>

@optional
- (void)meetingEnded;
@end

@interface JitsiView : UIView

@property (nonatomic, nullable, weak) id<JitsiViewDelegate> delegate;
- (void)join:(JitsiMeetingConfig*)config;
@end

Where join (.m) file looks like this加入 (.m) 文件的位置如下所示

- (void) join:(JitsiMeetingConfig*)config
{
    RCTBridge *bridge = [[Jitsi sharedInstance] getReactBridge];
  rootView = [[RNRootView alloc] initWithBridge:bridge
                                moduleName: @"JitsiTest"
                              initialProperties: [self getInitialProperties:config]];
  rootView.backgroundColor = self.backgroundColor;
  // Add rootView as a subview which completely covers this one.
          [rootView setFrame:[self bounds]];
        rootView.autoresizingMask
            = UIViewAutoresizingFlexibleWidth
                | UIViewAutoresizingFlexibleHeight;
    [self addSubview:rootView];
}

In above在上面

RNRootView is this RNRootView 是这个

and RCTBridge would be this而RCTBridge就是这个

I am using this in a swift project我在一个 swift 项目中使用它

import UIKit
import JitsiSdk;

class ViewController: UIViewController {
    @IBOutlet weak var videoButton: UIButton?
    fileprivate var JitsiView: jitsiView?
    fileprivate var config: JitsiMeetingConfig?
    
    override func viewDidLoad() {
        super.viewDidLoad()

       //Config (removed code intentionally as it wasn't realvent) 

    }
    
    @IBAction func joinMeeting(_ sender: Any) {
                let jitsiView = JitsiView(frame: CGRect(x: 0, y: 0, width: self.view.bounds.size.width, height:self.view.bounds.size.height ))
                .delegate = self
                self.jitsiView = jitsiView;
                self.view.addSubview(jitsiView)
                jitsiView.join(config);
    }
    
    fileprivate func onMeetingEnded() {
            if(jitsiView != nil) {
                jitsiView?.removeFromSuperview()
            }
        }
}


extension ViewController: JitsiViewDelegate {
    func meetingEnded() {
        onMeetingEnded()
    }
}

Here on meetingEnded, I want to remove JitsiView and show the content which were there in my view controller before loading JitsiMeetView but the current code is throwing following error在会议结束时,我想删除JitsiView并在加载JitsiMeetView之前显示视图控制器中的内容,但当前代码抛出以下错误

'NSInternalInconsistencyException', reason: 'Modifications to the layout engine must not be performed from a background thread after it has been accessed from the main thread.' 'NSInternalInconsistencyException',原因:'从主线程访问布局引擎后,不得从后台线程对其进行修改。'

Can someone help me in understanding that why I am getting this error and how I can fix it?有人可以帮助我理解为什么我会收到此错误以及如何解决它?

Modifications to the layout engine must not be performed from a background thread after it has been accessed from the main thread.从主线程访问布局引擎后,不得从后台线程执行对布局引擎的修改。

This means that you are trying to call onMeetingEnded() on a background thread.这意味着您正在尝试在后台线程上调用onMeetingEnded() All UI work must happen on main/UI thread.所有 UI 工作都必须在主/UI 线程上进行。

Here's what you can do -这是你可以做的 -

func meetingEnded() {
    DispatchQueue.main.async { [weak self] in
        self?.onMeetingEnded()
    }
}

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

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