UINavigationController pushViewController推送控制器然后“自动”解除它

时间:2014-10-22 03:31:21

标签: ios objective-c uinavigationcontroller ios8

在我的HomeViewController's 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加载,该代码就可以正常工作,但随后它会自动弹回家庭控制器。我检查了子控制器中的代码,并且我弹出控制器的唯一时间是当用户点击按钮时。

这里是我弹出控制器的保存IBAction:

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

    [self saveToDB:sender];

    [self.navigationController popViewControllerAnimated:YES];
}

如果我改为使用self.navigationController setViewControllers,则不会发生这种情况,并且ChildEditTableViewController会在屏幕上保持加载状态,但单击“保存”按钮(弹出视图控制器)并不会执行任何操作。< / p>

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

****编辑***** 我注意到它在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控制器。所以我注释掉app delegate和控制器中的代码就像在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];
}

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

1 个答案:

答案 0 :(得分:1)

我认为应用程序委托中的回调正在对视图/控制器层次结构执行某些操作。我会在你的应用程序委托方法applicationWillResignActive:,applicationDidBecomeActive中添加一些断点,看看它们是否正在做任何事情。