Skip to content

Instantly share code, notes, and snippets.

@kuju63
Created October 7, 2017 09:08
Show Gist options
  • Save kuju63/04563b5826343a2abd4a20676631b3a4 to your computer and use it in GitHub Desktop.
Save kuju63/04563b5826343a2abd4a20676631b3a4 to your computer and use it in GitHub Desktop.
iOSアプリでフォアグラウンドに移行した際に画面を更新する
class ViewController: UIViewController {
@IBOutlet weak var viewLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// NotificationCenterを使用して、フォアグラウンドに移った時に指定のメソッドを実行する
NotificationCenter.default.addObserver(self, selector: #selector(ViewController.didForeground), name: NSNotification.Name.UIApplicationWillEnterForeground, object: nil)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func didForeground() {
// この画面が表示されている時のみ実行する
if (self.isViewLoaded && self.view.window != nil) {
print("foreground show")
viewLabel?.text = "foreground"
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment