gpt4 book ai didi

ios - 访问 UIViewController 类别中的 UIButton

转载 作者:行者123 更新时间:2023-11-28 21:43:19 25 4
gpt4 key购买 nike

我正在创建一个简单的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com