【问题标题】:Modify View Controller Property from Delegate Method从委托方法修改视图控制器属性
【发布时间】:2019-06-10 20:12:05
【问题描述】:

我正在使用 Swift 4.2 和 Xcode 10.2。

我创建了一个自定义警报,它是一个名为 DialAlert 的 UIViewController。它覆盖在当前UIViewController 之上,并有两个按钮,Call 和Cancel。

当Call 被点击时,我使用名为callTapped. 的委托方法将点击报告给底层视图控制器,该方法使用各种其他方法执行调用设置。在执行呼叫设置时,我想用状态更新DialAlert viewcontroller。但是我无法从委托方法callTapped 访问它的属性,因为它是在 tableView 方法中创建的,如下所示:

let dialAlert = self.storyboard?.instantiateViewController(withIdentifier: Constants.Storyboard.dialAlert) as! DialAlert

我在这个 tableView 方法中设置了它的所有初始属性。

我在课程开始时尝试了这段代码:

var dialAlert = UIViewController() as! DialAlert

然后在整个代码中引用 self.dialAlert(它确实编译了),但在运行时我收到一个错误,我无法将 UIViewController 类型的值转换为 DialAlert。

如何在创建实例的方法之外的方法中修改dialAlert 的属性?我在 SO 中找到了一些看起来很有希望的答案,但最近没有,也没有任何意义。

【问题讨论】:

    标签: swift delegates viewcontroller subview


    【解决方案1】:

    我有你的问题的解决方案,我不知道你如何编写代码来显示你的DialAlert,但按照我的说法,你可以理解如何实现它。

    所以,首先在 AppDelegate.swift 方法中添加以下代码。

    class AppDelegate: UIResponder, UIApplicationDelegate {
    
        static var shared  = UIApplication.shared.delegate as! AppDelegate
        var window: UIWindow?
    
        func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
            return true
        }
    
        .....
        .....
    }
    

    此后,在您的对话框警报中声明协议并创建委托,除了检查委托并在单击按钮时调用委托方法。

    DialAlertVC.swift

    protocol DialAlertVCDelegate: class {
        func callButtonTapped(_ sender: UIButton)
        func cancelButtonTapped(_ sender: UIButton)
    }
    
    
    class DialAlertVC: UIViewController {
    
        @IBOutlet weak var dialAlertView: UIView!
    
        weak var delegate   : DialAlertVCDelegate?
    
        class func viewController() -> DialAlertVC {
            return UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "DialAlertVC") as! DialAlertVC
        }
    
        @IBAction func btnCallTapped(_ sender: UIButton) {
    
            if let selfDelegate = self.delegate {
                selfDelegate.callButtonTapped(sender)
            }
        }
    
        @IBAction func btnCancelTapped(_ sender: UIButton) {
    
            if let selfDelegate = self.delegate {
                selfDelegate.cancelButtonTapped(sender)
            }
        }
    
        override func viewDidLoad() {
            super.viewDidLoad()
        }
    }
    

    现在是显示/隐藏DialoAlertVC 的时候了,所以在ViewController 中添加以下代码。

    ViewController.swift

    class ViewController: UIViewController {
    
        @IBOutlet weak var lblButtonTapped: UILabel!
    
        var dialAlertVC         : DialAlertVC?
    
        @IBAction func btnShowDialAlert(_ sender: UIButton) {
            self.showDialAlert(true)
        }
    
        func showDialAlert(_ show: Bool) {
    
            if show {
    
                if self.dialAlertVC != nil {
                    self.dialAlertVC?.view.removeFromSuperview()
                    self.dialAlertVC = nil
                }
    
                dialAlertVC = DialAlertVC.viewController()
                dialAlertVC?.delegate = self
                AppDelegate.shared.window?.addSubview(dialAlertVC!.view)
    
            } else {
    
                if self.dialAlertVC != nil {
                    self.dialAlertVC?.view.removeFromSuperview()
                    self.dialAlertVC = nil
                }
            }
        }
    
        override func viewDidLoad() {
            super.viewDidLoad()
        }
    }
    
    extension ViewController: DialAlertVCDelegate {
    
        func callButtonTapped(_ sender: UIButton) {
            self.showDialAlert(false)
            self.lblButtonTapped.text = "Call Button Tapped"
        }
    
        //------------------------------------------------------------------------------
    
        func cancelButtonTapped(_ sender: UIButton) {
            self.showDialAlert(false)
            self.lblButtonTapped.text = "Cancel Button Tapped"
        }
    }
    

    在此处查看演示项目:https://gofile.io/?c=P2VKCl

    希望对你有帮助。

    【讨论】:

    • 这看起来是个好方法。我会尝试并回帖。谢谢!
    猜你喜欢
    • 2011-06-26
    • 2021-03-12
    • 2012-04-02
    • 1970-01-01
    • 1970-01-01
    • 2015-08-22
    • 2011-12-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多