我已经在MainViewController中创建了一个导航栏按钮

override func viewDidLoad() {
    super.viewDidLoad()    

    navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Save", style: .plain, target: self, action: #selector(presentPopup))
        
}
    
@objc func presentPopup() {
        
    let popupVC = PopupViewController()
    popupVC.modalPresentationStyle = .overCurrentContext
    present(popupVC, animated: true, completion: nil)
        
}

它显示用于保存主视图内容的弹出表单(包含"保存"按钮).我想让这个"保存"按钮(在PopupViewController):(1)将S导航栏按钮的标题改为"已保存";(2)禁用该按钮.

到目前为止,我在PopupViewController人中有以下几点:

let popupBox: UIView = {
    let view = UIView()
    view.translatesAutoresizingMaskIntoConstraints = false
    return view
}()

let button: UIButton = {
    let view = UIButton()
    view.translatesAutoresizingMaskIntoConstraints = false
    return view
}()

override func viewDidLoad() {
    super.viewDidLoad()
        
    self.definesPresentationContext = true
    view.backgroundColor = .clear
        
    view.addSubview(popupBox)
    // configure popupBox

    view.addSubview(button)
    // configure button
    button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
        
}

@objc func buttonAction(_ sender: UIButton) {
    // save main VC's contents
    // change title of main VC's nav button - WHAT GOES HERE?
    // disable main VC's nav button - WHAT GOES HERE?
    self.dismiss(animated: true)  // dismiss popup
    
}

我应该在按钮的操作中添加什么来完成我想做的事情?

推荐答案

只需将一个回调加到PopupViewController,它将在buttonAction中调用.

var onDismiss: (() -> Void)?

@objc func buttonAction(_ sender: UIButton) {
    dismiss(animated: true)
    onDismiss?()
    //or,  you can also run onDismiss after the dismiss animation:
    // dismiss(animated: true, completion: onDismiss)
}

在显示弹出窗口之前指定您想要做的事情:

@objc func presentPopup() {
    let popupVC = PopupViewController()
    popupVC.modalPresentationStyle = .overCurrentContext
    popupVC.onDismiss = { [weak self] in
        guard let self else { return }
        self.navigationItem.title = "Saved"
        self.navigationItem.rightBarButtonItem?.isEnabled = false
    }
    present(popupVC, animated: true, completion: nil)       
}

Ios相关问答推荐

[NSJSONSerializationdataWithJSONObserver:选项:错误:]:SON写入中无效的顶级类型

创建带有Flutter 翼的会所头像

如何在Android或iPhone上的原生react 中处理其他应用程序通知?

在视图上执行手势会阻止相机操作

无法下载并安装带有Xcode 15.0的iOS 17.0模拟器运行时

如何在后台显示 Swift UI 中的通讯通知?

如何擦除 Swift 中的 Core Graphics Context 绘图?

如何在应用程序中显示我的小部件的快照?

过渡动画在 iOS16 中不起作用,但在 iOS15 中起作用

渲染的海边重定向在物理 iOS 设备上不起作用

SwiftUI 中的偏移量和按钮有问题吗?

.onChange(of: ) 一次用于多个 @State 属性?

如何在不支持并发的自动关闭中修复'async'调用?

在 iPad 上删除列表的顶部和底部行

如何让我的 iOS 应用程序与 api 连接?

禁用 UITextField 的自动更正

如何在 Swift 中旋转 UIButton 和 UILabel 的文本?

在 Swift 中为 UITextField/UIView 摇动动画

Select UITableViewCell 时 UIView backgroundColor 消失

警告:iPad:Icon-72.png:图标尺寸(0 x 0)