0

该应用程序用于进行身体测量。用户可以说我要测量:腿、手臂和脖子,在设置选项卡和主选项卡中,有一个视图可以循环进行每次测量。
这是这样实现的:

我有选项卡控制器第一个选项卡有一个导航控制器故事板上的第一个视图控制器,并且有一个自身的 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 中删除了代码:

但仍然是同样的错误。我认为问题在于它试图在视图被删除后获取滑动控件的值。但是视图的某些部分必须仍然存在......?无论如何要杀死它?还是让它活着??

4

2 回答 2

1

在您的代码中:

    [[viewController view] setNeedsDisplay];
    if (resetWizardView) {
        [(UINavigationController*)[self.viewControllers objectAtIndex:0] popToRootViewControllerAnimated: NO]; // ******* POP CALLED HERE ******

几点:

  1. 您可能只需要setNeedsDisplaywhen resetWizardView == NO。我认为你根本不需要它来告诉你真相。

  2. 我认为你想要定位viewController而不是[self.viewControllers objectAtIndex:0]最后一行。

[(UINavigationController*)viewController popToRootViewControllerAnimated:NO]

于 2012-11-13T15:39:53.480 回答
0

最后我放弃了直接解决这个问题。相反,我在视图控制器中捕获通知,然后用一个按钮弹出一个 UIAlertView。当用户按下这个 popToRootViewControllerAnimated 时被调用。我认为这实际上也是一种更好的用户体验,因为用户会收到有关其标签内容为何发生更改的消息。

希望这可以为将来节省一些时间/压力。

于 2012-11-14T13:13:24.877 回答