【问题标题】:How to make a UIActivityIndicatorView in a UIToolbar tappable如何使 UIToolbar 中的 UIActivityIndi​​catorView 可点击
【发布时间】:2012-08-31 10:23:57
【问题描述】:

我正在使用此代码将 UIActivityIndi​​catorView 插入到我的工具栏

-(void)addActivityIndicatorToToolbar {
    UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
    activityIndicator.userInteractionEnabled = YES;
    activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhite;
    [activityIndicator startAnimating];

    UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView:activityIndicator];
    barButton.target = self;
    barButton.action = @selector(playButtonPressed:);

    NSMutableArray *toolbarItemsMutable = [self.toolbar.items mutableCopy];
    [toolbarItemsMutable replaceObjectAtIndex:0 withObject:barButton];
    self.toolbar.items = toolbarItemsMutable;  
}

但是,当我点击 UIActivityIndi​​catorView 时,未执行操作 (playButtonPressed)。

我该如何纠正这个问题?

【问题讨论】:

    标签: objective-c uitoolbar uiactivityindicatorview


    【解决方案1】:

    您似乎更有可能想要一个带有活动指示器的按钮。您可以按照this post 中的描述创建一个带有自定义视图的按钮来做到这一点。然后您可以正常设置此按钮的操作,并且您可能希望保留对活动指示器的引用以启动和停止它。

    【讨论】:

    • 我使用故事板将工具栏添加到我的视图中。它没有属性 rightBarButtonItem...
    • 在更清楚地阅读您的帖子时,我可以看到您将活动指示器设置为按钮内的自定义视图,为误读道歉。按钮是否显示活动指示器并在您按下时按下?它可能是选择器名称。方法-(void)playButtonPressed:(id)sender是定义在同一个类中,还是有-(void)playButtonPressed
    • 按钮没有按下它。我在同一个类中定义了 -(void)playButtonPressed:(id)sender (所以选择器末尾的冒号是正确的)同时我通过在 activityIndi​​catorView 顶部添加一个额外的视图来实现一个穷人的解决方案手势识别器。一个更优雅的解决方案会很好。
    • 尝试设置activityIndicator.userInteractionEnabled = NO 而不是YES。可能是活动指示器正在吃水龙头。
    【解决方案2】:

    我最终实现了一个穷人的解决方案,通过在 activityIndi​​catorView 的顶部添加一个额外的视图和一个手势识别器。

    【讨论】:

      【解决方案3】:

      这是一个很老的问题,但为什么不直接将 UITapGestureRecognizer 实例添加到您的 UIActivityIndicatorView 实例中呢? (在 iOS 8.2 上运行良好,我还没有在以前的版本上测试过)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多