简体   繁体   中英

UIView Extension does not work on app first launch

I am using the following UIView extension:

https://github.com/snoozelag/GoneVisible

I have successfully downloaded the file and added the Swift file and I am using the extension to hide (gone method) and show (visible method) buttons on the navigation bar. When the app first opens, I call this extension in an attempt to hide certain buttons if the user is already logged in. However, this has not been working. Strangely, it DOES work and hides the buttons after I segue to a different view and go back.

Here is the code:

import UIKit

import Parse

class ViewController: UIViewController {

@IBOutlet weak var signUpButton: UIButton!
@IBOutlet weak var logInButton: UIButton!
@IBOutlet weak var myAccountButton: UIButton!

@IBOutlet weak var bigGame: UIImageView!


private func setUpPage(){
    let currentUser = PFUser.current()
    if currentUser != nil {
      // Do stuff with the user
        self.myAccountButton.visible()
        self.signUpButton.gone()
        self.logInButton.gone()

    } else {
      // Show the signup or login screen
        self.myAccountButton.gone()
        self.signUpButton.visible()
        self.logInButton.visible()
    }
}


override func viewDidLoad() {

    setUpPage()

    super.viewDidLoad()

    self.navigationItem.hidesBackButton = true;

}

override func viewWillAppear(_
    animated: Bool) {
    setUpPage()
}

My question is, how can I get this extension to fire when the app is first opened?

Thanks a lot for your help :)

  1. Don't forget to call super.viewWillAppear(...) when you override the inherited implementation.

This might solve your issue - but even if not it is correct to do it.

Update:

  1. try calling setUpPage() only once and only after you call super.viewDidLoad()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM