草庐IT

ios - UINavigationController pushViewController 推送 Controller 然后 "automatically"关闭它

coder 2024-01-24 原文

在我的 HomeViewController 的 viewDidAppear 方法中,我有以下代码:

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    BOOL didRunBefore = [defaults boolForKey:@"didRunBefore"];

    if (!didRunBefore) {
        // check to see if children already exist (previous user)
        NSArray *children = [CoreDataHelper getObjectsForEntity:NSStringFromClass([Child class]) withSortKey:@"name" andSortAscending:YES andContext:self.managedObjectContext];

        if (children.count == 0) {
            // send user to create fist child
            UIStoryboard *storyboard = self.storyboard;

            ChildEditTableViewController *editController = [storyboard instantiateViewControllerWithIdentifier:@"ChildEditControllerID"];

            NSManagedObjectContext *newContext = [[NSManagedObjectContext alloc] init];
            newContext.parentContext = self.managedObjectContext;
            editController.managedObjectContext = newContext;

            [self.navigationController pushViewController:editController animated:NO];
        }
    }
}

这是 ChildEditTableViewController 中 ViewDidLoad 的代码:

- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"Child Edit controller loaded");

    self.availablePicker.delegate = self;
    self.bankedPicker.delegate = self;

    self.carryOverCellIsShowing = NO;

    self.isNewChild = self.child == nil;

    self.imageButton.layer.cornerRadius = self.imageButton.frame.size.width/2;
    self.imageButton.layer.masksToBounds = YES;
    [[self.imageButton imageView] setContentMode: UIViewContentModeScaleAspectFill];

    if (self.isNewChild) {

        // check to see if it's user's first time running app
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        BOOL didRunBefore = [defaults boolForKey:@"didRunBefore"];

        if (!didRunBefore) {
            // hide Home back button
            [self.navigationItem setHidesBackButton:YES];

            // update didRunBefore to yes
            [defaults setBool:YES forKey:@"didRunBefore"];
            [defaults synchronize];
        }

        self.child = [NSEntityDescription insertNewObjectForEntityForName:@"Child" inManagedObjectContext:self.managedObjectContext];
        self.title = NSLocalizedString(@"Add New", @"Add New Title");
    }
    else {
        if (self.child.profileImage != nil) {

            [self.imageButton setImage:[UIImage squaredImageFromImage:[UIImage imageWithData:self.child.profileImage] scaledToSize:self.imageButton.frame.size.height] forState:UIControlStateNormal];
        }
        self.name.text = self.child.name;

        self.autoBankSwitch.on = [self.child.autoBank boolValue];
        self.carryOverSwitch.on = ![self.child.resetDailyTotal boolValue];

        [self setCarryOverSwitchVisibility:self.autoBankSwitch];
    }

    // This will remove extra separators from tableview
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
}

就我在屏幕上看到 ChildEditTableViewController 加载而言,该代码工作正常,但随后它会自动弹出回主 Controller 。我检查了子 Controller 中的代码,唯一一次弹出 Controller 是在用户点击按钮时。

这是我弹出 Controller 的 Save IBAction:

- (IBAction)save:(UIBarButtonItem *)sender {

    [self saveToDB:sender];

    [self.navigationController popViewControllerAnimated:YES];
}

如果我改用 self.navigationController setViewControllers,这不会发生并且 ChildEditTableViewController 保持加载在屏幕上,但单击“保存”按钮(弹出 View Controller )什么都不做。

有什么想法吗? (谢谢!)

**** 编辑 ***** 我注意到它在 iOS 7.1 和 7.03 中运行良好。从 UI 的角度来看,唯一的区别是下面的这段代码:

// enable handling of push notifications
if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {
    // use registerUserNotificationSettings
    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIRemoteNotificationTypeBadge
                                                                                         |UIRemoteNotificationTypeSound
                                                                                         |UIRemoteNotificationTypeAlert) categories:nil];
    [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
} else {
    // use registerForRemoteNotifications
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];
}

在 iOS 8 中,我收到允许在模拟器上发送通知的提示(这在之前的版本中无法在 sim 上运行)。单击确定后,EditChild Controller 将弹出。所以我注释掉了应用程序委托(delegate)中的代码,并且 Controller 保持加载状态,就像在 iOS 7 中一样。

****** 编辑 ****** 下面是 ApplicationDidBecomeActive 代码

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    NSLog(@"%s", __PRETTY_FUNCTION__);
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.


    // move user to home screen so app is locked each time they open it (but not on first use)

    SWRevealViewController* revealController = (SWRevealViewController*)self.window.rootViewController;
    UINavigationController *nav = (UINavigationController *)revealController.frontViewController;

    [nav popToRootViewControllerAnimated:YES];
}

所以这是罪魁祸首。出于某种疯狂的原因,在用户单击通知注册警报上的“接受”后,立即再次调用此代码。

最佳答案

我认为您的应用程序委托(delegate)中的回调正在对您的 View / Controller 层次结构执行某些操作。我会在您的应用程序委托(delegate)方法 applicationWillResignActive:、applicationDidBecomeActive: 中添加一些断点,看看它们是否在做任何事情。

关于ios - UINavigationController pushViewController 推送 Controller 然后 "automatically"关闭它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26499895/

有关ios - UINavigationController pushViewController 推送 Controller 然后 "automatically"关闭它的更多相关文章

随机推荐