【问题标题】:why will UIViewController viewWillAppear not run after subViews are removed from superView为什么从superView中删除子视图后UIViewController viewWillAppear不会运行
【发布时间】:2017-11-03 22:35:46
【问题描述】:

我正在像这样对 UIViewController 做一个子层

- (IBAction)transactionListViewCameraBtn_Pressed:(id)sender {

    if([NWTillHelper isDebug] == 1) {
        NSLog(@"%s entered", __PRETTY_FUNCTION__);
    }

    self.capture = [[ZXCapture alloc] init];
    self.capture.camera = self.capture.back;
    self.capture.focusMode = AVCaptureFocusModeContinuousAutoFocus;

    [self.view.layer addSublayer:self.capture.layer];

    self.capture.delegate = self;

    [self applyOrientation];
}

问题是,当我删除 subLayer 时,我希望原始视图 viewWillAppear 能够运行,但事实并非如此。

我正在像这样删除子层

[self.capture.layer removeFromSuperlayer];

当原始视图再次出现时,viewWillAppear 是否不应该运行?

如果没有,我如何确保我在 viewWillAppear 中的代码在我删除 subLayer 时会运行?

【问题讨论】:

  • viewWillAppear 当您转到另一个控制器并再次返回时调用。但你只是删除了超级层,所以它不会被调用。
  • 你可以这样做......采取2个按钮开始和取消。 start 将添加图层,当单击取消时,它将删除图层。

标签: ios objective-c viewwillappear subviews


【解决方案1】:

按照建议检查Apple doc 以了解视图生命周期。 然后您可以将您的 viewWillAppear 代码放入一个新方法中:

-(void)yourFunctionName {
    //your code here 
}

在你需要的地方调用它。

但是,也许,如果您只需要更新您的布局,您可以查看 setNeedsLayoutlayoutIfNeededmethods。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-06
    相关资源
    最近更新 更多