2012-01-10 106 views
2

我是iphone的新手,使用MKMapView在我的應用程序中加載谷歌地圖。但是它拋出一個異常「MKMapView必須在主線程上初始化」。那麼我應該在哪裏初始化我的MKMapView對象。我正在初始化viewDidLoad()MKMapView必須在主線程上初始化

在此先感謝.....

+0

你真的應該提供更多的細節...... – Vladimir 2012-01-10 14:21:35

回答

3

在主線程它是不是被創造的原因是2個選項

  1. 創建它生活在背景中的一個的一個觀螺紋
  2. 你在呼喚performSelectorInBackground:創建視圖

到主線程中調用執行功能performSelectorOnMainThread:

題外話:如果函數需要更多然後1點的參數,將其更改爲一個NSDictionary和所有的設置加載到字典,並在如

NSDictionary *params = ...... //load your parameters into here 

[myMapView performSelectorOnMainThread:@selector(initMap:) 
           withObject:params 
          waitUntilDone:YES]; 

又讀通過了以下FirstSecond以更好地理解iOS中的多線程

+0

我使用下面的代碼viewDidLoad中和這種觀點得到了另一種觀點認爲所謂 的MapView = [[的MKMapView頁頭] initWithFrame:方法CGRectMake(0,0,780,920); mapView.delegate = self; [self.view addSubview:mapView]; [NSThread detachNewThreadSelector:@selector(displayMyMap)toTarget:self withObject:nil]; – user1141020 2012-01-10 14:36:33

+0

你正在創建一個新的線程,嘗試改變performSelectorOnMainThread而不是detachNewThreadSelector – CStreel 2012-01-10 14:43:59

+0

感謝您的建議,但我仍然面臨同樣的問題 – user1141020 2012-01-11 07:08:13

0

使用Swift 3,以下內容將確保您的函數在主線程上運行。 OperationQueue.main.addOperation {「你賽格瑞或函數調用」}

+1

歡迎來到SO。請用反引號包裝您的代碼,以便將其顯示爲代碼。 – YakovL 2016-10-19 16:35:33

0

我得到這個錯誤:

MKMapView must be initialized on the main thread

當我試圖改變的定製UITableViewCell這樣的內容:

[self.tableView beginUpdates]; 
[self.tableView reloadRowsAtIndexPaths:@[myIndexPath] withRowAnimation:UITableViewRowAnimationNone]; 
[self.tableView endUpdates]; 

The UITableviewCell in the corresponding indexpath contained a MKMapView that I tried to change its region. 

myTabelViewCell *cell = [self.tableView cellForRowAtIndexPath:myIndexPath]; 
[cell setContentWithLatitude:currentLatitude longitude:currentLongitude]; 

使用上面的代碼而不是[self.tableView reloadData]刷新一個單元格。

相關問題