从保存按钮关闭视图控制器

时间:2016-07-10 04:39:34

标签: ios xcode swift swift2 xcode7

我在放卷/关闭视图控制器时遇到问题。

此视图控制器中的导航栏有两个按钮。左边是"放松/退出"功能正常的按钮关闭当前视图控制器并将用户返回到主设置屏幕。第二个按钮是" Save"将用户数据提交到NSUserdefaults的按钮。两个按钮都按照设计进行。虽然我想要" Save"按钮再做一件事。

我想要导航栏" Save"按钮继续保存为其设计的数据,但我也希望它能够"放松/退出"查看控制器并返回主设置视图控制器,就像"展开/退出"导航栏按钮确实。

这是我的视图控制器代码。正如您将能够看到的那样,我正在呼叫" backButtonTapped"功能来自我的" settingSaveEmailAddress"功能。我知道" backButtonTapped"功能正在被触发,因为"打印"命令在调试窗口中打印。虽然它正在保存我的数据,但它无法关闭视图控制器并退回到主设置视图控制器。

import UIKit

class SettingsViewController: UIViewController {


    @IBOutlet weak var settingEmailAddress: UITextField!
    @IBOutlet weak var settingEmailAddressLastUpdate: UILabel!

    // Constant keys for NSUserDefaults look ups
    static let settingDefaultEmailAddress = "EMAIL_ADDRESS"
    static let settingDefaultEmailAddressLastUpdated = "EMAIL_ADDRESS_LAST_UPDATED"


    //End Setting - Email Address
    @IBAction func settingSaveEmailAddress(sender: AnyObject) {

        if (settingEmailAddress.text!.characters.count > 0) {
            let prefs = NSUserDefaults.standardUserDefaults()
            prefs.setObject(settingEmailAddress.text, forKey: SettingsViewController.settingDefaultEmailAddress)
            saveTimestampEmailAddress()
        }
        dismissKeyboard()
        print(settingEmailAddress.text)
        backButtonTapped(self)




    }
    //End Setting - Email Address


    override func viewDidLoad() {
        super.viewDidLoad()

        // Uncomment these if you want to clear out the saved defaults
        //let appDomain = NSBundle.mainBundle().bundleIdentifier!
        //NSUserDefaults.standardUserDefaults().removePersistentDomainForName(appDomain)

        let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(SettingsViewController.dismissKeyboard))
        view.addGestureRecognizer(tap)

        // update labels from NSUserDefaults
        getUserPreferences()

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    // Updates the view with the user values already stored in NSUserDefaults
    func getUserPreferences() {
        let prefs = NSUserDefaults.standardUserDefaults()

        // Get Email Address
        if let email = prefs.stringForKey(SettingsViewController.settingDefaultEmailAddress) {
            settingEmailAddress.text = email
        }

        // Get the last time something was stored
        let dateFormatter = NSDateFormatter()
        dateFormatter.dateStyle = NSDateFormatterStyle.LongStyle
        dateFormatter.timeStyle = .MediumStyle
        if let lastUpdateStored = (prefs.objectForKey(SettingsViewController.settingDefaultEmailAddressLastUpdated) as? NSDate) {
            settingEmailAddressLastUpdate.text = "Last Update:" + dateFormatter.stringFromDate(lastUpdateStored)
        } else {
            settingEmailAddressLastUpdate.text = "Last Update: Never"
        }
    }


    // MARK: - Keyboard responders so the keyboard goes away when we're done editing.
    // Dismiss the keyboard when the user is finished editing.
    func dismissKeyboard(){
        // Resign the first responder status.
        view.endEditing(true)
    }

    func textFieldShouldReturn(textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        return true
    }

    // Saves the timestamp of when the user has made a change to the NSUserDefaults
    func saveTimestampEmailAddress() {
        let prefs = NSUserDefaults.standardUserDefaults()
        let timestamp = NSDate()
        prefs.setObject(timestamp, forKey: SettingsViewController.settingDefaultEmailAddressLastUpdated)
        let dateFormatter = NSDateFormatter()
        dateFormatter.dateStyle = NSDateFormatterStyle.LongStyle
        dateFormatter.timeStyle = .MediumStyle
        settingEmailAddressLastUpdate.text = "Last Update:" + dateFormatter.stringFromDate(timestamp)
    }

    func backButtonTapped(sender: AnyObject) {
        self.dismissViewControllerAnimated(true, completion: nil)
            print("Exit Triggered")
    }


}

2 个答案:

答案 0 :(得分:0)

你可以写: -

  

在Objective-C

 [self.navigationController popToRootViewControllerAnimated:YES];
  

在Swift中

 func popToRootViewControllerAnimated(_ animated: Bool) -> [UIViewController]?
如果“主要设置”viewController是根视图控制器,则在“保存”按钮方法的末尾

答案 1 :(得分:0)

我创建了这个功能,当我按“保存”时,我现在正在获得预期的功能。

  1. 保存NSUserDefault数据
  2. 关闭当前的ViewController并返回 我的主要设置视图控制器“SettingsMainTableViewController”。

    func closeVCReturnSettings() {
    for controller in self.navigationController!.viewControllers as Array {
        if controller.isKindOfClass(SettingsMainTableViewController) {
            self.navigationController?.popToViewController(controller as UIViewController, animated: true)
            break
        }
        print("Return to settings")
    }
    }