【问题标题】:Calling popToRootViewControllerAnimated causing crash. How should I be doing this?调用 popToRootViewControllerAnimated 导致崩溃。我应该怎么做?
【发布时间】:2012-11-12 09:35:46
【问题描述】:

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

我有标签控制器 第一个选项卡有一个导航控制器 故事板上的第一个视图控制器,它自己有一个 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:

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

【问题讨论】:

  • 你能把调用pop的方法连同它所在的类一起贴出来吗?
  • 一种可行的方法是在您的应用程序委托上实现一个方法,该方法执行所需的功能,然后从您当前的类中调用该方法。无论如何,应用程序委托都应该为每个选项卡设置根导航控制器,因此这可能是实现此类功能的合适位置。
  • 谢谢@WolfgangSchreurs 你能详细说明一下吗?我将如何从委托中调用 pop 方法?从那里到选项卡/视图控制器的路径是什么?

标签: objective-c ios xcode uinavigationcontroller uistoryboard


【解决方案1】:

在您的代码中:

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

几点:

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

  2. 我认为您希望在最后一行定位 viewController 而不是 [self.viewControllers objectAtIndex:0]

[(UINavigationController*)viewController popToRootViewControllerAnimated:NO]

【讨论】:

  • 嗨@MichaelKerhahan,谢谢。我已经尝试了您的建议,但仍然遇到相同的错误:[AddMeasurementViewController pickerView:titleForRow:forComponent:]: message sent to deallocated instance 0x20089e40 :((
  • 好吧,此时我建议您创建一个weak 引用,该引用显示在选项卡0 中的UINavigationController,并尝试在您的NSNotification 处理程序中直接调用popToRootViewControllerAnimated。当然它可以运行不止一次,但在后续调用中它将是无操作的。如果这不起作用,请尝试直接在UINavigationController 上设置viewControllers 数组(设置为仅包含根控制器的数组)
  • 嗨@MichaelKerhahan,感谢您一直以来的支持。恐怕这种方法不会让人高兴:(我已经更新了问题以反映我所做的事情。可能需要重新考虑整个事情......
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多