0

This is a tough question. I will simply state what my program can do at the moment. Simply it is a top up system

  1. shows the current balance (eg. $26.00)
  2. I can select a top up value from segmented control (eg. top up $20)
  3. current balance will be updated after selecting a top up. (current balance = $46)

Now here's the problem - when I try to top up again it should add on top of the amount shown but it starts over from scratch and tops up the initial set value.

I was hoping theres a method of saving it as a global variable or even pass it from a method or loop until program has ended.

this is the .m file

// button for confirming the selected top up amount & confirmation box
- (IBAction) confirmAmount:(id)sender{
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Confirm Payment" message:@"Would you like to proceed with the payment?" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Yes", nil];
    alertView.tag = 108;


    [alertView show];
    }


// method for toping up different amounts with segmentedcontrol
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    result =26.00;

    for (int i = 0; i < 100; i++){

    if (topUpamount.selectedSegmentIndex == 0)
    {
        result = result+ 20;
    }
    else if (topUpamount.selectedSegmentIndex == 1)
    {
        result = result + 40;
    }
    else if (topUpamount.selectedSegmentIndex == 2)
    {
        result = result + 50;
    }
    else if (topUpamount.selectedSegmentIndex == 3)
    {
        result = result + 60;
    }
    else if (topUpamount.selectedSegmentIndex == 4)
    {
        result = result + 80;
    }
    else
    {
        result = 0;
    }
    if (alertView.tag==108)
    {
        switch (buttonIndex) {
            case 0:
                break;
            case 1:
                currentBalance.text = [[NSString alloc] initWithFormat: @"%.2f",result];                break;

            default:
                break;
        }
    }
rmaddy
  • 314,917
  • 42
  • 532
  • 579
cc_hey
  • 63
  • 1
  • 11
  • 1
    in your clickedButtonAtIndex: you're setting the starting value i.e. results to a constant 26. So obviously it will always start at 26. Store the value as a property in your controller and increment that value. – MDB983 May 27 '15 at 16:19
  • I really hope you're [not handling real people's real money](http://stackoverflow.com/a/6334471/). – jscs May 27 '15 at 18:24
  • lol this is just a mini task for a uni assignment just learning xcode step by step. – cc_hey May 27 '15 at 23:46

0 Answers0