我正在开发一个应用程序,允许用户在搜索栏中输入文本,并且该应用程序将自动打开“https://www.example.com/(来自搜索栏的文本)。
我不断收到失败的构建以及有关类 UIViewController
和 UISearchBar
的多重继承的错误。
任何帮助将不胜感激。
代码如下:
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)
}
}
}
}
最佳答案
您不需要 UISearchBar
继承,它已经从 UIViewController
继承。您可能需要添加 UISearchBarDelegate
。但是,您还需要向 searchBar
添加一个 socket 才能设置委托(delegate)。
此外,您无法在 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)
}
}
}
关于ios - 来自类 'UIViewController' 和 'UISearchBar' 的多重继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46860093/