代码之家  ›  专栏  ›  技术社区  ›  sk123

将函数从一个ViewController调用到另一个ViewController

  •  -1
  • sk123  · 技术社区  · 6 年前

    我有两个函数,我想在另一个函数中调用 ViewController .我不想在另一个 Viewcontroller .我已尝试创建 视图控制器 对象并调用这两个方法,但不断出现崩溃。我还尝试扩展 UIViewController 但运气不好。我怎么能继续

    import UIKit
    import WebKit
    
    class PrivacyPolicyController: UIViewController, WKUIDelegate {
    
        var webView: WKWebView!
    
        override func loadView() {
            super.loadView()
            createWebView()
        }
    
        override func viewDidLoad() {
            super.viewDidLoad()
            loadURL(url: "https://www.apple.com")
        }
    
        func loadWebView() {
            let webConfiguration = WKWebViewConfiguration()
            webView = WKWebView(frame: .zero, configuration: webConfiguration)
            webView.uiDelegate = self
            view = webView
    
        }
    
        func loadURL(url: String) {
            guard let myURL = URL(string: url) else { return }
            let myRequest = URLRequest(url: myURL)
            webView.load(myRequest)
        }
    }
    

    我想打电话 loadURL loadWebView 在这方面的作用 视图控制器

    导入UIKit 导入WebKit

    class TermsAndConditionsController: UIViewController{
    
        override func loadView() {
            super.loadView()
        }
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
        }
    }
    

    这个 视图控制器 执行操作的位置

    import UIKit
    
    class WelcomeViewController: UIViewController {
    
        @IBAction func termsAndConditionsTapped(_ sender: UIButton) {
            let termsAndConditions = TermsAndConditionsController()
            self.navigationController?.pushViewController(termsAndConditions, animated: true)
        }
    
        @IBAction func privacyPolicyTapped(_ sender: UIButton) {
            let privacyPolicy = PrivacyPolicyController()
            self.navigationController?.pushViewController(privacyPolicy, animated: true)
        }
    
    }
    
    2 回复  |  直到 6 年前
        1
  •  1
  •   rodskagg    6 年前

    正如我在评论中所写的,对隐私策略和T&使用相同的viewcontroller;C:

    import UIKit
    import WebKit
    
    class MyWebViewController: UIViewController {
    
        var webView: WKWebView!
        override func viewDidLoad() {
            super.viewDidLoad()
        }
    
        func loadWebView() {
            let webConfiguration = WKWebViewConfiguration()
            self.webView = WKWebView(frame: .zero, configuration: webConfiguration)
            self.view.addSubview(self.webView)
            webView.translatesAutoresizingMaskIntoConstraints = false
            webView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor).isActive = true
            webView.topAnchor.constraint(equalTo: self.view.topAnchor).isActive = true
            webView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor).isActive = true
            webView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true
        }
    
        func loadURL(url: String) {
            guard let myURL = URL(string: url) else { return }
            let myRequest = URLRequest(url: myURL)
            if (self.webView == nil) {
                self.loadWebView()
            }
            self.webView.load(myRequest)
        }
    
    }
    

    然后:

    import UIKit
    
    class MakeItHappenViewController: UIViewController {
    
        @IBAction func termsAndConditionsTapped(_ sender: UIButton) {
            let termsAndConditions = MyWebViewController()
            self.navigationController?.pushViewController(termsAndConditions, animated: true)
            termsAndConditions.loadURL("http://someurl.com")
        }
    
        @IBAction func privacyPolicyTapped(_ sender: UIButton) {
            let privacyPolicy = MyWebViewController()
            self.navigationController?.pushViewController(privacyPolicy, animated: true)
            privacyPolicy.loadURL("http://someotherurl.com")
        }
    
    }
    
        2
  •  0
  •   Kunal_D    6 年前

    可以为此创建基类(继承UIViewController),并使两个视图控制器都继承基类。这样,您就可以在两个类中都有这两个方法,并且只需调用它们

        Class BaseClass: UIViewController{
     var webView: WKWebView
     func loadWebView() {
          let webConfiguration = WKWebViewConfiguration()
          webView = WKWebView(frame: .zero, configuration: webConfiguration)
         webView.uiDelegate = self
    }
    
    func loadURL(url: String) {
        guard let myURL = URL(string: url) else { return }
        let myRequest = URLRequest(url: myURL)
        webView.load(myRequest)
    }}
    

    基类现在拥有为web视图创建、添加和重新加载url所需的所有方法。现在是视图控制器类。

    Class PrivacyPolicyController: BaseClass, WKUIDelegate {func methodWhereYouwantToCallWebviewMthods(){
     self.loadWebView()
     self.loadURL()
     self.view = self.webView}}
    

    其他班级也一样

    class TermsAndConditionsController: BaseClass{func methodWhereYouwantToCallWebviewMthods(){
     self.loadWebView()
     self.loadURL()
     self.view = self.webView}
    override func loadView() {
        super.loadView()
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
    }}