gpt4 book ai didi

objective-c - 调用 popToRootViewController 动画导致崩溃。我应该怎么做?

转载 作者:行者123 更新时间:2023-11-29 04:18:27 28 4
gpt4 key购买 nike

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

我有选项卡 Controller 第一个选项卡有一个导航 Controller Storyboard上的第一个 View Controller ,并且有一个自身的序列板循环直到获得所有测量值,然后转向不同的 Controller

问题是:如果用户更改他们在设置选项卡中进行的测量,第一个选项卡需要完全重新加载,就像应用程序刚刚启动一样,清除整个导航堆栈等。

目前,选项卡 Controller 在测量选项卡中的导航 Controller 上调用 popToRootViewControllerAnimated,但这会导致崩溃。每个屏幕都有一个 slider 控件,并且在已删除的 View 上调用 titleForRow:forComponent: 导致其崩溃。

我做错了什么?!

这是标签栏 Controller 代码

//  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:

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

最佳答案

在您的代码中:

    [[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]

关于objective-c - 调用 popToRootViewController 动画导致崩溃。我应该怎么做?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13341265/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com