我有两个UIViewControllers(ViewController和SettingViewController)。
在SettingViewController中我有3个按钮,按下时应更改主ViewController中UITextView的字体。
我正在使用NSUserDefaults。但是,它只能在第一次使用或根本不起作用。
我认为问题在于,当我从设置视图返回主视图时,视图不会重新加载。
我该怎么做?
SettingViewController.swift
class SettingsViewController: UIViewController {
var handWritingOption: String?
var zapfinoOption: String?
var systemOption: String?
@IBAction func handWritingPressed(sender: AnyObject) {
handWritingOption = "1"
zapfinoOption = "0"
systemOption = "0"
NSNotificationCenter.defaultCenter().postNotificationName("load", object: nil)
NSUserDefaults.standardUserDefaults().setObject(handWritingOption, forKey: "handWritingBool")
NSUserDefaults.standardUserDefaults().setObject(zapfinoOption, forKey: "zapfinoBool")
NSUserDefaults.standardUserDefaults().setObject(systemOption, forKey: "systemBool")
NSUserDefaults.standardUserDefaults().synchronize()
}
@IBAction func zapfinoPressed(sender: AnyObject) {
handWritingOption = "0"
zapfinoOption = "1"
systemOption = "0"
NSNotificationCenter.defaultCenter().postNotificationName("load", object: nil)
NSUserDefaults.standardUserDefaults().setObject(handWritingOption, forKey: "handWritingBool")
NSUserDefaults.standardUserDefaults().setObject(zapfinoOption, forKey: "zapfinoBool")
NSUserDefaults.standardUserDefaults().setObject(systemOption, forKey: "systemBool")
}
@IBAction func systemPressed(sender: AnyObject) {
handWritingOption = "0"
zapfinoOption = "0"
systemOption = "1"
NSNotificationCenter.defaultCenter().postNotificationName("load", object: nil)
NSUserDefaults.standardUserDefaults().setObject(handWritingOption, forKey: "handWritingBool")
NSUserDefaults.standardUserDefaults().setObject(zapfinoOption, forKey: "zapfinoBool")
NSUserDefaults.standardUserDefaults().setObject(systemOption, forKey: "systemBool")
}
}
ViewController.swift
class ViewController: UIViewController {
@IBOutlet weak var noteResult: UITextView!
var handWritingOption = NSUserDefaults.standardUserDefaults().objectForKey("handWritingBool") as? String
var zapfinoOption = NSUserDefaults.standardUserDefaults().objectForKey("zapfinoBool") as? String
var systemOption = NSUserDefaults.standardUserDefaults().objectForKey("systemBool") as? String
override func viewDidLoad() {
super.viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(self, selector: "loadList:",name:"load", object: nil)
}
func loadList(notification: NSNotification){
println("Font Changed")
if handWritingOption == "1" {
noteResult.font = UIFont(name: "ThinPencilHandwriting", size: 15)
}
if zapfinoOption == "1" {
noteResult.font = UIFont(name: "Zapfino", size: 15)
}
if systemOption == "1" {
noteResult.font = UIFont(name: "Papyrus", size: 15)
}
}
}
答案 0 :(得分:1)
由于您使用的是Notification
,因此无效。如果更改设置时未在ViewController中注册loadList
的通知,则不会更改任何内容。为实现这一目标,请进行以下修改。
Step 1
:取消使用通知
Step 2
:在ViewController
Step 3
:在viewWillAppear()
中调用它,如下所示
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
// *** Load font value ***
handWritingOption = NSUserDefaults.standardUserDefaults().objectForKey("handWritingBool") as? String
zapfinoOption = NSUserDefaults.standardUserDefaults().objectForKey("zapfinoBool") as? String
systemOption = NSUserDefaults.standardUserDefaults().objectForKey("systemBool") as? String
// *** call setFonts ***
self.setFonts()
}
func setFonts()
{
if handWritingOption == "1" {
noteResult.font = UIFont(name: "ThinPencilHandwriting", size: 15)
}
if zapfinoOption == "1" {
noteResult.font = UIFont(name: "Zapfino", size: 15)
}
if systemOption == "1" {
noteResult.font = UIFont(name: "Papyrus", size: 15)
}
}
每次视图显示在屏幕上时都会设置字体。