该应用程序用于进行身体测量。用户可以说我要测量:腿,胳膊和脖子,在“设置”选项卡和主选项卡中,有一个视图,可以循环显示每次测量。
这样实现:
我有标签控制器
第一个标签有一个导航控制器
情节提要上的第一个视图控制器,本身具有一个segue
电路板不断循环,直到完成所有测量,然后选择另一个控制器
问题是:如果用户在“设置”选项卡中更改了要进行的测量,则第一个选项卡需要完全重新加载,就好像该应用程序刚刚启动,清理整个导航堆栈等。
目前,标签控制器在“测量”标签中的导航控制器上调用popToRootViewControllerAnimated,但这会导致崩溃。每个屏幕都有一个滑块控件,并在已删除的视图上调用titleForRow:forComponent:导致其崩溃。
我究竟做错了什么?!
这是标签栏控制器代码
// TabBarController.m
//
#import "TabBarController.h"
#import "TodaysMeasurementObject.h"
#import "AppDelegateProtocol.h"
#import "AddMeasurementViewController.h"
#import "ReadPerson.h"
#import "AppDelegate.h"
@interface TabBarController () <UITabBarControllerDelegate>
@end
@implementation TabBarController
bool resetWizardView = false;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.delegate = self;
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:)
name:UIDeviceOrientationDidChangeNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(measurementsSettingsUpdated:) name:@"MeasurementsSettingsUpdated" object:nil];
}
- (void) measurementsSettingsUpdated:(NSNotification *) notification
{
// UINavigationController *navigationController = [self.viewControllers objectAtIndex:0];
// AddMeasurementViewController *addMeasurement = [[AddMeasurementViewController alloc] init];
// [navigationController setViewControllers: [[NSArray alloc] initWithObjects:addMeasurement, nil]];
resetWizardView = YES;
}
- (void) viewDidAppear:(BOOL)animated
{
if (![ReadPerson userHasRecords]) {
[self setSelectedIndex:3];
}
}
- (void)orientationChanged:(NSNotification *)notification
{
// We must add a delay here, otherwise we'll swap in the new view
// too quickly and we'll get an animation glitch
[self performSelector:@selector(showGraphs) withObject:nil afterDelay:0];
}
- (void)showGraphs
{
UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
if (deviceOrientation == UIDeviceOrientationLandscapeLeft && !isShowingLandscapeView)
{
[self performSegueWithIdentifier: @"toGraph" sender: self];
isShowingLandscapeView = YES;
}
else if (deviceOrientation != UIDeviceOrientationLandscapeLeft && isShowingLandscapeView)
{
[self dismissModalViewControllerAnimated:YES];
isShowingLandscapeView = NO;
}
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if(interfaceOrientation == UIInterfaceOrientationLandscapeRight)
{
[self performSegueWithIdentifier: @"toGraph" sender: self];
}
return false;
}
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
int tbi = tabBarController.selectedIndex;
if (tbi == 0) {
[[viewController view] setNeedsDisplay];
if (resetWizardView) {
[(UINavigationController*)[self.viewControllers objectAtIndex:0] popToRootViewControllerAnimated: NO]; // ******* POP CALLED HERE ******
resetWizardView = false;
}
}
}
- (TodaysMeasurementObject*) theAppDataObject
{
id<AppDelegateProtocol> theDelegate = (id<AppDelegateProtocol>) [UIApplication sharedApplication].delegate;
TodaysMeasurementObject* theDataObject;
theDataObject = (TodaysMeasurementObject*) theDelegate.theAppDataObject;
return theDataObject;
}
- (BOOL)shouldAutorotate {
return NO;
}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}
@end
更新
- (void) measurementsSettingsUpdated:(NSNotification *) notification
{
NSMutableArray *viewControllers = [[NSMutableArray alloc] initWithArray: self.viewControllers];
UINavigationController *navigationController = [viewControllers objectAtIndex:0];
AddMeasurementViewController *addMeasurement = [[AddMeasurementViewController alloc] init];
[navigationController setViewControllers: [[NSArray alloc] initWithObjects:addMeasurement, nil]];
[viewControllers setObject:navigationController atIndexedSubscript:0];
self.viewControllers = viewControllers;
}
并从中删除了代码
-tabBarController:didSelectViewController:
但仍然是相同的错误。我认为问题在于删除视图后,它正在尝试获取幻灯片控件的值。但是视图的某些部分必须仍然活着……?无论如何要杀死它?还是活着呢?
最佳答案
在您的代码中:
[[viewController view] setNeedsDisplay];
if (resetWizardView) {
[(UINavigationController*)[self.viewControllers objectAtIndex:0] popToRootViewControllerAnimated: NO]; // ******* POP CALLED HERE ******
要点:
当
setNeedsDisplay
时,您可能只需要resetWizardView == NO
。我认为您根本不需要告诉您真相。我认为您想在最后一行中定位
viewController
而不是[self.viewControllers objectAtIndex:0]
。[(UINavigationController*)viewController popToRootViewControllerAnimated:NO]
关于objective-c - 调用popToRootViewControllerAnimated导致崩溃。我应该怎么做?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13341265/