【问题标题】:UIButton may not respond to enumerateobjectsusingblock, in iPhone?UIButton 可能不响应 enumerateobjectsusingblock,在 iPhone 中?
【发布时间】:2013-01-25 09:27:30
【问题描述】:

我想在我的应用中使用 BCGenieEffect。我从 github 下载演示示例。 https://github.com/Ciechan/BCGenieEffect

在此示例中使用 xib,并且 4 个 UIButtons 与 xib 一起使用。我想在没有 xib 的情况下使用此代码,并且仅用于一个按钮。我不想拖动我的视图,这就是我删除该代码的原因。

我有一个 UIButton:

UIButton *Pop_Hidebtn = [[UIButton alloc]initWithFrame:CGRectMake(290, 5, 15, 15)];
Pop_Hidebtn.backgroundColor = [UIColor clearColor];
[Pop_Hidebtn setImage:[UIImage imageNamed:@"close.png"] forState:UIControlStateNormal];

Pop_Hidebtn.titleLabel.textColor = [UIColor blueColor];
[Pop_Hidebtn addTarget:self action:@selector(Hidden_pop:)  forControlEvents:UIControlEventTouchUpInside];  
[popup_View addSubview:Pop_Hidebtn];


-(void)Hidden_pop:(UIButton *)sender{
    [self genieToRect:sender.frame edge:BCRectEdgeBottom];
}


// I modified this and it says-> UIButton may not respond to enumerateobjectsusingblock
// And Crash 

- (void) genieToRect: (CGRect)rect edge: (BCRectEdge) edge {
    NSTimeInterval duration = 3.0;

    CGRect endRect = CGRectInset(rect, 5.0, 5.0);

    [help_Button enumerateObjectsUsingBlock:^(UIButton *button, NSUInteger idx, BOOL   *stop) { 
        help_Button.enabled = NO;
    }];


    if (self.viewIsIn) {

        [popup_View_under2 genieOutTransitionWithDuration:duration startRect:endRect      startEdge:edge completion:^{
            popup_View_under2.userInteractionEnabled = YES;

            [help_Button enumerateObjectsUsingBlock:^(UIButton *button, NSUInteger idx,  BOOL  *stop) {
                help_Button.enabled = YES;

            }];
        }];

    } else {
        popup_View_under2.userInteractionEnabled = NO;

        //UIButton may not respond to enumerateobjectsusingblock
        // Crash on this line
        [popup_View_under2 genieInTransitionWithDuration:duration destinationRect:endRect      destinationEdge:edge completion: ^{
            [help_Button enumerateObjectsUsingBlock:^(UIButton *button, NSUInteger idx,  BOOL *stop) {
                button.enabled = YES;
            }];
        }];
    }

    self.viewIsIn = ! self.viewIsIn;
}

我该如何解决这个问题?

【问题讨论】:

    标签: iphone ios objective-c xcode ipad


    【解决方案1】:
    - (void)enumerateObjectsUsingBlock:(void (^)(id obj, NSUInteger idx, BOOL *stop))block
    

    NSArrayNSOrderedSetNSSet 类的实例方法。因此,如果您在我认为是UIButton 的实例上调用该方法,它将不起作用。

    该方法旨在用于枚举集合中的对象。所以你必须以这种方式将你的按钮放在NSArray(或NSMutableArray)中:

    [myButtons enumerateObjectsUsingBlock:^(UIButton *aButton, NSUInteger idx, BOOL   *stop) { 
        aButton.enabled = NO;
    }];
    

    将为myButtons 数组中的每个对象调用该块,接收对象本身(在这种情况下,它将被强制转换为UIButton *)、所述对象的索引和指向@987654331 的指针@,您可以使用它来提前退出循环。

    请注意,既然您说您将只使用一个按钮,为什么不删除该方法调用并直接使用

    helpButton.enabled = YES;
    

    没有枚举?

    【讨论】:

    • 如果我删除 - (void)enumerateObjectsUsingBlock:(void (^)(id obj, NSUInteger idx, BOOL *stop))block 并且只使用 helpButton.enabled = YES;那么视图不会最小化。
    • 如何写这一行 [popup_View_under2 genieInTransitionWithDuration:duration destinationRect:endRect destinationEdge:edge 完成:如果我删除或评论这一行 /* [popup_View_under2 genieInTransitionWithDuration:duration destinationRect:endRect destinationEdge:edge 完成:^{ [help_Button enumerateObjectsUsingBlock:^(UIButton *button, NSUInteger idx, BOOL *stop) { button.enabled = YES; }]; }];*/
    • [popup_View_under2 genieInTransitionWithDuration:duration destinationRect:endRect destinationEdge:edge completion: ^{ helpButton.enabled = YES; }] 应该没问题。
    • 不工作,先生,您能做一个演示来更新一下吗?它将为新开发人员提供帮助。因为它很难为我解决这个问题。如果可以的话,请为我们制作一个演示并与我分享。谢谢...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多