1

我有一个现有的应用程序,其中在打开应用程序时,进度条的默认步骤是UserType, Credentials, Email, Password, Confirm Password, Agreement and Done,该应用程序需要对其进行一些更改Stepped Progress Bar,顺便说一下我使用FlexibleSteppedProgressBarpod 作为进度条。我的问题是,该应用程序有三个 (3) UserTypes。两种用户类型(User1,User2)应该有如下 8 个步骤UserType, Credentials, Email, Password, Confirm Password, Agreement and Done,而一种用户类型(User3)只有 7 个步骤(没有Agreement步骤)。User3当用户点击进度条中的步骤应该只有 7 个步骤以及用户点击时如何修改进度条User1 User2用户类型,应保留默认的 8 步进度条。我有点困惑,因为我是 swift 编程的新手,我正在修改一个需要修改的现有应用程序,而我不是创建该应用程序的原始应用程序。我尝试了一些修改,我添加了另外两个结构,其中包含 7 个步骤和 8 个步骤,点击User37 个步骤的结构将被调用并变成 7 个步骤,但MasterViewController布局仍然没有修改原来的 8 个步骤,只是在修改后的 7 个步骤中重叠,这意味着看起来我的应用程序中出现了两个重叠的进度条。

页面.swift

struct Page {

static let titles: [String] = ["User Type",
                              "Credential",
                              "Birthdate",
                              "Email",
                              "Password",
                              "Confirm",
                              "Agreement",
                              "Done"]
}

Page2.swift

struct Page2 {

static let titles: [String] = ["User Type",
                              "Credential",
                              "Birthdate",
                              "Email",
                              "Password",
                              "Confirm",
                             // "Agreement",
                              "Done"]
 }

MasterViewController.swift

override func viewDidLoad() {
    super.viewDidLoad()

    configureNotificationObserver()
    setupView()
    configureAnimationView()
}

// MARK: VIEW CONTROLLER INITIALIZATIONS

   private lazy var roleViewController: RoleViewController = {
    let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)
    let viewController = storyboard.instantiateViewController(withIdentifier: "RoleViewController") as! RoleViewController
    self.add(asChildViewController: viewController)

    return viewController
  }()

   private lazy var firstCredentialViewController: FirstCredentialViewController = {
    let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)
    let viewController = storyboard.instantiateViewController(withIdentifier: "FirstCredentialViewController") as! FirstCredentialViewController
    self.add(asChildViewController: viewController)

    return viewController
  }()

   private lazy var birthdateViewController: BirthdateViewController = {
    let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)
    let viewController = storyboard.instantiateViewController(withIdentifier: "BirthdateViewController") as! BirthdateViewController
    self.add(asChildViewController: viewController)

    return viewController
  }()

    private lazy var emailViewController: EmailViewController = {
    let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)
    let viewController = storyboard.instantiateViewController(withIdentifier: "EmailViewController") as! EmailViewController
    self.add(asChildViewController: viewController)

    return viewController
  }()

    private lazy var passwordViewController: PasswordViewController = {
    let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)
    let viewController = storyboard.instantiateViewController(withIdentifier: "PasswordViewController") as! PasswordViewController
    self.add(asChildViewController: viewController)

    return viewController
  }()

    private lazy var confirmPasswordViewController: ConfirmPasswordViewController = {
    let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)
    let viewController = storyboard.instantiateViewController(withIdentifier: "ConfirmPasswordViewController") as! ConfirmPasswordViewController
    self.add(asChildViewController: viewController)

    return viewController
  }()

    private lazy var termsAndConditionsViewController: TermsAndConditionsViewController = {
    let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)
    let viewController = storyboard.instantiateViewController(withIdentifier: "TermsAndConditionsViewController") as! TermsAndConditionsViewController
    self.add(asChildViewController: viewController)

    return viewController
  }()


private lazy var doneViewController: DoneViewController = {
    let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)
    let viewController = storyboard.instantiateViewController(withIdentifier: "DoneViewController") as! DoneViewController
    self.add(asChildViewController: viewController)

    return viewController
  }()

  // MARK: FUNCTIONS

  private func add(asChildViewController viewController: UIViewController) {
    addChildViewController(viewController)

    containerView.addSubview(viewController.view)

    viewController.view.frame = view.bounds
    viewController.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]

    viewController.didMove(toParentViewController: self)
  }

  private func remove(asChildViewController viewController: UIViewController) {
    viewController.willMove(toParentViewController: nil)
    viewController.view.removeFromSuperview()
    viewController.removeFromParentViewController()
  }

  private func configureAnimationView() {
    gearsAnimationView.frame = CGRect(x: 350, y: 160, width: 300, height: 200)
    gearsAnimationView.contentMode = .scaleAspectFit
    gearsAnimationView.loopAnimation = true

    loadingContainerView.addSubview(gearsAnimationView)
  }


   private func setupSteppedProgressBar() {
    progressBar.numberOfPoints = Page.titles.count
    progressBar.lineHeight = 8
    progressBar.radius = 15
    progressBar.progressRadius = 20
    progressBar.progressLineHeight = 3
    progressBar.delegate = self

    progressBar.stepTextColor = UIColor.lightGray
    progressBar.stepTextFont = UIFont.init(name: "HelveticaNeue", size: 15.0)
    progressBar.selectedBackgoundColor = UIColor(red: 82/255, green: 177/255, blue: 108/255, alpha: 1.0)

    // CURRENT SELECTED
    progressBar.currentSelectedCenterColor = UIColor(red: 244/255, green: 69/255, blue: 93/255, alpha: 1.0)
    progressBar.selectedOuterCircleStrokeColor = UIColor(red: 244/255, green: 69/255, blue: 93/255, alpha: 1.0)
    progressBar.currentSelectedTextColor = UIColor(red: 56/255, green: 56/255, blue: 56/255, alpha: 1.0)
}

   func updateView() {

    let currentIndex = progressBar.currentIndex

    switch currentIndex {
       case 0:
        remove(asChildViewController: firstCredentialViewController)
        add(asChildViewController: roleViewController)
        case 1:
        remove(asChildViewController: roleViewController)
        add(asChildViewController: firstCredentialViewController)
        case 2:
        remove(asChildViewController: firstCredentialViewController)
        add(asChildViewController: birthdateViewController)
        case 3:
        remove(asChildViewController: birthdateViewController)
        add(asChildViewController: emailViewController)
        case 4:
        remove(asChildViewController: emailViewController)
        add(asChildViewController: passwordViewController)
        case 5:
        remove(asChildViewController: passwordViewController)
        add(asChildViewController: confirmPasswordViewController)
        case 6:
        remove(asChildViewController: confirmPasswordViewController)
       // add(asChildViewController: termsAndConditionsViewController)
        case 7:
     //   remove(asChildViewController: termsAndConditionsViewController)
        add(asChildViewController: doneViewController)
        default: break
      }
   }

    private func setupView() {
    setupSteppedProgressBar()
    updateView()
   }

