简体   繁体   中英

settings view controller not dismissing

I'm making a task list application that has 2 simple view controllers... I embedded a navigation controller in the first( HomeScreen ), however, the next view controller ( Settings ) isn't dismissing when I hit my custom back button. Do I have to set up my Home View Controller as the root VC? Then use popToRootVIewController Method? I've been using dismissViewControllerAnimated ...

Settings View Controller implementation file:

//
//  AGSettingsViewController.m
//  QuickList3
//
//  Created by Alex Gartenberg on 4/11/14.
//  Copyright (c) 2014 A.Gartenberg. All rights reserved.
//

#import "AGSettingsViewController.h"

@interface AGSettingsViewController ()
@property (weak, nonatomic) IBOutlet UILabel *backgroundTitleLabel;
@property (weak, nonatomic) IBOutlet UILabel *fontTitleLabel;
@property (weak, nonatomic) IBOutlet UILabel *settingsTitleLabel;

@property (nonatomic) BOOL fontColor;
@end

@implementation AGSettingsViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    //set title of view
    self.settingsTitleLabel.text = @"SETTINGS";

    // Determine background color
    if (!BACKGROUND)
    {
        self.settingsBackgroundImage.image = [UIImage imageNamed:@"blue.png"];
    }
    else
    {
        // Retrieve image wrapped in NSData
        NSData *imageData = [[NSUserDefaults standardUserDefaults] objectForKey:BACKGROUND];
        // Unwrap the image from NSData
        UIImage *savedBackground = [UIImage imageWithData:imageData];
        // Set image as new background
        self.settingsBackgroundImage.image = savedBackground;
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Back Button Method

- (IBAction)backButtonPressed:(UIButton *)sender
{
    [self didSaveSettings];
    NSLog(@"Back Button Pressed");
}

#pragma mark - NSUserDefaults Persistance

-(void)saveSettings
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    //Select current background
    UIImage *savedBackground = self.settingsBackgroundImage.image;

    //Wrap image in an NSData Object
    NSData *imageData = UIImageJPEGRepresentation(savedBackground, 100);
    //Save Data wrapped Image Object to NSUserDefaults
    [defaults setObject:imageData forKey:BACKGROUND];

    //Font Color Bool Saved
    [defaults setBool:self.fontColor forKey:FONT_COLOR];

    //Persist Data
    [defaults synchronize];
}

#pragma mark - AGSettingsViewController Save and Dismiss

-(void)didSaveSettings
{
    [self saveSettings];
    [self dismissViewControllerAnimated:YES completion:nil];
}

@end

Yes, you shouldn't use dismissViewControllerAnimated:completion: as it will only affect view controllers presented modally by their parents. I'll agree that the doc could be clearer about this, albeit this line should have hinted that this couldn't work (emphasis mine):

The top-most view is dismissed using its modal transition style

So to remove the top view controller from the UINavigationController 's stack, you must use

-(void)didSaveSettings
{
    [self saveSettings];
    [self.navigationController popViewControllerAnimated:YES];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM