简体   繁体   English

无法隐藏以编程方式创建的UIButton

[英]Cannot hide programmatically created UIButton

This issue is baffling me for a few days now. 这个问题困扰了我几天。

I have 2 UIButtons, one created using the Storyboard and one created programmatically. 我有2个UIButton,一个是使用Storyboard创建的,另一个是以编程方式创建的。

When I press one of the buttons, I am moved to a method that hides both the programatically built and storyboard buttons, but only the storyboard button disappears, whereas the programmatically built button remains. 当我按下其中一个按钮时,我转到了一种方法,该方法同时隐藏了以编程方式构建的和情节提要板按钮,但是只有情节提要板按钮消失了,而以编程方式构建的按钮仍然保留。 I have tried not only hiding but also changing the frame of the programmatically built button, but to no avail. 我不仅尝试隐藏而且还更改了以编程方式构建的按钮的框架,但无济于事。 Here is the code used: 这是使用的代码:

@property (strong, nonatomic) UIButton *catBut;
@property (weak, nonatomic) IBOutlet UIButton *whenButton;
....
....
-(void)viewDidLoad {
....
    [self customizeButton:self.catBut withFrame:CGRectMake(165, 113, 135, 50)
          andAction:@selector(selectedCat) andColor:[UIColor belizeHoleColor] 
          andTag:2 andImage:@"coffee-64.png" andText:@"Cafes" andAlignmentLeft:YES];
....
}

- (void)customizeButton:(UIButton *)but withFrame:(CGRect)rect andAction:(SEL)action
                        andColor:(UIColor *)col andTag:(NSUInteger)tag 
                        andImage:(NSString *)imageName 
                        andText:(NSString *)buttonText 
                        andAlignmentLeft:(BOOL)alignLeft {
    but = [UIButton buttonWithType:UIButtonTypeCustom];
    [but setFrame:rect];
    [but setTitle:buttonText forState:UIControlStateNormal];
    but.tag = tag;
    but.titleLabel.numberOfLines = 0;
    but.titleLabel.font = [UIFont boldFlatFontOfSize:18]; 
    [but setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    CGFloat spacing = 10; // the amount of spacing to appear between image and title
    but.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    but.imageEdgeInsets = UIEdgeInsetsMake(0, spacing, 0, 0);
    but.titleEdgeInsets = UIEdgeInsetsMake(0, spacing*2, 0, 0);
    [but setTitleColor:[UIColor cloudsColor] forState:UIControlStateNormal];
    [but setTitleColor:[UIColor cloudsColor] forState:UIControlStateHighlighted];
    [but addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
    [self.buttonsView addSubview:but];
}
....
- (void)selectedCat {
    self.catBut.hidden = YES;
    self.whenButton.hidden = YES;
}

Why is only the Storyboard button compliant to UI changes? 为什么只有情节提要按钮符合UI更改? Is there something I am missing? 我有什么想念的吗? I have tried cancelling the AutoLayout but that didn't change a thing. 我曾尝试取消自动版式设置,但这并没有改变任何事情。

The issue is with the passing for argument in your method ( customizeButton:(UIButton *)but ). 问题出在方法中传递for参数customizeButton:(UIButton *)but )。

Change your code to this and check 将您的代码更改为此并检查

but = [UIButton buttonWithType:UIButtonTypeCustom];

to

   self.catBut = [UIButton buttonWithType:UIButtonTypeCustom];
......
[self.buttonsView addSubview:self.catBut];

OR You should pass the address customizeButton:(UIButton **)but in the method 或者您应该在方法中传递地址customizeButton:(UIButton **)but

and while calling use &self.catBut 并在调用use &self.catBut

It is BOOL type, not an bool type. 它是布尔类型,不是bool类型。

try this 尝试这个

self.catBut.hidden = YES;
self.whenButton.hidden = YES;

I can see this property catBut , But I cann't see creation for this, as well I didn't give Outlet too. 我可以看到此属性catBut ,但是我看不到为此创建的内容,也没有提供Outlet。 Check this. 检查一下。 You've tried something but simply fix by following line. 您已经尝试了一些方法,但是只需按照以下步骤进行修复即可。

[self.buttonsView addSubview:but];
self.catBut = but;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM