我正在填充标签中的对象字段,当在Log中显示时,标签是正确的值,但对象的字段为空。我是从Android / Java背景来的,这很尴尬。 任何帮助都会很棒。
要清楚,“土壤类型字段”日志显示“示例”,而“土壤类型”日志显示(null)
- (IBAction)saveButton:(id)sender {
Soil *thisSoil = self.thisSoil;
thisSoil.soilType = self.soilNameField.text;
NSLog(@"soil type field %@", self.soilNameField.text);
NSLog(@"soil type: %@", thisSoil.soilType);
thisSoil.frictionAngle = [self.frictionAngleValue.text integerValue];
if ([self.soilUnitsSwitch isOn] ) {
thisSoil.cohesion = [self.cohesionValue.text doubleValue];
thisSoil.unitWeight = [self.unitWeightValue.text doubleValue];
}else{
thisSoil.cohesion = [self.cohesionValue.text doubleValue];
thisSoil.unitWeight = [self.unitWeightValue.text doubleValue];
}
[self.delegate SoilCreatorViewController:self didFinishItem:thisSoil];
[self.navigationController popViewControllerAnimated:YES];
}
整个控制器.m文件
#import "SoilCreatorViewController.h"
#define IMPERIAL_TO_METRIC 0.3048
#define KG_TO_LBS 2.2
@interface SoilCreatorViewController ()
@end
@implementation SoilCreatorViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)soilQuestions:(id)sender {
[self popupmaker :@"Friction Angle" : @"Phi is the angle of internal friction for soil, which governs soil strength and resistance. This value should be attained from competent field testing and the judgment of a licensed engineer."];
[self popupmaker :@"Soil Cohesion": @"Cohesion defines the non-stress dependent shear strength of soil and should be used with caution. Typically,cohesion occurs in stiff, over-consolidated clays or cemented native soils. Cohesion should be neglected if the designer is unsure of its presence."];
}
- (IBAction)SwitchDidChange:(id)sender {
if ([sender isOn]) {
self.cohesionUnits.text = @"Ft";
self.unitWeightUnits.text= @"M";
}else{
self.cohesionUnits.text = @"M";
self.unitWeightUnits.text = @"M";
}
}
- (IBAction)unitWtDidChange:(id)sender {
self.unitWeightValue.text = [NSString stringWithFormat:@"%.1f", (double)self.unitWeightStepper.value];
}
- (IBAction)frictionAngleDidChange:(id)sender {
self.frictionAngleValue.text = [NSString stringWithFormat:@"%d", (int)self.frictionAngleStepper.value];
}
- (IBAction)cohesionDidChange:(id)sender {
self.cohesionValue.text = [NSString stringWithFormat:@"%.1f", (double)self.cohesionStepper.value];
}
- (IBAction)textFieldDismiss:(id)sender {
[[self view] endEditing:YES];
}
- (IBAction)UnitSwitch:(id)sender {
if ([sender isOn]) {
self.unitWeightUnits.text = @"LBS/cubic Ft.";
self.cohesionUnits.text = @"imp";
}else{
self.unitWeightUnits.text = @"KG/m3";
self.cohesionUnits.text = @"met";
}
}
- (IBAction)cancelButton:(id)sender {
[self.navigationController popViewControllerAnimated:YES];
}
- (IBAction)saveButton:(id)sender {
Soil *thisSoil = self.thisSoil;
thisSoil.soilType = self.soilNameField.text;
NSLog(@"soil type field %@", self.soilNameField.text);
NSLog(@"soil type: %@", thisSoil.soilType);
thisSoil.frictionAngle = [self.frictionAngleValue.text integerValue];
if ([self.soilUnitsSwitch isOn] ) {
thisSoil.cohesion = [self.cohesionValue.text doubleValue];
thisSoil.unitWeight = [self.unitWeightValue.text doubleValue];
}else{
thisSoil.cohesion = [self.cohesionValue.text doubleValue];
thisSoil.unitWeight = [self.unitWeightValue.text doubleValue];
}
[self.delegate SoilCreatorViewController:self didFinishItem:thisSoil];
[self.navigationController popViewControllerAnimated:YES];
}
-(void)popupmaker:(NSString *)title :(NSString *)message{
UIAlertView * alert =[[UIAlertView alloc ] initWithTitle:title
message:message
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil
];
[alert show];
}
@end
.h文件
#import "Soil.h"
@class SoilCreatorViewController;
@protocol SoilCreatorViewDelegate <NSObject>
-(void)SoilCreatorViewController:(SoilCreatorViewController *)controller didFinishItem:(Soil *)item;
@property (nonatomic, weak) id <SoilCreatorViewDelegate> delegate;
@end
#import <UIKit/UIKit.h>
#import "CalculationDetailViewController.h"
@interface SoilCreatorViewController : UIViewController
@property (nonatomic,weak) id<SoilCreatorViewDelegate> delegate;
@property (weak, nonatomic) IBOutlet UITextField *soilNameField;
@property (weak, nonatomic) IBOutlet UISwitch *soilUnitsSwitch;
@property (nonatomic,strong) Soil *thisSoil;
@property (weak, nonatomic) IBOutlet UIStepper *frictionAngleStepper;
@property (weak, nonatomic) IBOutlet UIStepper *unitWeightStepper;
@property (weak, nonatomic) IBOutlet UIStepper *cohesionStepper;
@property (weak, nonatomic) IBOutlet UILabel *unitWeightValue;
@property (weak, nonatomic) IBOutlet UILabel *frictionAngleValue;
@property (weak, nonatomic) IBOutlet UILabel *cohesionValue;
@property (weak, nonatomic) IBOutlet UILabel *unitWeightUnits;
@property (weak, nonatomic) IBOutlet UILabel *cohesionUnits;
- (IBAction)soilQuestions:(id)sender;
- (IBAction)SwitchDidChange:(id)sender;
- (IBAction)unitWtDidChange:(id)sender;
- (IBAction)frictionAngleDidChange:(id)sender;
- (IBAction)cohesionDidChange:(id)sender;
- (IBAction)textFieldDismiss:(id)sender;
- (IBAction)cancelButton:(id)sender;
- (IBAction)saveButton:(id)sender;
-(void)popupmaker:(NSString *)title :(NSString *)message;
@end
答案 0 :(得分:2)
我没有看到任何设置thisSoil的代码。在代码中的某个时刻,您需要编写类似
的内容self.thisSoil = [[Soil alloc] init];
或self.thisSoil将永远保持零。