2012-11-25 35 views
0

如何在uitextview使用google api加載當前位置時停止UIActivityIndi​​catorView。當uitextview使用google api加載當前位置時停止活動指示

這裏是我的代碼:

@implementation ViewController 
@synthesize loctxt; 

- (void)viewDidLoad { 

     loctxt=[[UITextView alloc]initWithFrame:CGRectMake(10, 50, 220, 30)]; 
     loctxt.backgroundColor=[UIColor whiteColor]; 
     loctxt.delegate=self; 
     [self.view addSubview:loctxt]; 

     locbtn=[[UIButton alloc]initWithFrame:CGRectMake(240, 50, 40, 30)]; 
     [locbtn addTarget:self action:@selector(clickToGetLocation)forControlEvents:UIControlEventTouchUpInside]; 
     locbtn.backgroundColor=[UIColor grayColor]; 
     [locbtn setImage:[UIImage imageNamed:@"sst14.gif"] forState:UIControlStateNormal]; 
     [self.view addSubview:locbtn]; 

     [super viewDidLoad]; 
     // Do any additional setup after loading the view, typically from a nib. 
    } 

    - (void)clickToGetLocation { 

     spinner = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0,100, 20, 20)]; 
     [spinner setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleGray]; 
     [self.view addSubview:spinner]; 

     **[spinner startAnimating];** 


     AppDelegate *delegate=(AppDelegate *)[[UIApplication sharedApplication]delegate]; 
     NSString *urlString = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%f,%f&output=csv",delegate.latitude, delegate.longitude]; 

     NSError* error; 
     NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString] encoding:NSASCIIStringEncoding error:&error]; 
     NSLog(@"%@",locationString); 

     locationString = [locationString stringByReplacingOccurrencesOfString:@"\"" withString:@""]; 
     **self.loctxt.text=[locationString substringFromIndex:6];** 
    //if i use code [spinner stopanimating]; here then the indicator does not appear  

}
+1

請多一點的描述。 –

回答

3

的問題是,您嘗試啓動微調,從互聯網上加載數據,然後停止微調都在主線程和所有在同一個方法。在主線程上做同步網絡是不好的。

您需要啓動微調器,然後在後臺執行網絡訪問。當這完成時,你停止在主線程上的微調器。

GCD對此非常好。事情是這樣的:

- (void)clickToGetLocation { 
    spinner = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0,100, 20, 20)]; 
    [spinner setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleGray]; 
    [self.view addSubview:spinner]; 
    [spinner startAnimating]; 

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 
     AppDelegate *delegate=(AppDelegate *)[[UIApplication sharedApplication]delegate]; 

     NSString *urlString = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%f,%f&output=csv",delegate.latitude, delegate.longitude]; 

     NSError* error; 
     NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString] encoding:NSASCIIStringEncoding error:&error]; 
     NSLog(@"%@",locationString); 

     locationString = [locationString stringByReplacingOccurrencesOfString:@"\"" withString:@""]; 

     dispatch_async(dispatch_get_main_queue(), ^{ 
      self.loctxt.text = [locationString substringFromIndex:6]; 
      [spinner stopAnimating]; 
     }); 
    }); 
} 
+0

非常感謝 –

1

,你可以嘗試這個spinner.hidesWhenStopped = YES

相關問題