2014-02-21 37 views
0

这个问题让我困惑了几天。无法隐藏编程式创建的UIButton

我有2个UIButtons,一个是使用Storyboard创建的,另一个是以编程方式创建的。

当我按下其中一个按钮时,我被移动到隐藏编程构建和故事板按钮的方法,但只有故事板按钮消失,而编程构建的按钮仍然存在。我试过的不仅是隐藏,还改变了编程式构建按钮的框架,但无济于事。 下面是使用的代码:

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

为什么只有故事板按钮符合用户界面的变化?有什么我失踪?我已经尝试取消AutoLayout,但这并没有改变一件事。

回答

3

问题是在您的方法中传递参数(customizeButton:(UIButton *)but)。

更改您的代码,并核对

but = [UIButton buttonWithType:UIButtonTypeCustom];

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

还是应该在方法

通过地址customizeButton:(UIButton **)but并同时呼吁使用&self.catBut

+0

您解决了问题!非常感谢。 – Yarneo

+0

我有一种预感,它与UIButton指针的传递有关。 – Yarneo

+0

好,你的问题解决:) – nanjunda

0

它是BOOL类型,而不是bool类型。

试试这个

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

我可以看到这个属性catBut,但我上无法看到的创建对于这一点,以及我没有给出口了。检查这个。你已经尝试了一些东西,但只是简单地通过跟随线修复

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

谢谢iMani,但是那不是问题。我相应地修改代码。 – Yarneo

+0

@Yarneo查看我的更新回答 – Mani