我有一个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);
}
答案 0 :(得分:0)
借助于此,您可以实现:
-(void)displayvalue:(id)sender {
UIButton *resultButton = (UIButton *)sender;
NSLog(@" The button's title is %@." resultButton.currentTitle);
}
现在您可以在文本字段中设置此文本。
答案 1 :(得分:0)
将此方法添加为每个按钮操作。
-(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];
}
答案 2 :(得分:0)
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
}
答案 3 :(得分:0)
你可以这样做:
- (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];
}
答案 4 :(得分:0)
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];
}
答案 5 :(得分:0)
首先为每个按钮设置标签,如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;
}
}
答案 6 :(得分:0)
尝试通过实施标记
来使用它- (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];
}
答案 7 :(得分:0)
这是一个更好的解决方案。 您可以按原样使用不同的标签。 只需将三个按钮绑定为此方法的操作,并将标记设置为100/500/1000
- (void)buttonAction:(id)sender {
textField.text = [NSString stringWithFormat:@"%li", (long)(textfield.text.intValue + sender.tag)];
}