objective-c 从 IBAction 获取按钮文本 - iPhone

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/888668/
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-03 21:34:54  来源:igfitidea点击:

Get text of button from IBAction - iPhone

iphoneobjective-ccocoainterface-builder

提问by Organiccat

When an IBAction is called:

当调用 IBAction 时:

-(IBAction) onClick1: (id) sender;

What is passed in the sender? Since it's hooked up through the IB, I'm not really sure. My question is how to get the text of the button to be the passed object (NSString most likely) so that I could call it inside the action implementation.

发送方传递了什么?因为它是通过 IB 连接的,所以我不太确定。我的问题是如何使按钮的文本成为传递的对象(最有可能是 NSString),以便我可以在操作实现中调用它。

-(IBAction) onClick1: (id) sender {
  NSLog(@"User clicked %@", sender);
  // Do something here with the variable 'sender'
}

采纳答案by Marc W

It's actually:

其实是:

-(IBAction) onClick1: (id) sender {
  NSLog(@"User clicked %@", sender);
  // Do something here with the variable 'sender'
}

senderis not a NSString, it's of type id. It's just the control that sent the event. So if your method is trigged on a button click, the UIButtonobject that was clicked will be sent. You can access all of the standard UIButtonmethods and properties programmatically.

sender不是NSString,它是类型id。它只是发送事件的控件。因此,如果您的方法是在单击按钮时触发的,UIButton则将发送被单击的对象。您可以以UIButton编程方式访问所有标准方法和属性。

回答by Matt Ball

The sendershould be the control which initiated the action. However, you should not assume its type and should instead leave it defined as an id. Instead, check for the object's class in the actual method as follows:

sender应是启动操作的控制。但是,您不应假定其类型,而应将其定义为id. 相反,在实际方法中检查对象的类,如下所示:

- (IBAction)onClick1:(id)sender {
    // Make sure it's a UIButton
    if (![sender isKindOfClass:[UIButton class]])
        return;

    NSString *title = [(UIButton *)sender currentTitle];
}

回答by GameLoading

-(IBAction)onClick:(id) sender {
     UIButton *btn = (UIButton *)sender;

    //now btn is the same object. And to get title directly
    NSLog(@"Clicked button: %@",[[btn titleLabel] text]);
}

回答by VSN

Simply write the following code

只需编写以下代码

-(IBAction) getButtonTitle:(id)sender
{
     UIButton *button = (UIButton *)sender; 
     NSString *buttonTitle = button.currentTitle;
     NSLog(@"Button Title %@",buttonTitle);

}

Thats it... you have done!!!

就是这样……你已经完成了!!!

回答by Marc Charbonneau

Sender should be defined as type id, not int or NSString. The sender is the actual object that's calling the method; if you hooked it up to a button, it will be a UIButton, if it's a text field, a UITextField. You can use this to get information from the control (for example the text field's current string value), or compare it to an IBOutlet instance variable if you have multiple controls hooked up to the same action method.

Sender 应该定义为类型 id,而不是 int 或 NSString。发送者是调用该方法的实际对象;如果你把它连接到一个按钮上,它将是一个 UIButton,如果它是一个文本字段,一个 UITextField。您可以使用它从控件中获取信息(例如文本字段的当前字符串值),或者如果您有多个控件连接到相同的操作方法,则将其与 IBOutlet 实例变量进行比较。

回答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]);

To answer your question, the id is the object from the IB.

为了回答您的问题,id 是来自 IB 的对象。

回答by Hardik Thakkar

To fetch the text from the button:

从按钮中获取文本:

 NSLog(@"Date::%@",[btn titleForState:UIControlStateNormal]);