来自类的多重继承' UIViewController'和' UISearchBar'

时间:2017-10-21 04:37:46

标签: ios swift uiviewcontroller uisearchbar

我正在努力创建一个允许某人在搜索栏中输入文字的应用,该应用会自动打开" https://www.example.com/(来自搜索栏的文字)。

我不断收到Failed Builds和来自类UIViewControllerUISearchBar的多重继承的错误。

非常感谢任何帮助。

以下代码:

import UIKit

class ViewController: UIViewController, UISearchBar {
    override func viewDidLoad() {
        super.viewDidLoad()

        func searchBarSearchButtonClicked(_ searchBar: UISearchBar)
        {
            searchBar.resignFirstResponder()
            if let url = URL ("https://www.doughnationgifts.com/",doughnationcode)
            let doughnationcode = String(searchBar.text!)
            if #available(iOS 10.0, *)
            {
                UIApplication.shared.open(URL, options: [:], completionHandler: nil)
            } else {
                UIApplication.shared.openURL(URL)
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

您不需要UISearchBar继承,这已经从UIViewController延伸。您可能希望添加UISearchBarDelegate。但是,您还需要在searchBar添加插座才能设置代理。

此外,您无法在viewDidLoad内添加功能。

请改为:

import UIKit

class ViewController: UIViewController, UISearchBarDelegate {
    @IBOutlet weak var searchBar: UISearchBar!

    override func viewDidLoad() {
        super.viewDidLoad()
        searchBar.delegate = self
    }

    func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
        searchBar.resignFirstResponder()
        if let url = URL ("https://www.doughnationgifts.com/",doughnationcode)
        let doughnationcode = String(searchBar.text!)
        if #available(iOS 10.0, *)
        {
            UIApplication.shared.open(URL, options: [:], completionHandler: nil)
        } else {
            UIApplication.shared.openURL(URL)
        }
    }
}