【发布时间】:2014-11-14 22:46:18
【问题描述】:
当取消选择按钮时,我试图隐藏 UIView 和其中的对象。该按钮位于同一 ViewController 中包含的 TableView 节标题中。
按下的按钮被存储在...
活动.h
@property BOOL invitesAll;
活动.m
#import "Activity.h"
@dynamic invitesAll;
还有剩下的代码……
InviteContactsViewController.h
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *expirationViewHeightConstraint;
@property (weak, nonatomic) IBOutlet UILabel *timeToRespondLabel;
@property (weak, nonatomic) IBOutlet UISlider *expirationSlider;
@property (weak, nonatomic) IBOutlet UILabel *expirationLabel;
@property (strong, nonatomic) IBOutlet UIButton *inviteAll;
InviteesTableViewController.h
#import "InviteContactsViewController.h"
@property (weak, nonatomic) NSLayoutConstraint *expirationViewHeightConstraint;
@property (weak, nonatomic) UILabel *timeToRespondLabel;
@property (weak, nonatomic) UISlider *expirationSlider;
@property (weak, nonatomic) UILabel *expirationLabel;
InviteesTableViewController.m
#import "InviteesTableViewController.h"
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
// create button
InviteAllButton *inviteAllButton = [InviteAllButton buttonWithType:UIButtonTypeCustom];
[inviteAllButton setTitle:@"Order Matters" forState:UIControlStateSelected];
[inviteAllButton setTitle:@"Order Doesn't Matter" forState:UIControlStateNormal];
inviteAllButton.titleLabel.font = [UIFont fontWithName:@"Avenir" size:11.0];
inviteAllButton.titleLabel.numberOfLines = 2;
[inviteAllButton addTarget:self action:@selector(inviteAllAction:) forControlEvents:UIControlEventTouchUpInside];
[headerView addSubview:inviteAllButton];
// set inviteAll button state
inviteAllButton.selected = !_activity.invitesAll;
// set constraints
[inviteAllButton setTranslatesAutoresizingMaskIntoConstraints:NO];
NSDictionary *views = NSDictionaryOfVariableBindings(inviteAllButton, titleLabel);
[headerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"[titleLabel]-(>=5)-[inviteAllButton(96)]-5-|" options:0 metrics:nil views:views]];
[headerView addConstraint:[NSLayoutConstraint constraintWithItem:inviteAllButton attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:headerView attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0.0]];
}
- (void)inviteAllAction:(UIButton *)sender {
sender.selected = !sender.selected;
_activity.invitesAll = !_activity.invitesAll;
[self validateReordering];
[self.tableView reloadData];
NSString *state = _activity.invitesAll ? @"invitesAll" : @"orderMatters";
// In my attempt to hide the view, I've set an expirationViewHeightConstraint
// and try to change its constant as the button is pressed
if (self.activity.invitesAll) {
self.expirationViewHeightConstraint.constant = 0.f;
self.timeToRespondLabel.hidden = YES;
self.expirationSlider.hidden = YES;
self.expirationLabel.hidden = YES;
} else {
self.expirationViewHeightConstraint.constant = 35;
self.timeToRespondLabel.hidden = NO;
self.expirationSlider.hidden = NO;
self.expirationLabel.hidden = NO;
}
}
我在将高度约束常量更改为 0.f 时引用了 Max's 答案,并尝试按照 Simon's 答案作为基础访问跨 ViewControllers 的属性,但无济于事。当我运行此命令时,expirationViewHeightConstraint、timeToRespondLabel、expirationSlider 或 expireLabel IBOutlets 没有任何可见的变化。
我是新手,所以我猜我在这里缺少一些基本的东西。我的方法有缺陷吗?
【问题讨论】:
-
您做了哪些故障排除?是否调用了inviteAllAction 方法?您尝试设置的属性是否为非零(如 self.timeToRespondLabel、self.expirationSlider 等)?
-
@rdelmar 已验证正通过断点调用邀请AllAction 方法。如何检查属性是否设置为非零?
-
只需记录它们。 NSLog(@"%@", self.timeToRespondLabel) 或设置断点并使用调试器。
-
@rdelmar 看起来属性确实返回 null。如何从 InviteContactsViewController 正确调用它们?万一有所不同,InviteesTableViewController 位于 InviteContactsViewController 上的容器中。
-
我不确定您在做什么,因为两个控制器中的标签名称相同。你为什么这样做?
标签: ios objective-c uibutton nslayoutconstraint