2014-11-14 58 views
0

我试图隐藏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都没有可见。

我是一个新手,所以猜测我错过了一些基本的东西。我的方法有缺陷吗?

+0

你做了什么故障排除?是否调用了inviteAllAction方法?你试图设置非零的属性(如self.timeToRespondLabel,self.expirationSlider等)? – rdelmar 2014-11-14 22:55:16

+0

@rdelmar验证了inviteAllAction方法正在通过断点调用。如何检查属性是否设置为非零? – thedonniew 2014-11-14 22:58:53

+0

只需记录它们。 NSLog(@“%@”,self.timeToRespondLabel)或设置断点并使用调试器。 – rdelmar 2014-11-14 23:00:16

回答

0

您对InviteesTableViewController中的属性声明所做的操作是错误的。你不能只在子控制器中声明一些属性,并期望它们将指向其他控制器中的对象(父或不是)。你需要去的家长,您可以使用self.parentViewController参考,然后访问它的属性,

InviteContactsViewController *inviteVC = (InviteContactsViewController *)self.parentViewController; 
if (self.activity.invitesAll) { 
     inviteVC.expirationViewHeightConstraint.constant = 0.f; 
     inviteVC.timeToRespondLabel.hidden = YES; 
     inviteVC.expirationSlider.hidden = YES; 
     inviteVC.expirationLabel.hidden = YES; 
    } else { 
     inviteVC.expirationViewHeightConstraint.constant = 35; 
     inviteVC.timeToRespondLabel.hidden = NO; 
     inviteVC.expirationSlider.hidden = NO; 
     inviteVC.expirationLabel.hidden = NO; 
    } 

您应该删除所有这些属性声明中InviteesTableViewController。

+0

谢谢,刚试过这个,我得到了一个类似的错误:“Property'expirationViewHeightConstraint'not found on 'UIViewController *'的对象 – thedonniew 2014-11-15 00:41:50

+0

@D_W,听起来像self.parentViewController没有返回一个InviteContactsViewController。然后在inviteAllAction:方法中,记录self。parentViewController,并看看你给了什么。 – rdelmar 2014-11-15 00:44:52

+0

NSLog(@“%@”,self.parentViewController);日志2014-11-14 17:15:04.709 SampleProject [28411:1689414] thedonniew 2014-11-15 01:17:11

相关问题