【发布时间】:2012-01-10 14:20:22
【问题描述】:
我是 iphone 新手,使用 MKMapView 在我的应用程序中加载谷歌地图。但它抛出异常“MKMapView 必须在主线程上初始化。”。那么我应该在哪里初始化我的MKMapView 对象。我正在viewDidLoad() 中初始化。
提前致谢.....
【问题讨论】:
-
你真的应该提供更多细节......
标签: ios cocoa-touch mkmapview
我是 iphone 新手,使用 MKMapView 在我的应用程序中加载谷歌地图。但它抛出异常“MKMapView 必须在主线程上初始化。”。那么我应该在哪里初始化我的MKMapView 对象。我正在viewDidLoad() 中初始化。
提前致谢.....
【问题讨论】:
标签: ios cocoa-touch mkmapview
它没有在主线程上创建的原因是 2 个选项之一
performSelectorInBackground: 来创建您的视图在主线程调用performSelectorOnMainThread:执行一个函数
题外话:如果函数需要超过 1 个参数,请将其更改为 NSDictionary 并将所有设置加载到 Dictionary 中并将其传递给 eg
NSDictionary *params = ...... //load your parameters into here
[myMapView performSelectorOnMainThread:@selector(initMap:)
withObject:params
waitUntilDone:YES];
【讨论】:
使用 Swift 3,以下将确保您的函数在主线程上运行。 OperationQueue.main.addOperation{"你的 segue 或函数调用"}
【讨论】:
我收到了这个错误:
MKMapView 必须在主线程初始化
当我尝试以这种方式更改自定义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] 只刷新一个单元格。
【讨论】: