0

I'm trying to hide a UIView and objects within it when a button is deselected. The button is located in a TableView section header contained in the same ViewController.

The button press is being stored in...

Activity.h

@property BOOL invitesAll;

Activity.m

#import "Activity.h"

@dynamic invitesAll;

And the rest of the code...

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;
    }
}

I referenced Max's answer on changing the height constraint constant to 0.f and tried to follow Simon's answer on accessing properties across ViewControllers as a basis, but to no avail. When I run this nothing visible happens with the expirationViewHeightConstraint, timeToRespondLabel, expirationSlider, or expirationLabel IBOutlets when inviteAll is pressed.

I'm a newb so guessing I'm missing something basic here. Is my approach flawed?

Community
  • 1
  • 1
thedonniew
  • 213
  • 1
  • 3
  • 11
  • What troubleshooting have you done? Is the inviteAllAction method called? Are the properties you're trying to set non-nil (like self.timeToRespondLabel, self.expirationSlider, etc)? – rdelmar Nov 14 '14 at 22:55
  • @rdelmar verified inviteAllAction method is being called via breakpoint. How do I check if the properties are set to non-nil? – thedonniew Nov 14 '14 at 22:58
  • Just log them. NSLog(@"%@", self.timeToRespondLabel) or set a breakpoint and use the debugger. – rdelmar Nov 14 '14 at 23:00
  • @rdelmar Looks like the properties are indeed returning null. How to call them properly from InviteContactsViewController? In case it makes a difference, InviteesTableViewController is located in a container on InviteContactsViewController. – thedonniew Nov 14 '14 at 23:36
  • I'm not sure what you're doing, since you have the same name for labels in both controllers. Why are you doing that? – rdelmar Nov 14 '14 at 23:56
  • The objects are located on the InviteContactsViewController, but I'm trying to access them on InviteesTableViewController. Without declaring them on InviteesTableViewController.h I was getting "Property 'timeToRespondLabel' not found on object of type 'InviteesTableViewController'. – thedonniew Nov 15 '14 at 00:09

1 Answers1

0

What you're doing with the property declarations in InviteesTableViewController is wrong. You can't just declare some properties in the child controller and expect that they will point to objects in some other controller (parent or not). You need to get a reference to the parent, which you can using self.parentViewController, and then access its properties with that,

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;
    }

You should delete all those property declarations in InviteesTableViewController.

rdelmar
  • 103,982
  • 12
  • 207
  • 218
  • Thanks, just tried this and am getting a similar error for all of them: "Property 'expirationViewHeightConstraint' not found on object of type 'UIViewController *'" – thedonniew Nov 15 '14 at 00:41
  • @D_W, It sounds like self.parentViewController isn't returning a InviteContactsViewController then. In the inviteAllAction: method, log self.parentViewController, and see what that gives you. – rdelmar Nov 15 '14 at 00:44
  • NSLog (@"%@" ,self.parentViewController); logs 2014-11-14 17:15:04.709 SampleProject[28411:1689414] – thedonniew Nov 15 '14 at 01:17
  • @D_W, Oh, sorry, I should have created a variable for the InviteContactsViewController so the complier would know what type self.parentViewController was. Also, make sure you import InviteContactsViewController.h into the InviteesTableViewController.m file. I've updated my answer. – rdelmar Nov 15 '14 at 01:48