必须按两次IBAction进行计算?

时间:2015-12-31 09:38:09

标签: ibaction

我制作的应用程序是一种基本风格的学生贷款计算器。我坚持的位是按钮计算。用户必须输入多个值,然后单击计算按钮,除了底部计算之外的所有内容都有效,为了解决这个问题,我必须再次按下计算按钮。

此图显示了按一次计算按钮后应用程序的样子。

Image link 1

此图显示了按两次计算按钮后应用程序的外观。然后计算底部计算

Image link 1

这是我正在使用的当前代码:

//
//  ViewController.m
//  Repayment Calculator
//
//  Created by on 04/11/2015.
//  Copyright (c) 2015. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

@synthesize button1, textbox1, textbox2, textbox3, startingsalary, repaymentlabel, debtlabel, startingrepayments, timetakentopayoffloan, realtimetakentopayoffloan, debtamounttextfield, annualrepaymentstextfield, monthlyrepaymentstextfield,weeklyrepaymentstextfield, payoffloantextfield, testlabel, annualrepaymentstest, totaldebtamounttest;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

// - (IBAction)button1Press:(id)sender {
//    [button1 setTitle:textbox1.text forState:UIControlStateNormal];
// }

//The start of the calculate button that starts the calculations.

 -(IBAction)button1Press:(id)sender

{

    int x = ([textbox1.text intValue]);
    int y = ([textbox2.text intValue]);
    int q = ([textbox3.text intValue]);
    float startingsal = ([startingsalary.text intValue]);


    int debtamounttfield = ([debtamounttextfield.text intValue]);

    float annualrepayments = ([annualrepaymentstextfield.text floatValue]);

    //PROBLEM STARTS
    float p1 = debtamounttfield /annualrepayments;//([debtamounttextfield.text intValue]);

    if(isnan(p1) || isinf(p1)){
        p1 = 0.00;
    }


    [testlabel setText:[NSString stringWithFormat:@"%.01f",p1]];

    [totaldebtamounttest setText:[NSString stringWithFormat:@"%d", debtamounttfield]];

    [annualrepaymentstest setText:[NSString stringWithFormat:@"%@", annualrepaymentstextfield]];

    [payoffloantextfield setText:[NSString stringWithFormat:@"%.01f",  p1]];
    //PROBLEM ENDS


    [debtamounttextfield setText:[NSString stringWithFormat:@"%i" , (x + y) * q]];



    int arp = (startingsal - 16910)*0.09;

    [annualrepaymentstextfield setText:[NSString stringWithFormat:@"%i",  arp ]];

    [monthlyrepaymentstextfield setText:[NSString stringWithFormat:@"%f",  (startingsal - 16910)*0.09/12]];




    [weeklyrepaymentstextfield setText:[NSString stringWithFormat:@"%f",  (startingsal - 16910)*0.09/12/4]];

    //Error Handling
    //Enter starting salary
    if ([startingsalary.text isEqualToString: @""]) {

        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Error"
                                                                                 message:@"Please enter your expected starting salary"
                                                                          preferredStyle:UIAlertControllerStyleAlert];
        //We add buttons to the alert controller by creating UIAlertActions:
        UIAlertAction *actionOk = [UIAlertAction actionWithTitle:@"Ok"
                                                           style:UIAlertActionStyleDefault
                                                         handler:nil]; //You can use a block here to handle a press on this button
        [alertController addAction:actionOk];
        [self presentViewController:alertController animated:YES completion:nil];


    }

    //If course length is equal to 0
    if ([textbox3.text isEqualToString: @"0"]) {

        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Error"
                                                                                 message:@"Your course lenth cannot be 0 Years. Please enter a different value"
                                                                          preferredStyle:UIAlertControllerStyleAlert];
        //We add buttons to the alert controller by creating UIAlertActions:
        UIAlertAction *actionOk = [UIAlertAction actionWithTitle:@"Ok"
                                                           style:UIAlertActionStyleDefault
                                                         handler:nil]; //You can use a block here to handle a press on this button
        [alertController addAction:actionOk];
        [self presentViewController:alertController animated:YES completion:nil];
    }


    //If course length text field is empty
    if ([textbox3.text isEqualToString: @""]){
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Error"
                                                                                 message:@"Please enter your current course length"
                                                                          preferredStyle:UIAlertControllerStyleAlert];
        //We add buttons to the alert controller by creating UIAlertActions:
        UIAlertAction *actionOk = [UIAlertAction actionWithTitle:@"Ok"
                                                           style:UIAlertActionStyleDefault
                                                         handler:nil]; //You can use a block here to handle a press on this button
        [alertController addAction:actionOk];
        [self presentViewController:alertController animated:YES completion:nil];


    }

    //Error handling for Annual University Fee
    if ([textbox1.text isEqualToString: @""]){
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Error"
                                                                                 message:@"Please enter your annual University Fee."
                                                                          preferredStyle:UIAlertControllerStyleAlert];
        //We add buttons to the alert controller by creating UIAlertActions:
        UIAlertAction *actionOk = [UIAlertAction actionWithTitle:@"Ok"
                                                           style:UIAlertActionStyleDefault
                                                         handler:nil]; //You can use a block here to handle a press on this button
        [alertController addAction:actionOk];
        [self presentViewController:alertController animated:YES completion:nil];


    }

    if ([textbox2.text isEqualToString: @""]){
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Error"
                                                                                 message:@"Please enter your annual maintenance loan value."
                                                                          preferredStyle:UIAlertControllerStyleAlert];
        //We add buttons to the alert controller by creating UIAlertActions:
        UIAlertAction *actionOk = [UIAlertAction actionWithTitle:@"Ok"
                                                           style:UIAlertActionStyleDefault
                                                         handler:nil]; //You can use a block here to handle a press on this button
        [alertController addAction:actionOk];
        [self presentViewController:alertController animated:YES completion:nil];


    }

}




