【问题标题】:Hiding View and other objects when button selected选择按钮时隐藏视图和其他对象
【发布时间】: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


【解决方案1】:

您对 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 中的所有这些属性声明。

【讨论】:

  • 谢谢,刚刚尝试过,所有这些都得到了类似的错误:“在 'UIViewController *' 类型的对象上找不到属性 'expirationViewHeightConstraint'”
  • @D_W,听起来 self.parentViewController 没有返回 InviteContactsViewController。在inviteAllAction: 方法中,记录self.parentViewController,看看它给了你什么。
  • NSLog (@"%@" ,self.parentViewController);日志 2014-11-14 17:15:04.709 SampleProject[28411:1689414]
  • @D_W,哦,对不起,我应该为 InviteContactsViewController 创建一个变量,以便编译器知道 self.parentViewController 是什么类型。另外,确保将 InviteContactsViewController.h 导入 InviteesTableViewController.m 文件。我已经更新了我的答案。
猜你喜欢
  • 2013-01-29
  • 1970-01-01
  • 1970-01-01
  • 2019-03-19
  • 2014-11-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-29
相关资源
最近更新 更多