加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

4关于Swift中的选择器的问题

发布时间:2020-12-14 05:52:09 所属栏目:百科 来源:网络整理
导读:抱歉这些问题 我有4个关于Selector的问题。 第一个问题 我想知道在swift中使用选择器的正确方法是什么 closeBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Stop,target: self,action: Selector("closeBarButtonItemClicked:"
抱歉这些问题

我有4个关于Selector的问题。

第一个问题

我想知道在swift中使用选择器的正确方法是什么

closeBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Stop,target: self,action: Selector("closeBarButtonItemClicked:"));

VS

closeBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Stop,action: "closeBarButtonItemClicked:");

我们应该立即使用Selector(“methodName:”)还是“methodName:”?

两种方式都是正确的方法?

第二个问题

我们如何使用Swift中的参数调用函数?假设我想调用这样的函数

func methodName(parameterOne : String,parameterTwo: String)

第三个问题

我们如何使用Swift中的Selector调用类型方法?甚至有可能吗?

class SomeClass {
class func someTypeMethod() {
// type method implementation goes here
  }
}

第四个问题

Selector中函数名后面的冒号的目的是什么?

问题1:
我不认为真的是一个正确的方法。我个人更喜欢第二种方式,但两者都工作,所以我不认为这很重要。

问题2:
我只是重读你的问题。我想你的意思是如何在选择器中调用它。我相信的选择器是“methodName:parameterTwo:”,但我不是积极的,因为具有两个参数的选择器可能应该有一个外部参数名称放置在我的答案中参数parameter2的选择器中。

老问题2回答(编辑前):
您可以将该函数称为methodName(variable1,parameterTwo:variable2)。如果你想让他们在调用中说出参数名称,你可以使用header methodName(nameVarName parameterOne:String,calledVarName2 parameterTwo:String)。这将被称为methodName(称为VARName:variable1,calledVarName2:variable2)。您也可以将标题定义为methodName(#parameterOne:String,#parameterTwo:String)。这将被称为methodName(parameterOne:variable1,parameterTwo:variable2)。在这里阅读更多:https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Functions.html

问题3:
我不能肯定地说,但我不认为有办法为此选择器。如果我假设它将是“someTypeMethod”

老问题3回答(编辑前):
您可以通过SomeClass.someTypeMethod()调用此方法。

问题4:冒号表示函数头具有参数。所以“function1”对应于func function1(someParameterName:AnyObjectHere),而“function1”对应于func function1()。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读