Ios5 分段控制器

Ios5 分段控制器,ios5,Ios5,尝试使用分段控制器设置int值。我看过一些关于如何更改标签的教程,但我需要设置一个int值 #import "SecondViewController.h" @interface SecondViewController () @end @implementation SecondViewController @synthesize caseCost; @synthesize dilution; @synthesize returnMsg; @synthesize opcValue; /

尝试使用分段控制器设置int值。我看过一些关于如何更改标签的教程,但我需要设置一个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
#导入“SecondViewController.h”
@接口SecondViewController()
@结束
@第二视图控制器的实现
@综合成本;
@综合稀释;
@合成味精;
@综合OPC值;
//合成opc;<--尝试

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

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

#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
}

尝试在implementations和synthasize下添加“intopc;”,但仍然看不到veriable。您还提到要将其声明为属性。这是在.h文件中完成的,对吗?如果是这样,我试过了,但也不喜欢。提前谢谢@matthewI已经尝试了我所能想到的每一个组合,或者已经阅读了获得-(iAction)answer方法来将“opc”的值传递给-(iAction)finishBtn方法。请帮忙。我不敢相信允许方法共享变量有那么难。我猜Java喜欢共享,而iOS不喜欢;转到.h文件。感谢您的帮助!这很有效,而且很愚蠢!我也试着去掉“int”这个词,效果也不错。如果我花点时间看看我的Java版本,我可以省下很多时间在桌子上捶脑袋。再次感谢!