2014-11-14 64 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

相關問題