【发布时间】:2015-11-18 08:05:37
【问题描述】:
我知道这是微不足道的,但我把自己搞砸了,花了好几个小时。
我有一个 UIViewController,它由 CategoryTableview 和 ProductsTableView 组成。
ProductsTableView 正在创建名为 ProductTableViewCell 的自定义 TableViewCell。
ProductTableViewCell 有标题标签、细节标签、价格标签和一个自定义的步进器来控制从 UIViewController 子类化的产品的数量。
步进器由两个按钮和一个文本框组成。
我认为我实现程序的方式比它应该的要复杂。
我的问题是如何检测父视图控制器中步进控制器更改的值。
代码是:
ProductionViewController.h (parent view controller for listing everything)
@interface ProductionViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic,strong) UITableView* categoryTable;
@property (nonatomic,strong) UITableView* productsTable;
@property (nonatomic,strong) CheckOutBar* checkOutBar;
//datasources
@property (nonatomic,strong) NSMutableArray* menuLists;
@property (nonatomic,strong) NSMutableArray* detailLists;
//datefield
@property (nonatomic,strong) NSString* startDate;
@property (nonatomic,strong) NSString* endDate;
产品的自定义单元格是
@interface ProductCell : UITableViewCell
@property (nonatomic, strong) UIImageView* eqiupmentImage;
@property (nonatomic, strong) UILabel* Title;
@property (nonatomic, strong) UILabel* details;
@property (nonatomic, strong) UILabel* prices;
@property (nonatomic, strong) MCStepperViewController* stepper;
cell的实现方法是
@implementation ProductCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
{
_Title = [[UILabel alloc] init];
_eqiupmentImage = [[UIImageView alloc] init];
_details = [[UILabel alloc] init];
_details.font = kFONT(12);
_details.textColor = KColor_Gray;
_prices = [[UILabel alloc] init];
_stepper = [[MCStepperViewController alloc] init];
[self.contentView addSubview:_stepper.view];
[self.contentView addSubview:_Title];
[self.contentView addSubview:_eqiupmentImage];
[self.contentView addSubview:_details];
[self.contentView addSubview:_prices];
}
return self;
}
-(void)layoutSubviews
{
[super layoutSubviews];
[_eqiupmentImage setFrame:CGRectMake(kLeftMargin, kLeftMargin, 80 ,80)];
[_Title setFrame:CGRectMake(10+CGRectGetMaxX(_eqiupmentImage.frame), kLeftMargin/2, 60, 20)];
[_details setFrame:CGRectOffset(_Title.frame, 0, 20)];
[_prices setFrame:CGRectMake(10+CGRectGetMaxX(_eqiupmentImage.frame), CGRectGetMaxY(_eqiupmentImage.frame)-20, 40, 20)];
[_stepper.view setFrame:CGRectOffset(_prices.frame, 40, 0)];
}
自定义的stepperview是
MCStepperViewController.h (for controlling the amount of products)
@interface MCStepperViewController : UIViewController
@property (nonatomic, strong) UITextField *amountText;
@property (nonatomic, strong) UIButton * plusButton;
@property (nonatomic, strong) UIButton * minusButton;
@property (nonatomic, assign) int max;
@property (nonatomic, assign) int min;
@property (nonatomic, assign) int amount;
谢谢
【问题讨论】:
-
探索键值观察 (KVO) 的概念。您可以在父视图控制器中观察“数量”变量的值。每次更改值时,父级都会收到通知,您可以指定要调用的方法。
-
@TheAppMentor 谢谢,因为我要观察的值是步进控制器的一个属性,而步进是单元格的属性。我不确定我是否能够直接观察主父控制器中的值,或者我应该在自定义单元格中添加一个观察者?
-
@Mix 是否希望每次值更改时都收到通知?或者您只想检查求和逻辑的当前值?
-
只要您将金额作为属性(您拥有),您应该能够使用 KVO 观察其值的变化。另一种选择是发布 NSNotification,但我认为 KVO 旨在处理这种情况。
标签: ios objective-c iphone uitableview uiviewcontroller