private func configureNotificationObserver() {
    NotificationCenter.default.addObserver(self, selector: #selector(self.nextStep), name: NSNotification.Name(rawValue: "nextStep"), object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(self.previousStep), name: NSNotification.Name(rawValue: "previousStep"), object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(self.showLoading), name: NSNotification.Name(rawValue: "showLoading"), object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(self.hideLoading), name: NSNotification.Name(rawValue: "hideLoading"), object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(self.successfulUserCreated), name: NSNotification.Name(rawValue: "UserCreated"), object: nil)
   }

//sample typing 

@objc func nextStep() {

    let currentIndex = progressBar.currentIndex
    let totalNumberOfPoints = progressBar.numberOfPoints

    switch currentIndex {
    case 0:
        firstCredentialViewController.userRole = roleViewController.userRole

    case 1:
        birthdateViewController.userRole = firstCredentialViewController.userRole
        birthdateViewController.firstCredential = firstCredentialViewController.firstCredential
    case 4:
        confirmPasswordViewController.userRole = birthdateViewController.userRole
        confirmPasswordViewController.firstCredential = birthdateViewController.firstCredential
        confirmPasswordViewController.emailString = emailViewController.emailString
        confirmPasswordViewController.passwordString = passwordViewController.passwordString
   // case 5:
     //   termsAndConditionsViewController.userRole = confirmPasswordViewController.userRole
    //    termsAndConditionsViewController.firstCredential = confirmPasswordViewController.firstCredential
     //   termsAndConditionsViewController.emailString = confirmPasswordViewController.emailString
      //  termsAndConditionsViewController.confirmPasswordString = confirmPasswordViewController.passwordString
    default: break
    }

    if currentIndex < (totalNumberOfPoints - 1) {
        progressBar.currentIndex = currentIndex + 1
        updateView()
    }
}

@objc func previousStep() {

    let currentIndex = progressBar.currentIndex

    progressBar.currentIndex = currentIndex - 1
    updateView()



@objc func showLoading() {
    isLoading(on: true)
}

@objc func hideLoading() {
    isLoading(on: false)
}

@objc func successfulUserCreated() {
    clearAllData()
    progressBar.currentIndex = 0
    updateView()
}

private func clearAllData() {

    // RoleViewController
   roleViewController.userRole = nil

    // FirstCredentialViewController
    firstCredentialViewController.userRole = nil
    firstCredentialViewController.firstCredential = nil
    firstCredentialViewController.titleLabel.text = ""
    firstCredentialViewController.credentialTextField.text = ""

    // BirthdateViewController
    birthdateViewController.userRole = nil
    birthdateViewController.firstCredential = nil
    birthdateViewController.birthdate = nil

    // EmailViewController
    emailViewController.emailString = nil
    emailViewController.emailAddressTextField.text = ""

    // PasswordViewController
    passwordViewController.passwordString = nil
    passwordViewController.passwordTextField.text = ""

    // ConfirmPasswordViewController
    confirmPasswordViewController.userRole = nil
    confirmPasswordViewController.firstCredential = nil
    confirmPasswordViewController.passwordString = nil
    confirmPasswordViewController.confirmPasswordTextField.text = ""

    //TermsAndConditionsViewController
    termsAndConditionsViewController.proceedButton.isEnabled = false
    termsAndConditionsViewController.checkButton.isSelected = false
  }

 }

extension MasterViewController {

func progressBar(_ progressBar: FlexibleSteppedProgressBar, canSelectItemAtIndex index: Int) -> Bool {
    return false
}

func progressBar(_ progressBar: FlexibleSteppedProgressBar, textAtIndex index: Int, position: FlexibleSteppedProgressBarTextLocation) -> String {

    if position == FlexibleSteppedProgressBarTextLocation.bottom {

        return Page.titles[index]
    }

    return ""
  }
 }
4

0 回答 0