访问 UIViewController 类别中的 UIButton
Access UIButton in a category of a UIViewController
我正在创建一个 UIViewController
的简单类别,它增加了在视图顶部显示按钮以显示聊天的可能性 window。
@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];
}
我正在创建一个 UIViewController
的简单类别,它增加了在视图顶部显示按钮以显示聊天的可能性 window。
@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];
}