活动指示器作为注释附件

时间:2013-02-26 10:24:14

标签: objective-c xcode annotations mkmapview uiactivityindicatorview

我有一个带注释的mapView,它们有一个按钮作为右侧附件视图。如果在注释详细信息出现后立即按下按钮,则详细信息显示为空,因为没有足够的时间下载所需信息。我正在寻找的是只要有信息下载就显示一个活动指标。这是我到现在为止所拥有的:

-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation{

popButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 31, 31)];
[popButton addTarget:self action:@selector(button:) forControlEvents:UIControlEventTouchUpInside];
activityI = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 31, 31)];
[activityI startAnimating];
[popButton addSubview:activityI];
[NSThread detachNewThreadSelector:@selector(threadInfo:) toTarget:self withObject:annotation];
.
.
.
annView.rightCalloutAccessoryView = popButton;
}

-(void) threadInfo: (MKAnnotationView*) info{

while ([activityI isAnimating]) {
    if (ok ==0) {

    }
    else{

        NSLog(@"ok = %i",ok);
        popButton = (UIButton*) info.rightCalloutAccessoryView; <--error*
        popButton = [UIButton buttonWithType:UIButtonTypeInfoLight];
        [activityI removeFromSuperview];
        [activityI stopAnimating];
        ok=0;
        }
    }

}

其中“ok”=整数,其中1 =下载过程成功完成时

其中error =由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:' - [“注释类名称”rightCalloutAccessoryView]:无法识别的选择器发送到实例0x17e7fc60'

即使我注释掉按钮,activityI仍然可以动画而不会崩溃..

更新:(已注释掉的所有内容都已经过测试,无法正常工作 - 不一定是同时进行的。任何未评论的内容都是我的最后一次尝试)...

-(void) threadInfo: (MKAnnotationView*) annView{
UIButton* popButtons = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 31, 31)];
act = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 31, 31)];
[act setColor:[UIColor greenColor]];
[act startAnimating];
[popButtons addSubview:act];
while ([act isAnimating]) {
            if (ok ==0) {
        NSLog(@"ok = %i",ok);

    }
    else{

        NSLog(@"ok = %i",ok);

      // popButton = (UIButton*) annView.rightCalloutAccessoryView;
//UIButton* popButtons = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 31, 31)];
        popButtons = [UIButton buttonWithType:UIButtonTypeInfoLight];
       // UIActivityIndicatorView *currentActivityIndicator = (UIActivityIndicatorView *)[popButton viewWithTag:15];
        //currentActivityIndicator.hidden = YES;
        //[popButtons addSubview:[(UIActivityIndicatorView*) [popButton viewWithTag:15]]];
//            [(UIActivityIndicatorView *) [popButton viewWithTag:15] stopAnimating];
//            [(UIActivityIndicatorView *) [popButton viewWithTag:15] removeFromSuperview];
        annView.rightCalloutAccessoryView = popButtons;
        //[popButton addSubview:popButtons];
      //  NSLog(@"view = %@",[popButton viewWithTag:15]);
        //[act stopAnimating];

//            [currentActivityIndicator stopAnimating];
//            [currentActivityIndicator removeFromSuperview];

      //  popButton = [UIButton buttonWithType:UIButtonTypeInfoLight];

    //ok=0;
}
}

}

1 个答案:

答案 0 :(得分:0)

您在activityI中使用的threadInfo不一定是附加到正在使用的annotationView的infoviewForAnnotation变量)。它只是通过调用info创建的最后一个,可以随时调用任何注释。您需要获取viewForAnnotation上的活动指示器。这是一些启动你的伪代码。获取accessoryView,然后获取您在popButton = (UIButton*) info.rightCalloutAccessoryView; UIActivityIndicator *currentActivityIndicator = (UIActivityIndicator *)[popButton **Get the right subview here**];

中添加的活动指示符子视图
info.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeInfoLight];

另一种选择是直接将附件视图设置为信息按钮

{{1}}

您将popButton指向rightCalloutAccessoryView,然后您将其指向一个新按钮,但这并没有改变rightCalloutAccessoryView