【问题标题】:Find if a uibutton exists as a subview in an uibutton查找 uibutton 是否作为 uibutton 中的子视图存在
【发布时间】:2022-01-22 21:56:50
【问题描述】:

我有意见。在那个视图里面我有一个uibutton。在 uibutton 内,我使用以下代码添加了另一个较小的 uibutton。

self.badgeIndicatorView = [[UIButton alloc] initWithFrame:CGRectMake(self.friendsButton.frame.size.width-15, 5, 10, 10)];
self.badgeIndicatorView.backgroundColor = [UIColor redColor];
BOOL doesContain = [self.friendsButton.subviews containsObject:self.badgeIndicatorView];
 
if(!doesContain){
   [self.friendsButton addSubview:self.badgeIndicatorView];
}

doesContain 始终为 NO。 containsObject 似乎不起作用。我想检查名为badgeIndicatorView 的uibutton 是否已经存在于friendsButton 中。我错过了什么?任何帮助表示赞赏。

【问题讨论】:

  • 你试过这个if(!doesContain){ self.badgeIndicatorView = [[UIButton alloc] initWithFrame:CGRectMake(self.friendsButton.frame.size.width-15, 5, 10, 10)]; [self.friendsButton addSubview:self.badgeIndicatorView]; }

标签: ios objective-c


【解决方案1】:

使用此代码:

// create a NEW button
self.badgeIndicatorView = [[UIButton alloc] initWithFrame:CGRectMake(self.friendsButton.frame.size.width-15, 5, 10, 10)];
self.badgeIndicatorView.backgroundColor = [UIColor redColor];

// friendsButton CANNOT contain the button you just created
BOOL doesContain = [self.friendsButton.subviews containsObject:self.badgeIndicatorView];

你会想检查friendsButton是否已经有一个子视图按钮。

附带说明,更好的方法是创建一个子类,在 init 上添加 badgeIndicatorView 按钮并将其设置为 hidden。然后,根据需要显示或隐藏它。


编辑

这一行:

self.badgeIndicatorView = [[UIButton alloc] initWithFrame:CGRectMake(20, 10, 100, 40)];

创建一个NEW按钮并分配它self.badgeIndicatorView

如果一个按钮已被创建并分配给self.badgeIndicatorView,则新按钮将不等于旧按钮。旧按钮仍然存在,但不再分配给self.badgeIndicatorView

查看它的简单方法...运行此代码:

self.badgeIndicatorView = [[UIButton alloc] initWithFrame:CGRectMake(20, 10, 100, 40)];

// log description of self.badgeIndicatorView
NSLog(@"1: %@", self.badgeIndicatorView.debugDescription);

self.badgeIndicatorView = [[UIButton alloc] initWithFrame:CGRectMake(20, 10, 100, 40)];

// log description of self.badgeIndicatorView
NSLog(@"2: %@", self.badgeIndicatorView.debugDescription);

self.badgeIndicatorView = [[UIButton alloc] initWithFrame:CGRectMake(20, 10, 100, 40)];

// log description of self.badgeIndicatorView
NSLog(@"3: %@", self.badgeIndicatorView.debugDescription);

调试输出将与此类似 - 请注意对象地址不同(意味着您创建了 3 个按钮):

1: <UIButton: 0x7f997310e310; frame = (20 10; 100 40); opaque = NO; layer = <CALayer: 0x600002208000>>
2: <UIButton: 0x7f9951f09be0; frame = (20 10; 100 40); opaque = NO; layer = <CALayer: 0x600002275e60>>
3: <UIButton: 0x7f99730065a0; frame = (20 10; 100 40); opaque = NO; layer = <CALayer: 0x600002262760>>

因此,分配给self.badgeIndicatorView 的新按钮实例将与已创建并添加到self.friendsButton 的按钮实例不同。

您可以通过检查 self.badgeIndicatorView 是否不是 nil 来简化事情......这意味着它已经被创建并添加了:

if (!self.badgeIndicatorView) {
    // create badgeIndicatorView and add it to self.friendsButton
} else {
    // badgeIndicatorView already exists!
}

【讨论】:

  • 它第一次运行时应该返回 NO,第二次通过代码时应该返回 YES。你什么意思我不明白你的回答...
  • @stefanosn - 查看我的答案的编辑。
【解决方案2】:

您正在为您的 badgeIndicatorView 属性分配一个新对象。现有按钮在将其添加为子视图之前不能将其作为子视图。您可以使用标签来标识视图层次结构中的特定子视图。

我假设您只尝试设置一次子视图。为此,您可以首先检查按钮中是否存在具有特定标签的视图。如果没有,您可以创建按钮,为其分配特定标签并将其添加为子视图。下一次,viewWithTag 将为您提供先前创建的对象,该对象已添加为子视图。

self.badgeIndicatorView = (UIButton *)[self.friendsButton viewWithTag:1];

if (!self.badgeIndicatorView) {
    self.badgeIndicatorView = [[UIButton alloc] initWithFrame:CGRectMake(self.friendsButton.frame.size.width-15, 5, 10, 10)];
    self.badgeIndicatorView.backgroundColor = [UIColor redColor];
    self.badgeIndicatorView.tag = 1;

    [self.friendsButton addSubview:self.badgeIndicatorView];
} 

【讨论】:

    【解决方案3】:

    我认为这可能会有所帮助,只需调整您的代码顺序即可。

    BOOL doesContain = [self.friendsButton.subviews containsObject:self.badgeIndicatorView];
     
    if(!doesContain){
       self.badgeIndicatorView = [[UIButton alloc] initWithFrame:CGRectMake(self.friendsButton.frame.size.width-15, 5, 10, 10)];
       self.badgeIndicatorView.backgroundColor = [UIColor redColor];
       [self.friendsButton addSubview:self.badgeIndicatorView];
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-20
      • 2014-04-18
      相关资源
      最近更新 更多