gpt4 book ai didi

ios - 使用 UILabel 和 UIButton 显示用户标签

转载 作者:行者123 更新时间:2023-11-29 02:44:26 25 4
gpt4 key购买 nike

在我的应用程序中,我有一个与我想要显示的用户相对应的标签列表。标签应该在右侧有一个文本字段和一个删除标签的按钮(很像这个网站上提问时的标签!)

我只希望标签的一部分可以点击,所以我不想将 UILabel 放在 UIButton 中,而且似乎将 UIButton 添加到 UILabel 是不好的做法。创建这个对象的最佳方法是什么?我想要类似于 UITableViewCell 的东西,但不是 UITableViewCell,因为它不会显示在表格 View 中。

最佳答案

只是为了创建一个UIView子类,我们可以将它命名为TagView,给UILabelUIButton来显示,并且不要忘记创建一个委托(delegate)协议(protocol)来进行操作响应。

也许是这样。

@protocol TagViewDelegate 
@optional
- (void)didSelectedTagView:(TagView *)tagView;
- (void)didTagViewAccessoryButtonTapped:(TagView *)tagView;
@end
@interface TagView : UIView
{
UILabel *label;
UIButton *accessoryButton;
}
@property (weak, nonatomic) id delegate;
@property (readonly, nonatomic) UILabel *label;
@end

然后为标签实现一个UITapGestureRecognizer,为按钮实现一个touchUpInside事件

- (void)initUIElement {
// give it a gestureRecognizer for label to response the label did select
UITapGestureRecognizer *labelTapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didSelectedLabel:)];
[label addGestureRecognizer:labelTapGR];

[accessoryButton addTarget:self action:@selector(accessoryTapped:) forControlEvents:UIControlEventTouchUpOutside];
}

didSelectedLabel:accessoryTapped: 方法中,像这样响应 tagView 的委托(delegate)。

- (void)didSelectedLabel:(UITapGestureRecognizer *)gestureRecognizer
{
[self.delegate didSelectedTagView:self];
}

- (void)accessoryTapped:(id)sender
{
[self.delegate didTagViewAccessoryButtonTapped:self];
}

有一个图书馆你可以看看。

关于ios - 使用 UILabel 和 UIButton 显示用户标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25319923/

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