如何在单击 UIButton 时将金额添加到 UITextField
How to add amount into UITextField on click of UIButton
我有一个 UITextField
和下面那个 3 按钮。用户可以在UITextField
中输入金额。当用户单击下面的按钮UITextField
时,它会将UITextField
中的金额与按钮中写入的金额相加。
第一个按钮将添加 100,第二个将添加 500,第三个将添加 1000。实现它的最佳方法是什么。
我尝试执行以下操作但得到 button.tag=0
/*Amount increment*/
- (IBAction)Btn_Incr100:(id)sender {
[self addTextFieldValue:nil];
}
- (IBAction)Btn_Incr500:(id)sender {
[self addTextFieldValue:nil];
}
- (IBAction)Btn_Incr1000:(id)sender {
[self addTextFieldValue:nil];
}
-(void)tagNumber{
Btn_Incr100.tag=1;
Btn_Incr500.tag=2;
Btn_Incr1000.tag=3;
}
-(void) addTextFieldValue:(UIButton*) button
{
int amount=[Txt_Amount.text intValue];
int AddAmount;
NSLog(@"button.tag%d",button.tag);
if (button.tag==1) {
AddAmount=100;
}
else if (button.tag==2) {
AddAmount=500;
}
else if(button.tag==3) {
AddAmount=1000;
}else{
AddAmount=0;
}
amount=amount+AddAmount;
Txt_Amount.text=[NSString stringWithFormat:@"%d",amount];
NSLog(@"%d",amount);
NSLog(@"%@",Txt_Amount.text);
}
借助于此您可以实现:
-(void)displayvalue:(id)sender {
UIButton *resultButton = (UIButton *)sender;
NSLog(@" The button's title is %@." resultButton.currentTitle);
}
现在您可以在文本字段中设置此文本。
将此方法添加为每个按钮操作。
-(void) addTextFieldValue:(UIButton*) button
{
NSString *string;
if ([button.text hasPrefix:@"+"] && [button.text length] > 1)
{
string = [string substringFromIndex:1];
}
int textFieldValue =[textField.text integerValue] + string.integerValue;
textField setText:[NSString stringWithFormat:@"%i",textFieldValue];
}
NSString *string;
if ([button.text hasPrefix:@"+"] && [button.text length] > 1)
{
string = [string substringFromIndex:1];
}
int textFieldValue =[textField.text integerValue] + string.integerValue;
textField setText:[NSString stringWithFormat:@"%i",textFieldValue];
或
连接单独的操作
-(IBAction) onClick1: (id) sender]
{
NSLog(@"User clicked %@", sender);
textField.text = [textField.text integerValue] + 100
}
你可以这样做:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_txtAmount.text=@"0";
_btn100.tag=100;
_btn500.tag=500;
_btn1000.tag=1000;
}
- (IBAction)btnAmountClick:(id)sender
{
UIButton *btn=sender;
if (btn.tag==100)
{
[self addAmount:@"100"];
}
if (btn.tag==500)
{
[self addAmount:@"500"];
}
if (btn.tag==1000)
{
[self addAmount:@"1000"];
}
}
-(void)addAmount:(NSString*)strAmount
{
int amount=[_txtAmount.text intValue]+[strAmount intValue];
_txtAmount.text=[NSString stringWithFormat:@"%d",amount];
}
You can do this way -
The three buttons below your textfield, link them all to a common method say,
-(IBAction)addBtnClicked:(UIButton *)btn
Given tags to three buttons like for button 100,
btn1.tag=100..
then for button 500,
btn2.tag = 500..
then for button 1000, btn2.tag = 1000.
then in
-(IBAction)addBtnClicked:(UIButton *)btn
{
int value = -1;
if(btn.tag == 100)
{
value =[txtFldText.text intValue] + 100;
}
else if(btn.tag == 500)
{
value =[txtFldText.text intValue] + 500;
}
else if(btn.tag == 1000)
{
value =[txtFldText.text intValue] + 1000;
}
txtFldText.text = [NSString stringWithFormat:@"%d",value];
}
首先给每个按钮设置标签,比如0,1,2。
然后将所有按钮的 IBOutlet 连接到单个 IBAction 和
如下实施 IBAction。
- (IBAction)btnClicked:(id)sender
{
switch ([sender tag]) {
case 0:
{
NSString *s=self.txtField.text;
int add=[s intValue]+ 100;
self.txtField.text=[NSString stringWithFormat:@"%d",add];
break;
}
case 1:
{
NSString *s=self.txtField.text;
int add=[s intValue]+ 200;
self.txtField.text=[NSString stringWithFormat:@"%d",add];
}
break;
case 2:
{
NSString *s=self.txtField.text;
int add=[s intValue]+ 500;
self.txtField.text=[NSString stringWithFormat:@"%d",add];
}
break;
default:
break;
}
}
通过实现标签尝试使用它
- (void)viewDidLoad {
[super viewDidLoad];
[self tagNumber];
}
-(void)tagNumber{
Btn_Incr100.tag=1;
Btn_Incr500.tag=2;
Btn_Incr1000.tag=3;
}
-(void) addTextFieldValue:(UIButton*) button
{
int amount=[Txt_Amount.text intValue];
int AddAmount;
int buttonTagIndex = button.tag;
switch (buttonTagIndex) {
case 1:
AddAmount=100;
break;
case 2:
AddAmount=500;
break;
case 3:
AddAmount=1000;
break;
default:
AddAmount=0;
break;
}
amount=amount+AddAmount;
Txt_Amount.text=[NSString stringWithFormat:@"%d",amount];
}
- (IBAction)btnAmountClick:(id)sender{
UIButton *button = sender;
[self addTextFieldValue:button];
}
对此有更好的解决方案。
您可以按原样使用不同的标签。
只需将您的三个按钮作为此方法的操作并将标签设置为 100/500/1000
- (void)buttonAction:(id)sender {
textField.text = [NSString stringWithFormat:@"%li", (long)(textfield.text.intValue + sender.tag)];
}
我有一个 UITextField
和下面那个 3 按钮。用户可以在UITextField
中输入金额。当用户单击下面的按钮UITextField
时,它会将UITextField
中的金额与按钮中写入的金额相加。
第一个按钮将添加 100,第二个将添加 500,第三个将添加 1000。实现它的最佳方法是什么。
我尝试执行以下操作但得到 button.tag=0
/*Amount increment*/
- (IBAction)Btn_Incr100:(id)sender {
[self addTextFieldValue:nil];
}
- (IBAction)Btn_Incr500:(id)sender {
[self addTextFieldValue:nil];
}
- (IBAction)Btn_Incr1000:(id)sender {
[self addTextFieldValue:nil];
}
-(void)tagNumber{
Btn_Incr100.tag=1;
Btn_Incr500.tag=2;
Btn_Incr1000.tag=3;
}
-(void) addTextFieldValue:(UIButton*) button
{
int amount=[Txt_Amount.text intValue];
int AddAmount;
NSLog(@"button.tag%d",button.tag);
if (button.tag==1) {
AddAmount=100;
}
else if (button.tag==2) {
AddAmount=500;
}
else if(button.tag==3) {
AddAmount=1000;
}else{
AddAmount=0;
}
amount=amount+AddAmount;
Txt_Amount.text=[NSString stringWithFormat:@"%d",amount];
NSLog(@"%d",amount);
NSLog(@"%@",Txt_Amount.text);
}
借助于此您可以实现:
-(void)displayvalue:(id)sender {
UIButton *resultButton = (UIButton *)sender;
NSLog(@" The button's title is %@." resultButton.currentTitle);
}
现在您可以在文本字段中设置此文本。
将此方法添加为每个按钮操作。
-(void) addTextFieldValue:(UIButton*) button
{
NSString *string;
if ([button.text hasPrefix:@"+"] && [button.text length] > 1)
{
string = [string substringFromIndex:1];
}
int textFieldValue =[textField.text integerValue] + string.integerValue;
textField setText:[NSString stringWithFormat:@"%i",textFieldValue];
}
NSString *string;
if ([button.text hasPrefix:@"+"] && [button.text length] > 1)
{
string = [string substringFromIndex:1];
}
int textFieldValue =[textField.text integerValue] + string.integerValue;
textField setText:[NSString stringWithFormat:@"%i",textFieldValue];
或 连接单独的操作
-(IBAction) onClick1: (id) sender]
{
NSLog(@"User clicked %@", sender);
textField.text = [textField.text integerValue] + 100
}
你可以这样做:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_txtAmount.text=@"0";
_btn100.tag=100;
_btn500.tag=500;
_btn1000.tag=1000;
}
- (IBAction)btnAmountClick:(id)sender
{
UIButton *btn=sender;
if (btn.tag==100)
{
[self addAmount:@"100"];
}
if (btn.tag==500)
{
[self addAmount:@"500"];
}
if (btn.tag==1000)
{
[self addAmount:@"1000"];
}
}
-(void)addAmount:(NSString*)strAmount
{
int amount=[_txtAmount.text intValue]+[strAmount intValue];
_txtAmount.text=[NSString stringWithFormat:@"%d",amount];
}
You can do this way -
The three buttons below your textfield, link them all to a common method say,
-(IBAction)addBtnClicked:(UIButton *)btn
Given tags to three buttons like for button 100,
btn1.tag=100..
then for button 500,
btn2.tag = 500..
then for button 1000, btn2.tag = 1000.
then in
-(IBAction)addBtnClicked:(UIButton *)btn
{
int value = -1;
if(btn.tag == 100)
{
value =[txtFldText.text intValue] + 100;
}
else if(btn.tag == 500)
{
value =[txtFldText.text intValue] + 500;
}
else if(btn.tag == 1000)
{
value =[txtFldText.text intValue] + 1000;
}
txtFldText.text = [NSString stringWithFormat:@"%d",value];
}
首先给每个按钮设置标签,比如0,1,2。
然后将所有按钮的 IBOutlet 连接到单个 IBAction 和 如下实施 IBAction。
- (IBAction)btnClicked:(id)sender
{
switch ([sender tag]) {
case 0:
{
NSString *s=self.txtField.text;
int add=[s intValue]+ 100;
self.txtField.text=[NSString stringWithFormat:@"%d",add];
break;
}
case 1:
{
NSString *s=self.txtField.text;
int add=[s intValue]+ 200;
self.txtField.text=[NSString stringWithFormat:@"%d",add];
}
break;
case 2:
{
NSString *s=self.txtField.text;
int add=[s intValue]+ 500;
self.txtField.text=[NSString stringWithFormat:@"%d",add];
}
break;
default:
break;
}
}
通过实现标签尝试使用它
- (void)viewDidLoad {
[super viewDidLoad];
[self tagNumber];
}
-(void)tagNumber{
Btn_Incr100.tag=1;
Btn_Incr500.tag=2;
Btn_Incr1000.tag=3;
}
-(void) addTextFieldValue:(UIButton*) button
{
int amount=[Txt_Amount.text intValue];
int AddAmount;
int buttonTagIndex = button.tag;
switch (buttonTagIndex) {
case 1:
AddAmount=100;
break;
case 2:
AddAmount=500;
break;
case 3:
AddAmount=1000;
break;
default:
AddAmount=0;
break;
}
amount=amount+AddAmount;
Txt_Amount.text=[NSString stringWithFormat:@"%d",amount];
}
- (IBAction)btnAmountClick:(id)sender{
UIButton *button = sender;
[self addTextFieldValue:button];
}
对此有更好的解决方案。 您可以按原样使用不同的标签。 只需将您的三个按钮作为此方法的操作并将标签设置为 100/500/1000
- (void)buttonAction:(id)sender {
textField.text = [NSString stringWithFormat:@"%li", (long)(textfield.text.intValue + sender.tag)];
}