我試圖隱藏UIView和其中的對象。該按鈕位於包含在同一ViewController中的TableView節標題中。當按鈕被選中時,隱藏視圖和其他對象
按鈕按下被存儲在...
Activity.h
@property BOOL invitesAll;
Activity.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訪問屬性作爲基礎,但無濟於事。當我運行這個時,在按下inviteAll時expirationViewHeightConstraint,timeToRespondLabel,expirationSlider或expirationLabel IBOutlets都沒有可見。
我是一個新手,所以猜測我錯過了一些基本的東西。我的方法有缺陷嗎?
你做了什麼故障排除?是否調用了inviteAllAction方法?你試圖設置非零的屬性(如self.timeToRespondLabel,self.expirationSlider等)? – rdelmar 2014-11-14 22:55:16
@rdelmar驗證了inviteAllAction方法正在通過斷點調用。如何檢查屬性是否設置爲非零? – thedonniew 2014-11-14 22:58:53
只需記錄它們。 NSLog(@「%@」,self.timeToRespondLabel)或設置斷點並使用調試器。 – rdelmar 2014-11-14 23:00:16