MKMapView不断重置回世界视图

发布于 2024-10-20 21:30:52 字数 2342 浏览 2 评论 0原文

原始问题如下。 我解决了我的问题。我为表格视图和地图视图设置了相同的视图控制器。我的所有mapView init 都在viewDidLoad 中。这两者一起给我带来了以下问题:

1)当标签栏控制器以模态方式呈现并且我收到内存警告时,调用了viewDidUnload(我之前什么也没做)。当模态选项卡栏控制器被关闭并调用 viewDidLoad 时,它会重置我的 mapView。 2)当标签栏控制器以模态方式呈现并且我在使用视图控制器移动到选项卡然后切换到该选项卡之前收到内存警告时,我的viewDidLoad在我的mapView的控制器中被调用,它重置了我的地图,并且它没有重置表属性,以便我的 tableView 无法工作。

非常感谢 Anomie 帮助我调试这个问题,并更好地了解这些部分是如何连接的。现在一切都很好,我的应用程序有更好的设计。

原问题: 我有一个具有地图视图的应用程序,当我以模态方式呈现选项卡栏视图控制器时,每隔一段时间,当我取消模态视图控制器时,地图就会重置回世界视图,就像按下了重置按钮一样。一些细节:

  • 这在模拟器上永远不会发生
  • 我注意到在我的设备上发生这种情况时出现内存警告
  • 当我的应用程序中发生内存警告时我没有执行任何操作,所以没有任何内容应该触及地图视图

此外,我的模态视图中的表格视图控制器有时也会显示空白(在内存警告期间)。当它显示为空白时,通常调用来获取行数、节数和数据数的方法根本不会被调用。取消模态视图控制器并重新打开它后,数据很好,因此数据不会被删除。

我用来创建选项卡栏控制器、选择器和表格的代码:

ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init];
picker.peoplePickerDelegate = self;
// Display only a person's address(es)
NSArray *displayedItems = [NSArray arrayWithObjects:[NSNumber numberWithInt:kABPersonAddressProperty],
                                                           nil];  
picker.displayedProperties = displayedItems;

UITabBarItem *peoplePickerTabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemContacts tag:0];
picker.tabBarItem = peoplePickerTabBarItem;

UITableViewController *tvc = [[UITableViewController alloc] initWithStyle:UITableViewStylePlain];
tvc.tableView.delegate = self;
tvc.tableView.dataSource = self;
UINavigationController *nvc = [[UINavigationController alloc] initWithRootViewController:tvc];
UIBarButtonItem *uibbiCancel = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonItemStylePlain target:self action:@selector(cancelTable)];
tvc.navigationItem.rightBarButtonItem = uibbiCancel;
tvc.title = @"Recents";
UITabBarItem *nvcTabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemRecents tag:2];
nvc.tabBarItem = nvcTabBarItem;

tbc = [[UITabBarController alloc] init];

NSArray *sections = [[NSArray alloc] initWithObjects:picker, nvc, nil];

[tbc setViewControllers:sections];

[self presentModalViewController:tbc animated:YES];
[nvcTabBarItem release];
[uibbiCancel release];
[tvc release];
[peoplePickerTabBarItem release];
[picker release];
[nvc release];
[sections release];
[tbc release];

Original question is below.
I fixed my problem. I had the same view controller set for the tableview and the mapview. AND I had all of my mapView init in viewDidLoad. These both together made the following problems for me:

1) When the tab bar controller was presented modally and I received a memory warning the viewDidUnload was called (which I did nothing in before). When the modal tab bar controller was dismissed and viewDidLoad was called, it reset my mapView.
2) When the tab bar controller was presented modally and I received a memory warning before moving to the tab with the view controller and then switched to that tab, my viewDidLoad was called in my mapView's controller which reset my map, AND it didn't reset the tables properties so that my tableView would not work.

Much thanks to Anomie for helping me debug this issue, and gain a better understanding of how these pieces are all connected. Things work great now, and my App has a better design.

Original Question:
I have an application that has a mapview and when I present a tab bar view controller modally, every once in a while when I cancel the modal view controller the map resets back to the world view like a reset button was pressed on it. A few details:

  • This NEVER happens on the simulator
  • I notice memory warnings around the time this happens on my device
  • I do not do anything when memory warnings happen in my app, so nothing should have touched the mapview

Also, my tableview in my modal view controller sometimes shows up blank as well (aroudn the time of memory warnings). When it comes up blank, the methods normally called to get the number of rows, sections, and data are not called at all. Upon cancelling the modal view controller and re-opening it, the data is there fine, so the data is not getting erased..

Code I used to create the tab bar controller, picker and table:

ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init];
picker.peoplePickerDelegate = self;
// Display only a person's address(es)
NSArray *displayedItems = [NSArray arrayWithObjects:[NSNumber numberWithInt:kABPersonAddressProperty],
                                                           nil];  
picker.displayedProperties = displayedItems;

UITabBarItem *peoplePickerTabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemContacts tag:0];
picker.tabBarItem = peoplePickerTabBarItem;

UITableViewController *tvc = [[UITableViewController alloc] initWithStyle:UITableViewStylePlain];
tvc.tableView.delegate = self;
tvc.tableView.dataSource = self;
UINavigationController *nvc = [[UINavigationController alloc] initWithRootViewController:tvc];
UIBarButtonItem *uibbiCancel = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonItemStylePlain target:self action:@selector(cancelTable)];
tvc.navigationItem.rightBarButtonItem = uibbiCancel;
tvc.title = @"Recents";
UITabBarItem *nvcTabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemRecents tag:2];
nvc.tabBarItem = nvcTabBarItem;

tbc = [[UITabBarController alloc] init];

NSArray *sections = [[NSArray alloc] initWithObjects:picker, nvc, nil];

[tbc setViewControllers:sections];

[self presentModalViewController:tbc animated:YES];
[nvcTabBarItem release];
[uibbiCancel release];
[tvc release];
[peoplePickerTabBarItem release];
[picker release];
[nvc release];
[sections release];
[tbc release];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吻泪 2024-10-27 21:30:52

对于您的地图视图,可能发生的情况是:当 UIViewController 未主动显示时(例如,它位于 UITabViewController 的非显示选项卡中,而不是 UINavigationController 中的可见视图,或者被模态呈现的视图隐藏)控制器),如果发生内存通知,它可能会释放其视图。然后它将在需要时重新创建视图。

对于你的表格视图,我不确定。您是否在创建包含视图控制器的代码中将数据源和委托分配给表视图,而不是在视图控制器的 viewDidLoad 方法中这样做?

For your map view, what is probably happening is this: When a UIViewController is not being actively displayed (e.g. it is in a non-displayed tab of a UITabViewController, not the visible view in a UINavigationController, or is hidden by a modally presented view controller), it may release its view if a memory notification occurs. It will then recreate the view when needed.

For your table view, I'm not sure. Are you assigning the data source and delegate to the table view in the code that creates the containing view controller, rather than doing so in the view controller's viewDidLoad method?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文