首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

每次在UIImagePickerController()的dismiss()之后调用viewDidLoad()

在每次调用UIImagePickerController()的dismiss()之后调用viewDidLoad()是不合适的做法。viewDidLoad()是UIViewController生命周期中的一个方法,它在视图控制器的视图加载完成后调用,通常只会在视图控制器初始化时调用一次。而dismiss()是用于关闭模态视图控制器的方法,当调用dismiss()后,模态视图控制器会被移除,返回到上一个视图控制器。

如果需要在dismiss()之后执行一些操作,可以考虑使用代理模式或闭包回调来实现。具体的实现方式如下:

  1. 使用代理模式: 在模态视图控制器的定义中,声明一个代理协议,并定义一个代理属性。在dismiss()之后,通过代理方法通知上一个视图控制器执行相应的操作。
代码语言:txt
复制
// 在模态视图控制器中定义代理协议
protocol ImagePickerDelegate: class {
    func imagePickerDidDismiss()
}

class ImagePickerViewController: UIViewController {
    weak var delegate: ImagePickerDelegate?

    // 在dismiss()之后调用代理方法
    func dismissImagePicker() {
        dismiss(animated: true) {
            self.delegate?.imagePickerDidDismiss()
        }
    }
}

class MainViewController: UIViewController, ImagePickerDelegate {
    // 在MainViewController中实现代理方法
    func imagePickerDidDismiss() {
        // 在dismiss()之后执行相应操作
        viewDidLoad()
    }

    // 弹出模态视图控制器
    func presentImagePicker() {
        let imagePicker = ImagePickerViewController()
        imagePicker.delegate = self
        present(imagePicker, animated: true, completion: nil)
    }
}
  1. 使用闭包回调: 在模态视图控制器的定义中,声明一个闭包属性。在dismiss()之后,通过闭包回调执行相应的操作。
代码语言:txt
复制
class ImagePickerViewController: UIViewController {
    var dismissCompletion: (() -> Void)?

    // 在dismiss()之后调用闭包回调
    func dismissImagePicker() {
        dismiss(animated: true) {
            self.dismissCompletion?()
        }
    }
}

class MainViewController: UIViewController {
    // 弹出模态视图控制器
    func presentImagePicker() {
        let imagePicker = ImagePickerViewController()
        imagePicker.dismissCompletion = {
            // 在dismiss()之后执行相应操作
            self.viewDidLoad()
        }
        present(imagePicker, animated: true, completion: nil)
    }
}

以上是两种常见的实现方式,根据具体情况选择适合的方式来处理dismiss()之后的操作。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

  • IOS 弹出框

    2、弹出框: import UIKit class ViewController:UIViewController { var label:UILabel! override func viewDidLoad() { super.viewDidLoad() self.view.backgroundColor = UIColor.brown label = UILabel(frame:CGRect(x:40, y:100,width:240, height:44)) label.text = ”” self.view.addSubview(label) let button = UIButton(frame:CGRect(x:40, y:180,width:240, height:44)) button.setTitle(“打开新的视图控制器”, for:UIControlState()) button.backgroundColor = UIColor.black button.addTarget(self, action:#selector(ViewController.openViewController),fo:.touchUpInside) self.view.addSubview(button) } func openViewController() { let newViewController = NewViewController() newViewController.labelTxt = “传递的参数!” newViewController.viewController = self self.present(newViewController, animated:true,completion:nil) } }

    05

    IOS移动开发从入门到精通 视图UIView、层CALayer(2)

    或者修改 rootViewController参数 2、弹出框: import UIKit class ViewController:UIViewController { var label:UILabel! override func viewDidLoad() { super.viewDidLoad() self.view.backgroundColor = UIColor.brown label = UILabel(frame:CGRect(x:40, y:100,width:240, height:44)) label.text = ”” self.view.addSubview(label) let button = UIButton(frame:CGRect(x:40, y:180,width:240, height:44)) button.setTitle(“打开新的视图控制器”, for:UIControlState()) button.backgroundColor = UIColor.black button.addTarget(self, action:#selector(ViewController.openViewController),fo:.touchUpInside) self.view.addSubview(button) } func openViewController() { let newViewController = NewViewController() newViewController.labelTxt = “传递的参数!” newViewController.viewController = self self.present(newViewController, animated:true,completion:nil) } }

    01
    领券