一个简单的问题,希望。为什么我的lostWagesLabel和totalNeedLabel为0 - 以下代码中计算的两个值?
编辑:原始问题完全替换为以下代码-根据迄今为止收到的建议进行了更新:
三个值通过 segue 从父场景传递到此场景。当在此场景中显示为文本值时,所有三个值都会正确显示。但是当我运行 NSLog 时,只有一个值(周)正确显示 - 这意味着其他两个值将作为 0 进入计算。
以下是父场景中的声明:
IBOutlet UISlider *earningsSlider;
IBOutlet UILabel *earningsLabel;
IBOutlet UISlider *disabledSlider;
IBOutlet UILabel *disabledLabel;
IBOutlet UISlider *expensesSlider;
IBOutlet UILabel *expensesLabel;
这是一个实现示例:
- (IBAction)earningsSlide:(id)sender {
NSNumberFormatter *commas = [NSNumberFormatter new];
commas.numberStyle = NSNumberFormatterDecimalStyle;
int earningsValue = (int)(earningsSlider.value / 1000.0);
[earningsSlider setValue:earningsValue * 1000.0];
earningsLabel.text = [NSString stringWithFormat:@"$%@",
[commas stringFromNumber:[NSNumber numberWithFloat:earningsValue * 1000]]];
}
这是所有东西都通过segue的地方:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"DisabilitySegue"]) {
DisabilityViewController *destViewController = segue.destinationViewController;
destViewController.earningsNumber = self->earningsLabel.text;
destViewController.expensesNumber = self->expensesLabel.text;
destViewController.disabledNumber = self->disabledLabel.text;
}
}
以下是新场景中的声明:
@property (nonatomic, strong) IBOutlet UILabel *earningsLabel;
@property (nonatomic, strong) NSString *earningsNumber;
@property (nonatomic, strong) IBOutlet UILabel *expensesLabel;
@property (nonatomic, strong) NSString *expensesNumber;
@property (nonatomic, strong) IBOutlet UILabel *disabledLabel;
@property (nonatomic, strong) NSString *disabledNumber;
@property (nonatomic, strong) IBOutlet UILabel *message;
@property (nonatomic, strong) NSString *lostWages;
@property (nonatomic, strong) IBOutlet UILabel *lostWagesLabel;
@property (nonatomic, strong) NSString *totalNeed;
@property (nonatomic, strong) IBOutlet UILabel *totalNeedLabel;
这是当前状态下的实现:
- (void)viewDidLoad
{
NSNumberFormatter *commas = [NSNumberFormatter new];
commas.numberStyle = NSNumberFormatterDecimalStyle;
earningsLabel.text = earningsNumber;
expensesLabel.text = expensesNumber;
disabledLabel.text = disabledNumber;
int earn = [earningsNumber intValue];
int expense = [expensesNumber intValue];
int weeks = [disabledNumber intValue];
NSLog(@"%d %d %d",earn,expense,weeks);
//print values to console
int lost = (int)(earn / 52 * weeks);
int need = (int)(expense * 12 / 52 * weeks);
NSLog(@"%d %d",lost,need);
//print values to console
lostWages = [NSString stringWithFormat:@"$%@",
[commas stringFromNumber:[NSNumber numberWithInteger:lost]]];
totalNeed = [NSString stringWithFormat:@"$%@",
[commas stringFromNumber:[NSNumber numberWithInteger:need]]];
lostWagesLabel.text = lostWages;
totalNeedLabel.text = totalNeed;
[super viewDidLoad];
}
当我在父场景中使用尽可能少的 1000、100 和 1 作为收入、费用和禁用(周)运行 NSLog 时,NSLog 看起来像这样:
2012-08-17 09:01:38.277 我的应用程序 [8117:15203] 0 0 1
2012-08-17 09:01:38.279 我的应用程序 [8117:15203] 0 0
我还尝试更改通过 segue 发送的内容的格式,并将其作为父场景中的声明:
@interface IncomeProtectionViewController : UIViewController {
IBOutlet UISlider *earningsSlider;
IBOutlet UISlider *expensesSlider;
IBOutlet UISlider *disabledSlider;
IBOutlet UILabel *earningsLabel;
IBOutlet UILabel *expensesLabel;
IBOutlet UILabel *disabledLabel;
NSNumber *earningsNumber;
NSNumber *expensesNumber;
NSNumber *disabledNumber;
}
这在实现中:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"DisabilitySegue"]) {
DisabilityViewController *destViewController = segue.destinationViewController;
destViewController.earningsNumber = self->earningsNumber;
destViewController.expensesNumber = self->expensesNumber;
destViewController.disabledNumber = self->disabledNumber;
}
}
但是我在目标场景中收到了不兼容警告。