设置视图控制器不解雇

时间:2014-05-15 20:12:13

标签: ios objective-c uiviewcontroller uinavigationcontroller

我正在创建一个具有2个简单视图控制器的任务列表应用程序...我在第一个( HomeScreen )中嵌入了导航控制器,但是,下一个视图控制器(设置)不会被解雇。我是否必须将Home View Controller设置为根VC?然后使用popToRootVIewController方法?我一直在使用dismissViewControllerAnimated ...

设置视图控制器实施文件:

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

1 个答案:

答案 0 :(得分:2)

是的,您不应该使用dismissViewControllerAnimated:completion:,因为它只会影响其父母以模态方式呈现的视图控制器。 我同意the doc可以更明确地说明这一点,虽然这条线应该暗示这不起作用(强调我的):

  

使用模态转换样式

取消最顶层视图

所以要从UINavigationController堆栈中删除顶视图控制器,必须使用

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