如何实例化不在storyboard中的viewController

时间:2016-10-18 20:33:16

标签: ios swift firebase

我目前正在使用自定义tabBarController导航到我的应用。我正在为一些视图控制器和其他人使用故事板我不是。问题是不在故事中的viewControllers没有被重新实例化。当我第一次导航到视图时一切正常。如果我退出然后返回相同的视图不刷新。在旧数据之上重新添加相同的数据。这发生在我无法使用标识符添加的视图中。我做self.storyboard?.instantiateViewController(withIdentifier: "LoginViewController") as! LoginViewController的工作正常。我该怎么解决这个问题。提前谢谢。

这项工作很好:这个视图表现正常,因为它是实例化的

 let dashboardController = self.storyboard?.instantiateViewController(withIdentifier: "DashboardViewController") as! DashboardViewController
        let dashboardTabImg = UIImage.fontAwesomeIconWithName(.User, textColor: iconColor, size: iconSize )
        let dashboardTabImgSelected = UIImage.fontAwesomeIconWithName(.User, textColor: iconColor, size: iconSize)
        dashboardController.tabBarItem = UITabBarItem(title: "Login", image: dashboardTabImg, selectedImage: dashboardTabImgSelected )


// this does not work properly because it was not instantiate.

     //receivePostRequest VC
            let receivePostRequest = UINavigationController(rootViewController: ReceivePostRequestViewController())
            let receivePostRequestImg = UIImage.fontAwesomeIconWithName(.Dollar, textColor: iconColor, size: iconSize )
            let receivePostRequestImgSelected = UIImage.fontAwesomeIconWithName(.Dollar, textColor: iconColor, size: iconSize)
            receivePostRequest.tabBarItem = UITabBarItem(title: "Request", image: receivePostRequestImg, selectedImage: receivePostRequestImgSelected )

//这是我遇到问题的viewController

import UIKit
import Firebase

class ReceivePostRequestViewController: UIViewController {
   let receiveRequestSwitch = UISwitch()
    let receiveRequestLabel = UILabel()

    override func viewWillAppear(_ animated: Bool) {
        navigationItem.title = "Post Request"
        receiveRequestSwitch.isOn = false
        checkFirBaseForSwitchStatus()
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white
        pageSetup()
        receiveRequestSwitch.addTarget(self, action: #selector(postRequestSwitchButtonPressed), for: .touchUpInside)

    }

}
enum receivePostStatusEnums{

    case pending
    case accepted
    case disabled
    case declined
    case contactCustomerSupport
    case  error
}

extension ReceivePostRequestViewController {
    func pageSetup(){
        receiveRequestLabel.text = "Receive Post Request"
        receiveRequestLabel.isUserInteractionEnabled = true
        receiveRequestLabel.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(receiveRequestLabel)
        receiveRequestLabel.topAnchor.constraint(equalTo: view.topAnchor, constant: 80).isActive = true
        receiveRequestLabel.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 26).isActive = true
        receiveRequestLabel.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -26).isActive = true
        receiveRequestLabel.heightAnchor.constraint(equalToConstant: 44).isActive = true


        receiveRequestLabel.addSubview(receiveRequestSwitch)

        receiveRequestSwitch.frame = CGRect(x: 0, y: 0, width: 40, height: 44)
        receiveRequestSwitch.translatesAutoresizingMaskIntoConstraints = false
       // receiveRequestSwitch.leftAnchor.constraint(equalTo: receiveRequestLabel.leftAnchor).isActive = true
        receiveRequestSwitch.rightAnchor.constraint(equalTo: receiveRequestLabel.rightAnchor).isActive = true
        receiveRequestSwitch.centerYAnchor.constraint(equalTo: receiveRequestLabel.centerYAnchor).isActive = true



    }
    func checkFirBaseForSwitchStatus(){


        FIRDatabase().childRef(refUrl: "frontEnd/users/\((FIRAuth.auth()?.currentUser?.uid)!)/receivePostRequest/info").observe(.value, with: {snapshot in
            if let dict = snapshot.value as? NSDictionary {
                if let stat = dict["isAvailabileForRequest"] as? Bool {
                    if stat{
                        self.receiveRequestSwitch.isOn = true
                    }else{
                         self.receiveRequestSwitch.isOn = false
                    }
                }
            }else{
                self.receiveRequestSwitch.isOn = false
            }
        })
    }
    func postRequestSwitchButtonPressed(){
        if self.receiveRequestSwitch.isOn{
            receiveRequestSwitch.isOn = false
            let userId = FIRAuth.auth()?.currentUser?.uid
            FIRDatabase.database().reference().child("frontEnd/users/\(userId!)/receivePostRequest/application").observeSingleEvent(of: .value, with: {snapshot in
                if let dict = snapshot.value as? [String:AnyObject]{
                            if let status = dict["status"] as? String {
                                print(status)
                                switch status {
                                    case "pending":
                                        print("its pending")
                                        self.simpleAlert(title: nil, message: "Your application is currently Being Reviewed, no action is required from you")

                                    case "accepted":
                                        print("its accepted")
                                        let viewController:UIViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "PostSelectViewController")
                                        self.present(viewController, animated: true, completion: nil)
                                    case "disabled":
                                        print("its disabled")
                                         self.simpleAlert(title: nil, message: "Your account is currently disabled if you have any questions please contact customer service")

                                    case "declined":
                                        self.simpleAlert(title: nil, message: "After reviewing your application we will not be able to continue your application we are very sorry. If you feel this was an error please contact our customer service")

                                    case "contactCustomerSupport":
                                        print("its contactCustomerSupport")
                                        self.simpleAlert(title: nil, message: "Please contact customer")

                                    case "error":
                                        print("its error")
                                          self.simpleAlert(title: nil, message: "We can not process your request please try again at a later time")

                                    default:
                                        print ("error")
                                          self.simpleAlert(title: nil, message: "We can not process your request please try again at a later time")
                                    }
                            }
                    return
                }
                //user need submit application here
                print("user need to fill out application page")
                let applicationNavController = UINavigationController(rootViewController:ReceivePostRequestApplicationViewController())
                self.present(applicationNavController, animated: true, completion: nil)

            })
        }else{
            //update info to turn switch off
            FIRDatabase().childRef(refUrl: "frontEnd/users/\((FIRAuth.auth()?.currentUser?.uid)!)/receivePostRequest/info").updateChildValues(["isAvailabileForRequest":false], withCompletionBlock: { (error, fir) in
                if error != nil {
                    print(error?.localizedDescription)
                    return
                }
                let ac = UIAlertController(title: nil, message: "You will no longer receive service request", preferredStyle: .alert)
                let okAction = UIAlertAction(title: "Ok", style: .default, handler: { (okAction) in
                    self.dismiss(animated: true, completion: nil)

                })
                ac.addAction(okAction)
                self.present(ac, animated: true, completion: nil
                )

            })
        }
    }
}

0 个答案:

没有答案