如何获取按下的按钮标题文本值并在按下时显示为其他按下的按钮?我使用以下代码从按钮获取标题文本
- (IBAction) checkIt:(id)sender
{
UIButton *button = (UIButton *)sender;
NSLog(@"Button text value %@", button.titleLabel.text);
}
如果我按下,如何将按钮文本显示到其他按钮中?请帮我解决这个问题
这样做,
- (IBAction) checkIt:(id)sender
{
UIButton *button = (UIButton *)sender;
[your_other_button setTitle:button.titleLabel.text forState:UIControlStateNormal];
}
你可以让它指向标签或只是制作一个可变按钮并将其添加到你的按钮。并获取 btn 文本。
您需要在您的应用程序委托中创建 Globle 变量,例如:-
yourAppdelegate.h
@property (strong, nonatomic) NSString *buttonTitleCopy;
yourAppdelegate.m
@synthesize buttonTitleCopy;
现在您只需在特定类中获取此变量,例如:-
你的类.h
#import "yourAppdelegate.h"
//create Object of your Delegate Class
yourAppdelegate *objAppdelegate;
你的班级.m
- (void)viewDidLoad
{
objAppdelegate = (REMAppDelegate *) [[UIApplication sharedApplication]delegate];
[super viewDidLoad];
}
现在在您的方法中:-
- (IBAction) checkIt:(id)sender
{
UIButton *button = (UIButton *)sender;
objAppdelegate.buttonTitleCopy=button.titleLabel.text
NSLog(@"Button text value %@", delegate.buttonTitleCopy);
}
现在您可以buttonTitleCopy
在您的项目中使用 app-delegate 类 Object 的任何变量。希望你得到这个并解决你的问题。
注意: -只需将您的 OtherButton Title 设置为
otherButton.titleLabel.text=objAppdelegate.buttonTitleCopy;
你可以试试这个方法
- (IBAction) checkIt:(id)sender
{
otherButton.titleLabel.text=sender.titleLabel.text;
}