带有默认参数的swift函数也是一个选择器?

时间:2016-04-14 06:27:14

标签: ios swift selector stackview

我希望能够从两个地方调用此功能:当我完成编辑文本字段时,我想在webView中没有时添加新的stackView,我也是希望能够使用barButtonItem来执行此操作。

我有两个问题。当条形按钮调用此函数时,参数' url'成为对象,类型为UIBarButtonItem。当它从textFieldShouldReturn调用时,它会以NSURL的形式正确显示。如果用户没有在地址字段中输入任何内容,并且点击输入,则会出现空白NSURL,并且不使用默认值。 (我喜欢它)

来自textfieldShouldReturn函数的调用应该是什么样的,以便空白会触发默认值?

我如何处理我的函数或按钮将调用该函数的事实,以及为什么我的命名参数' url'成为我认为会发送的人?'

    override func viewDidLoad() {
    super.viewDidLoad()

    setDefaultTitle()

    let add = UIBarButtonItem(barButtonSystemItem: .Add, target: self, action: #selector(ViewController.addWebView))
    let delete = UIBarButtonItem(barButtonSystemItem: .Trash, target: self, action: #selector(ViewController.deleteWebView))
    navigationItem.rightBarButtonItems = [delete, add]
}

   func addWebView(url: NSURL = NSURL(string: "https://www.google.com")!) {
    let webView = UIWebView()
    webView.delegate = self
    stackView .addArrangedSubview(webView)
    webView.loadRequest(NSURLRequest(URL: url))

    webView.layer.borderColor = UIColor.blueColor().CGColor
    selectWebView(webView)

    let recognizer = UITapGestureRecognizer(target: self, action: #selector(ViewController.webViewTapped))
    recognizer.delegate = self
    webView.addGestureRecognizer(recognizer)
}

   func textFieldShouldReturn(textField: UITextField) -> Bool {
    if let webView = activeWebView, address = addressBar.text {
        if let url = NSURL(string: address) {
            webView.loadRequest(NSURLRequest(URL: url))
        }

    } else if stackView.arrangedSubviews.count == 0 {
         let address = NSURL(string: addressBar.text!)!
        addWebView(address)
    }
    textField.resignFirstResponder()
    return true
}

1 个答案:

答案 0 :(得分:1)

你正在获得发件人对象,这实际上是UIBarButtonItem。你听说过Target-Action Cocoa模式吗?如果不是,您可以在这里阅读更多内容: https://developer.apple.com/library/ios/documentation/General/Conceptual/Devpedia-CocoaApp/TargetAction.html

特别相关的部分是"行动方法必须有某种形式"。

考虑引入addWebView重载:

func addWebView(sender: NSObject) {

    addWebView(url: NSURL(string: "https://www.google.com")!)
}

private func addWebView(url: NSURL) {
    //left as is ...
}

这是Dave评论的更新。 必须使用不同的名称作为实际的实现方法。否则,Swift编译器无法解析分配的选择器名称。

有用的代码,演示了这个问题,如下所示:

class Notifier: NSObject {

    private var _target: NSObject!
    private var _action: Selector!

    func addObserver(target: NSObject, action: Selector) {
        _target = target
        _action = action
    }

    func invokeMethod() {

        guard let t = _target else {
            print("target must be set")
            return
        }

        guard let a = _action else {
            print("action must be set")
            return
        }
        if t.respondsToSelector(a) {
            t.performSelector(a, withObject: self)
        }
    }

}

class Observer: NSObject {

    func subscribe(notifier: Notifier) {
        notifier.addObserver(self, action: #selector(Observer.callback))
    }

    func callback(sender: NSObject) {
        callbackImpl(NSURL(string: "https://www.google.com")!)
    }

    private func callbackImpl(url: NSURL) {
        print("url\(url)")
    }
}

//client's code
let n = Notifier()
let o = Observer()
o.subscribe(n)
n.invokeMethod()