我正在创建一个简单的 UIViewController
类别,它增加了在 View 顶部显示一个按钮以显示聊天窗口的可能性。
@interface UIViewController (ChatButton)
- (void)showChatButtonFromTop;
- (void)showChatButtonFromBottom;
- (void)hideButton;
@end
现在在我创建一个按钮并显示它的方法中:
- (void)showChatButtonFromTop
{
UIButton* chatBtn = [self constructButtonWithWidth:buttonAxisSize X:buttonX Y:buttonY];
[self.view addSubview:chatBtn];
}
但我还需要隐藏按钮:
- (void)hideButton
{
// confusion!
}
我如何获得该按钮?类别不允许属性,那么如何存储引用?
最佳答案
添加按钮查看时,给按钮添加标签
chatBtn.tag = 1234;
[self.view addSubview:chatBtn];
当你试图隐藏按钮访问它的标签时
- (void)hideButton
{
UIButton *chatBtn = (UIButton *)[self.view viewWithTag:1234];
[chatBtn setHidden:YES];
}
关于ios - 访问 UIViewController 类别中的 UIButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31252259/