在Swift中使用switch访问类属性

时间:2019-09-25 11:40:57

标签: ios swift

我有以下课程:

import UIKit

protocol PickNumberVieDelegate: class {
    func dismissPickNumberView(tempDef:String, tempAlter:String)
}

class PickNumberViewController: UIViewController,UITableViewDataSource, UITableViewDelegate{

    weak var delegate: PickNumberVieDelegate?
    var whatTypOfNumber = 0
    @IBOutlet weak var tableview: UITableView!
    @IBOutlet weak var indicator: UIActivityIndicatorView!

    var arr = [PresentationNumberOption]()

    var tempAlernatNr = ""
    var tempDefaultNr = ""

    override func viewDidLoad() {
        super.viewDidLoad()

        //HERE
        var storyboard: UIStoryboard? { get }
        print("?here", storyboard);

        self.setUserInterfaceStyleLight()
        if (whatTypOfNumber == 0) {

            self.title = NSLocalizedString("TimeControlled AfterWork Time" , comment: "")

        }else if (whatTypOfNumber == 1){
            self.title = NSLocalizedString("TimeControlled Showing Number" , comment: "")
        }else{

            self.title = NSLocalizedString("Display Number", comment: "")
        }

    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        var mobilNumber = ""
        if  let mobileNumberForThisDevice = CurrentPerson.shared().thisMobilePhone(){

            mobilNumber = mobileNumberForThisDevice.address
        };
        Communication.sendGetCustomPresentationNumberOptionsforNumber(mobilNumber, withCallbackMethod: #selector(self.gotPresentationNumberOptions(_:)), callbackFailMethod: #selector(self.failedToGetNumber), on: self)

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return arr.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let CellIdentifier = "CellId"
        var cell = tableView.dequeueReusableCell(withIdentifier: CellIdentifier)

        if cell == nil {
            cell = UITableViewCell(style: .default, reuseIdentifier: CellIdentifier)
        }

        var option: PresentationNumberOption?

        option = arr[indexPath.row]
        var str = option?.displayText()
        str = str?.replacingOccurrences(of: "[", with: "")
        str = str?.replacingOccurrences(of: "]", with: "")
        cell?.textLabel?.text = str

        if( whatTypOfNumber == 0){
            if (ModelManager.shared()?.tcsm.defaultNr == option?.address) {

                cell?.accessoryType = .checkmark

            } else {
                cell?.accessoryType = .none
            }
        }else{
            if (ModelManager.shared()?.tcsm.alternateNr == option?.address) {

                cell?.accessoryType = .checkmark
            } else {
                cell?.accessoryType = .none
            }
        }

        return cell!
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        tempAlernatNr = (ModelManager.shared()?.tcsm.alternateNr)!
        tempDefaultNr = (ModelManager.shared()?.tcsm.defaultNr)!

        let presentationNumber: PresentationNumberOption?
        presentationNumber = arr[indexPath.row]

        if( whatTypOfNumber == 0){
            if(presentationNumber?.address == ""){
                if presentationNumber?.type == kPresentationNumberOptionTypeMobileNumber{

                    self.showVerificationScreen()
                }else{

                    ModelManager.shared()?.tcsm.defaultNr = presentationNumber?.address ?? ""
                    delegate?.dismissPickNumberView(tempDef: tempDefaultNr, tempAlter: tempAlernatNr)
                    self.navigationController?.popViewController(animated: true)
                }

            }else{
                ModelManager.shared()?.tcsm.defaultNr = presentationNumber?.address ?? ""
                delegate?.dismissPickNumberView(tempDef: tempDefaultNr, tempAlter: tempAlernatNr)
                self.navigationController?.popViewController(animated: true)
            }
        }else{
            if(presentationNumber?.address == ""){
                if presentationNumber?.type == kPresentationNumberOptionTypeMobileNumber{

                    self.showVerificationScreen()
                }else{

                    ModelManager.shared()?.tcsm.alternateNr = presentationNumber?.address ?? ""
                    delegate?.dismissPickNumberView(tempDef: tempDefaultNr, tempAlter: tempAlernatNr)
                    self.navigationController?.popViewController(animated: true)
                }

            }else{
                ModelManager.shared()?.tcsm.alternateNr = presentationNumber?.address ?? ""
                delegate?.dismissPickNumberView(tempDef: tempDefaultNr, tempAlter: tempAlernatNr)
                self.navigationController?.popViewController(animated: true)
            }
        }


    }

    @objc func gotPresentationNumberOptions(_ presentationNumbers: [Any]?) {
        if let aNumbers = presentationNumbers {
            self.arr = aNumbers as! [PresentationNumberOption]
        }

        indicator.stopAnimating()

        tableview.reloadData()
    }

    @objc func failedToGetNumber() {
        //ALog("Failed to get presentation number options:%@", error)
        indicator.stopAnimating()
    }

    func numberOfSections(in tableView: UITableView) -> Int {
        // Return the number of sections.
        return 1
    }

    func showVerificationScreen() {
        print("?Show Verification Screen");

        let nextController = NumberVerificationViewController(nibName: "NumberVerificationViewController", bundle: Bundle.main)

        navigationController?.pushViewController(nextController, animated: true)
    }
}

当我打电话

class Router: NSObject {

 enum Route {
    case auth
    case main
    case news
  }

  private var authNavigationController: UINavigationController?
  private var mainNavigationController: UINavigationController?
  private var newsNavigationController: UINavigationController?

func setInitialVC(){
    let vc = LoginService.initialVC()
    setRootViewControllerWithNavigationControlledEmbeded(route: .auth,
                                                         viewController: vc)
  }

  private func getCurrentNavC(_ route: Route) -> UINavigationController?{
    switch route {
    case .auth:
      return self.authNavigationController
    case .main:
      return self.mainNavigationController
    case .news:
      return self.newsNavigationController
    }
  }

  private func setRootViewControllerWithNavigationControlledEmbeded(route: Route,
                                                                    viewController: UIViewController){
    var navC = getCurrentNavC(route)
    self.window = UIWindow(frame: UIScreen.main.bounds)
    navC = UINavigationController.init(rootViewController: viewController)
    print("auth vc \(authNavigationController)")
    print("navC \(navC)")
    self.window?.rootViewController = navC
    self.window?.makeKeyAndVisible()
  }
}

我假设setRootViewControllerWithNavigationControlledEmbeded(route: Route, viewController: UIViewController){ 将指向我的类变量var navC = getCurrentNavC(route),但在日志中我可以看到以下内容:

authNavigationController

我想通过route获取此属性,但是看起来我的逻辑不起作用,为什么?

应用刚启动时会调用-auth vc nil navC Optional(<UINavigationController: 0x7fd9ca025000>)

1 个答案:

答案 0 :(得分:0)

如下所示修改setRootViewControllerWithNavigationControlledEmbeded函数,然后尝试。

private func setRootViewControllerWithNavigationControlledEmbeded(route: Route,
                                                                    viewController: UIViewController){
    var navC = getCurrentNavC(route)
    self.window = UIWindow(frame: UIScreen.main.bounds)
    //navC = UINavigationController.init(rootViewController: viewController)
    print("auth vc \(authNavigationController)")
    print("navC \(navC)")
    self.window?.rootViewController = navC
    self.window?.makeKeyAndVisible()
  }