xcode 铸造发件人参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1089752/
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
Casting sender parameter
提问by Bramble
I have a method that accepts a sender and is called with a UIbutton. How do I get that object to casted into a UIImageView? Basically how do I re-use the code for the button.
我有一个接受发件人并使用 UIbutton 调用的方法。如何将该对象转换为 UIImageView?基本上我如何重用按钮的代码。
- (IBAction)startClick:(id)sender {
button.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:@"Pic_1.png"],
[UIImage imageNamed:@"Pic_2.png"],
[UIImage imageNamed:@"Pic_3.png"],
nil];
[button setAnimationRepeatCount:1];
button.animationDuration = 1;
[button startAnimating];
}
回答by Quinn Taylor
My first response is that it's probably a mistake to do so. UIButton
and UIImageView
are distinct subclasses of UIView
(and UIButton
is a subclass of UIControl
) so the only things they have in common are UIView
methods. If sender
is really a UIButton
, treating it as a UIImageView
is likely to result in errors, such as "unrecognized selector sent to instance" and the like.
我的第一反应是,这样做可能是错误的。UIButton
andUIImageView
是不同的子类UIView
(并且UIButton
是 的子类UIControl
),所以它们唯一的共同点是UIView
方法。如果sender
真的是a UIButton
,把它当成aUIImageView
很可能会导致错误,比如“unrecognized selector sent to instance”之类的。
In general, a cleaner solution is to create a separate method that is the action for a UIImageView
. (Unlike Java, Objective-C and Cocoa prefer to not shoehorn UI response code into a single method based on the event type — rather, you organize the code logically based on the operation that needs to happen.)
通常,更简洁的解决方案是创建一个单独的方法,作为UIImageView
. (与 Java 不同,Objective-C 和 Cocoa 不喜欢根据事件类型将 UI 响应代码硬塞到单个方法中——而是根据需要发生的操作逻辑地组织代码。)
If you must call this method from both buttons and image views, you can separate the logic like this:
如果你必须同时从按钮和图像视图调用这个方法,你可以像这样分离逻辑:
- (IBAction) startClick:(id)sender {
if ([sender isKindOfClass:UIButton]) {
...
}
else if ([sender isKindOfClass:UIImageView]) {
...
}
}
回答by John Ballinger
I seem to think you would have done this.
我似乎认为你会这样做。
UIButton *button = (UIButton*) id;
// then do all you button code here.
This should work. Otherwise, it might be better to change the function to
这应该有效。否则,最好将函数更改为
(IBAction)startClick:(UIButton *)theButton
This way you want have to cast the button.
这样你就必须投射按钮。
回答by Jim Correia
Do you have an outlet for your button? Is the button the only UI element which will send -startClick:? If so, then write the action method like this:
你的按钮有插座吗?按钮是唯一会发送 -startClick: 的 UI 元素吗?如果是这样,那么编写这样的操作方法:
- (IBAction)startClick:(id)sender {
NSParameterAssert(sender == self.startClickButton);
if (sender == self.startClickButton) {
UIButton *button = self.startClickButton;
[...]
}
}
If you don't have an outlet/property for the button, you can test the sender's class, and cast if appropriate.
如果您没有按钮的插座/属性,您可以测试发送者的类,并在适当时进行转换。
- (IBAction)startClick:(id)sender {
NSParameterAssert([sender isKindOfClass: [UIButton class]]);
if ([sender isKindOfClass: [UIButton class]]) {
UIButton *button = (UIButton *)sender;
[...]
}
}
Finally, it is permissible (at compile time) to send any message to an id typed value. (You'll get an exception at runtime if the receiver doesn't respond to that message.) You cannot, however, use dot syntax.
最后,允许(在编译时)向 id 类型的值发送任何消息。(如果接收方不响应该消息,您将在运行时收到异常。)但是,您不能使用点语法。
So if you know that the sender is always a button, you can write your method without casting at all. You'll have to use messages send syntax instead of dot syntax.
因此,如果您知道发送方始终是一个按钮,您就可以编写您的方法,而无需进行任何强制转换。您必须使用消息发送语法而不是点语法。
[button setAnimationImages: [NSArray ...]];
[button setAnimationDuration: 1];
This is important to understand - dot syntax is equivalent to message send syntax - it isn't mystical.
理解这一点很重要 - 点语法等同于消息发送语法 - 它并不神秘。
回答by Suresh
You can just use the following to get the button label and determine which one was clicked:
您可以仅使用以下内容来获取按钮标签并确定单击了哪个:
NSLog(@"Clicked button: %@",[[sender titleLabel] text]);
NSLog(@"点击按钮:%@",[[发件人titleLabel] text]);
I hope this helps.
我希望这有帮助。
Suresh
苏雷什