因此,我创建了一个名为Query
的NSObject子类。
@interface Query : NSObject
@property (nonatomic, assign) NSNumber *weight;
@property (nonatomic, assign) NSNumber *bodyFat;
@property (nonatomic, assign) NSNumber *activityLevel;
@end
这对于设置对象的属性是否正确?
在VC1中:
BodyFatViewController *aViewController = [[BodyFatViewController alloc]init];
aViewController.query = self.query;
[self.navigationController pushViewController:aViewController animated:YES];
在VC2中:
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
Query *anQuery = [[Query alloc]init];
anQuery.bodyFat = [self.bodyFatArray objectAtIndex:row];
anQuery.weight = self.query.weight;
self.query = anQuery;
}
最佳答案
在两个VC之间共享对象是很自然的:
在VC1中:
@property (strong, nonatomic) Query *query;
@synthesize query=_query;
// init it
self.query = [[Query alloc] init];
self.query.weight = [NSNumber numberWithInt:150];
// when it's time to present VC2:
BodyFatViewController *aViewController = [[BodyFatViewController alloc]init];
aViewController.query = self.query;
[self.navigationController pushViewController:aViewController animated:YES];
然后在VC2中:
// this is in the public interface in VC2.h
//
@property (strong, nonatomic) Query *query;
不要在VC2中分配/初始化它。 VC1做到了!但是请随意设置或覆盖值...
self.query.bodyFat = [NSNumber numberWithFloat:0.5];
关于iphone - 如何使用NSObject子类?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10059094/