ios 在 Swift 中重新加载/更新视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28827192/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reload/Update View In Swift
提问by Ted Huinink
I'm busy making an app with an account page. I want that users can logon via that page and as soon as they have done so successfully that the page reloads to display their account information rather than the standard message stating that they have to logon to make use of the page.
我正忙于制作带有帐户页面的应用程序。我希望用户可以通过该页面登录,并且一旦他们成功登录,页面就会重新加载以显示他们的帐户信息,而不是标准消息,说明他们必须登录才能使用该页面。
However when I get sent back to the account page from logging on the view doesn't really update. So therefore I am wondering if I can't reload the view after certain buttons are pressed that can check again wether the user is logged on or not and deal accordingly.
但是,当我从登录后返回到帐户页面时,视图并没有真正更新。因此,我想知道在按下某些按钮后是否无法重新加载视图,这些按钮可以再次检查用户是否登录并进行相应处理。
采纳答案by Daij-Djan
if you want to trigger layouting or just drawing there is setNeedsLayout
and setNeedsDisplay
如果要触发布点,或只是画有setNeedsLayout
和setNeedsDisplay
There is no built-in method to reload custom data(on iOS)
没有重新加载自定义数据的内置方法(在 iOS 上)
so do a reload and inside a reload -- call setNeedsDisplay
所以重新加载并在重新加载 - 调用 setNeedsDisplay
import UIKit
protocol MyViewDelegate {
func viewString() -> String;
}
class MyView : UIView {
var myViewDelegate : MyViewDelegate?
private var str : String?
func reloadData() {
if myViewDelegate != nil {
str = myViewDelegate!.viewString()
}
self.setNeedsDisplay()
}
override func drawRect(rect: CGRect) {
UIColor.whiteColor().setFill()
UIRectFill(self.bounds)
if str != nil {
let ns = str! as NSString
ns.drawInRect(self.bounds, withAttributes: [NSForegroundColorAttributeName: UIColor.blueColor(), NSFontAttributeName: UIFont.systemFontOfSize(10)])
}
}
}
class ViewController: UIViewController, MyViewDelegate {
func viewString() -> String {
return "blabla"
}
var v : MyView!
override func viewDidLoad() {
super.viewDidLoad()
v = MyView(frame: self.view.bounds)
self.view.addSubview(v)
v.myViewDelegate = self;
}
override func viewWillAppear(animated: Bool) {
v.reloadData()
}
}
回答by Madavaram Ramesh
In Swift use this,
在 Swift 中使用这个,
If you wants to reload the UIView present in the viewController just use NotificationCenter.
如果您想重新加载 viewController 中存在的 UIView,只需使用 NotificationCenter。
In ViewController class add this in viewWillAppear
在 ViewController 类中,在 viewWillAppear 中添加它
override func viewWillAppear(_ animated: Bool) {
//Trigger notification
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "update"), object: nil)
}
In UIView class add Observer for the notification
在 UIView 类中为通知添加观察者
required init?(coder aDecoder: NSCoder) {
//Add Observer
NotificationCenter.default.addObserver(self, selector: #selector(updateList), name: NSNotification.Name(rawValue: "update"), object: nil)
}
@objc func updateList(){
//write Reload data here......
tableView.reloadData()
}
回答by Rouny
The Swift have really advanced itself and for many people if they don't know we can refresh the whole view with just one simple line of code.
Swift 确实取得了进步,对于许多人来说,如果他们不知道我们只需一行简单的代码就可以刷新整个视图。
viewWillAppear(true)
viewWillAppear(true)