0

尝试使用分段控制器设置 int 值。我已经看过几个关于如何更改标签的 tuts,但我需要设置一个 int 值。

#import "SecondViewController.h"

@interface SecondViewController ()

@end

@implementation SecondViewController

@synthesize caseCost;
@synthesize dilution;
@synthesize returnMsg;
@synthesize opcValue;
//synthesize opc; < -- Tried
//int opc; <--- tried



- (IBAction)opcView:(id)sender {
    if (opcValue.selectedSegmentIndex == 0) {
    int opc = 320;
    }
    if (opcValue.selectedSegmentIndex == 1) {
    int opc = 128;
    }
    if (opcValue.selectedSegmentIndex == 2) {
    int opc = 135;
    }
    if (opcValue.selectedSegmentIndex == 3) {
    int opc = 88;
    }


}


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

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



   - (IBAction)finishBtn:(id)sender {
    //int opc = 320;
    float case_cost = ([caseCost.text floatValue]);
    float dilutionValue = ([dilution.text floatValue]);
    float gpc = (opc / dilutionValue);
    float gCost = (case_cost / gpc);
    float bCost = (gCost / 4);
    float bpc = (gpc * 4);

NSNumberFormatter *formatterCur = [[NSNumberFormatter alloc] init];
NSNumberFormatter *formatterInt = [[NSNumberFormatter alloc] init];
[formatterCur setNumberStyle:NSNumberFormatterCurrencyStyle];
[formatterInt setNumberStyle:NSNumberFormatterDecimalStyle];

NSString *bottlesCost = [formatterCur stringFromNumber:[NSNumber numberWithFloat:bCost]];
NSString *gallons = [formatterInt stringFromNumber:[NSNumber numberWithInt:gpc]];
NSString *gallonsCost = [formatterCur stringFromNumber:[NSNumber numberWithFloat:gCost]];
NSString *bottles = [formatterInt stringFromNumber:[NSNumber numberWithInt:bpc]];


returnMsg.text = [NSString stringWithFormat:@"%@ gallons per case at %@ per gallon and %@      - 32 oz bottles at %@ per bottle.", gallons, gallonsCost, bottles, bottlesCost];


}

   - (IBAction)opcView:(id)sender {
    }

   @end

在“float gpc = (opc/dilutionValue);”行中 is 显示为 opc 的未知值,即使我认为它应该来自分段控制器。我正在使用分段控制器而不是我在 Java 中使用的单选按钮。我使用“//int opc=320”来确保其余代码正常工作。

4

1 回答 1

1

在您方法的每个if块中,您- (IBAction)opcView:(id)sender正在创建一个int名为opc. 所以当执行离开if块时,局部变量就消失了。因此,在范围内- (IBAction)finishBtn:(id)sender没有命名变量opc

您也应该声明opc为财产。当段控件更改选择时,您将设置此属性。稍后,您可以在完成按钮的处理程序中读取该属性的值。

#import "SecondViewController.h"

@interface SecondViewController()
@property (nonatomic) int opc;
@end


@implementation SecondViewController

// this method is wired to the segment control's UIControlEventValueChanged event
- (IBAction)opcView:(id)sender 
{
  if (opcValue.selectedSegmentIndex == 0) {
    self.opc = 320;
  }
  if (opcValue.selectedSegmentIndex == 1) {
    self.opc = 128;
  }
  if (opcValue.selectedSegmentIndex == 2) {
    self.opc = 135;
  }
  if (opcValue.selectedSegmentIndex == 3) {
    self.opc = 88;
  }
}

- (IBAction)finishBtn:(id)sender 
{
  float case_cost = ([caseCost.text floatValue]);
  float dilutionValue = ([dilution.text floatValue]);
  float gpc = (self.opc / dilutionValue);

  // lots more code
}
于 2013-10-24T03:10:32.827 回答