-(IBAction)clearbutton:(id)sender {

    [textbox1 setText:[NSString stringWithFormat:@""]];

    [textbox2 setText:[NSString stringWithFormat:@""]];

    [textbox3 setText:[NSString stringWithFormat:@""]];


    [startingrepayments setText:[NSString stringWithFormat:@"Loan at start of repayments: "]];

    [startingsalary setText:[NSString stringWithFormat:@""]];

    [realtimetakentopayoffloan setText:[NSString stringWithFormat:@""]];

    [debtamounttextfield setText:[NSString stringWithFormat:@""]];

    [annualrepaymentstextfield setText:[NSString stringWithFormat:@""]];

    [monthlyrepaymentstextfield setText:[NSString stringWithFormat:@""]];

    [weeklyrepaymentstextfield setText:[NSString stringWithFormat:@""]];

    [payoffloantextfield setText:[NSString stringWithFormat:@""]];

    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Cleared"
                                                                             message:@"All fields have now been cleared."
                                                                      preferredStyle:UIAlertControllerStyleAlert];
    //We add buttons to the alert controller by creating UIAlertActions:
    UIAlertAction *actionOk = [UIAlertAction actionWithTitle:@"Ok"
                                                       style:UIAlertActionStyleDefault
                                                     handler:nil]; //You can use a block here to handle a press on this button
    [alertController addAction:actionOk];
    [self presentViewController:alertController animated:YES completion:nil];
}




@end

非常感谢任何可以帮助我的人。这是我现在已经停留了一段时间的事情,并希望得到解决。

1 个答案:

答案 0 :(得分:0)

好吧我是新手,我根本不知道,但我会尽力帮助你。 英语也不是我的母语,请原谅我的错误,我会尽我所能。 所以,似乎你的数据卡在某处。我要给你的解决方案可能对你有所帮助,而且一劳永逸。 我会放置一个NSTimer,它会在所有文本字段都被填充的时间间隔内计算出很短的时间间隔,这样就可以“先按”并获得结果。 然后,“计算”按钮仅显示在标签或文本字段中(如您所愿)的结果。一旦显示,您就会使NSTimer无效,这样就不会出错。

我不知道您是否理解我,如果您对此消息有任何疑问,请与我联系。

我告诉你的代码就是这样:

//make a values int: 
int valor;


//in your viewDidLoad probably:
NSTimer *intervalo = [NSTimer scheduledTimerWithTimeInterval:(1.0/3.0) target:self selector:@selector(autocalculate) userInfo:nil repeats:YES];


-(void)autocalculate{
//the code that you have in your UIButton "Calculate" 
//take the result into the int valor so it can be use later
}

-(IBAction)show{ 
**thenameofyourtextresultstextfield**=[NSString stringWithFormat:@"%i", valor];
[intervalo invalidate];

希望有所帮助