Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reload/Update View In Swift

Tags:

ios

swift

iphone

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.

like image 517
Ted Huinink Avatar asked Mar 03 '15 08:03

Ted Huinink


People also ask

How do I refresh in Xcode?

For a single file, you can open the external file in Xcode and cmd + s to save it, it will be refreshed in Xcode. Also, for the quick way you can just chose the external folder in Xcode and cmd + s , it will refresh all your external file in this folder.

What is Viewdidload in Swift?

This method is called after the view controller has loaded its view hierarchy into memory. This method is called regardless of whether the view hierarchy was loaded from a nib file or created programmatically in the loadView() method.

How do I reload a list in SwiftUI?

To add the pull to refresh functionality to our SwiftUI List, simply use the . refreshable modifier. List(emojiSet, id: \. self) { emoji in Text(emoji) } .


1 Answers

if you want to trigger layouting or just drawing there is setNeedsLayout and setNeedsDisplay

There is no built-in method to reload custom data (on iOS)


so do a reload and inside a reload -- call 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()
    }
}
like image 136
Daij-Djan Avatar answered Oct 07 '22 22:10

Daij-Djan