xcode IOS按钮按下事件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16633502/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 03:24:06  来源:igfitidea点击:

IOS button pressed event?

iphoneiosxcode

提问by jycr753

I'm would like to know what it is the difference between:

我想知道它之间的区别是什么:

- (IBAction)operationPressed:(UIButton *)sender {

}

and:

和:

- (IBAction)operationPressed:(id)sender {


}

I can see the xcode try to put more extra help with the auto complete when using the id. so which one is the correctto use and why?

我可以看到 xcode 尝试在使用 id 时为自动完成提供更多额外帮助。那么哪个是正确使用的,为什么?

thanks

谢谢

采纳答案by dasblinkenlight

Technically, it does not matter: the declared type of UIButton*does not guarantee that calls with objects of other types are impossible. The fitst style lets you access properties of UIButtonwith the "dot syntax", while the second style lets you reuse the handler for other UI objects without making your readers wonder what is going on.

从技术上讲,这无关紧要:声明的类型UIButton*并不能保证不可能使用其他类型的对象进行调用。最合适的样式允许您使用UIButton“点语法”访问 的属性,而第二个样式允许您将处理程序重用于其他 UI 对象,而不会让您的读者想知道发生了什么。

For example, if you know that your event handler is used only with buttons, you can declare the type of sender as UIButton, and then do this:

例如,如果您知道您的事件处理程序仅用于按钮,您可以将发送者的类型声明为UIButton,然后执行以下操作:

- (IBAction)operationPressed:(UIButton *)sender {
    sender.adjustsImageWhenHighlighted = YES;
}

With the second declaration you would have to write this:

对于第二个声明,您必须这样写:

- (IBAction)operationPressed:(id)sender {
    [sender setAdjustsImageWhenHighlighted:YES];
}

On the other hand, if you plan to reuse the handler for different UI objects, the second approach is preferable.

另一方面,如果您计划为不同的 UI 对象重用处理程序,则第二种方法更可取。

回答by Maarten

When you are implementing this method with a specific class (i.e. UIButton), Xcode will give you more help. For example, it will autocomplete more stuff and will give you errors if you are sending messages to the button which it can't understand. If the compiler only knew that the sender was of type idit wouldn't be able to help you.

当您使用特定类(即UIButton)实现此方法时,Xcode 将为您提供更多帮助。例如,它会自动完成更多内容,并且如果您向其无法理解的按钮发送消息,则会出现错误。如果编译器只知道发件人的类型id,它将无法帮助您。

On the other hand, if you have several buttons and some of them are a subclass of UIControl, but not of UIButtonand you all want them to trigger operationPressed, then you might want to set the sender to type to UIControlor id.

另一方面,如果您有多个按钮,其中一些是 的子类UIControl,但不是 的子类,UIButton并且您都希望它们触发operationPressed,那么您可能希望将发送者设置为键入UIControlid

So basically, in cases where you know what the type of the sender will be, life is easier if you tell Xcode what the type of the sender will be. In cases where you don't know, use id.

所以基本上,如果你知道发件人的类型,如果你告诉 Xcode 发件人的类型,生活会更容易。如果您不知道,请使用id.

回答by Tim

idis a datatype that will hold all other data types, which is useful if you want to store something you dont know the type of at runtime.

id是一种将保存所有其他数据类型的数据类型,如果您想存储在运行时不知道类型的内容,这将非常有用。

In cases where you do know the type (like this one) it's better to use that type over idbecause it uses less bytes to store it.

如果您确实知道类型(如这个),最好使用该类型,id因为它使用较少的字节来存储它。