Вы можете изменить контроллеры представления без перехода? - PullRequest
0 голосов
/ 21 февраля 2020

По сути, у меня есть кнопка, которая меняет заголовок, и если заголовок кнопки является чем-то другим, я хочу, чтобы кнопка перенесла пользователя в другое место, если бы заголовок кнопки был чем-то другим

let act1 = act1ViewController()
let act2 = act2ViewController()

override func viewDidLoad() {
    super.viewDidLoad()

    self.buttonName.setTitle(self.text, for: .normal)
    // Do any additional setup after loading the view.
}

@IBAction func buttonPressed(_ sender: UIButton) {

   if text == "cheer up" {
    self.present(act1, animated: true, completion: nil)

}
   else if text == "yay" {
    self.present(act2, animated: true, completion: nil)
    }

}

Ответы [ 2 ]

0 голосов
/ 21 февраля 2020

Если 2 контроллера, которые вы хотите показать, это

class Act1ViewController: UIViewController {
    //your code...
}

class Act2ViewController: UIViewController {
    //your code...
}

Вам просто нужно поставить корпус переключателя на buttonName's title и представить соответствующий контроллер act1 или act2. Создайте act1 и act2, используя storyboard.

@IBAction func buttonPressed(_ sender: UIButton) {
    switch sender.titleLabel?.text {
    case "cheer up":
        if let act1 = self.storyboard?.instantiateViewController(withIdentifier: "Act1ViewController") as? Act1ViewController {
            self.present(act1, animated: true, completion: nil)
        }

    case "yay":
        if let act2 = self.storyboard?.instantiateViewController(withIdentifier: "Act2ViewController") as? Act2ViewController {
            self.present(act2, animated: true, completion: nil)
        }

    default:
        break
    }
}

}

Не забудьте установить Act1ViewController и Act2ViewController как storyboardID соответствующего контроллеры в storyboard.

0 голосов
/ 21 февраля 2020

Создайте ViewController в зависимости от проверки if и представьте его:

let storyBoard: UIStoryboard = UIStoryboard(name: "MyStoryboard", bundle: nil)
let destinationVC = storyBoard.instantiateViewController(withIdentifier: "ViewControllerIdentifier") as! NewViewController
present(destinationVC, animated: true, completion: nil)

Просто не забудьте установить Идентификатор в раскадровке

...