Can you change view controllers without segue? - swift

Basically i have a button that changes title, and if the button title is something i want the button to take the user to somewhere different to if the button title was something else
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)
}
}

Instantiate the ViewController depending on the if check, and present it:
let storyBoard: UIStoryboard = UIStoryboard(name: "MyStoryboard", bundle: nil)
let destinationVC = storyBoard.instantiateViewController(withIdentifier: "ViewControllerIdentifier") as! NewViewController
present(destinationVC, animated: true, completion: nil)
Just remember to set the Identificator in the Storyboard

If the 2 controllers that you want to show are,
class Act1ViewController: UIViewController {
//your code...
}
class Act2ViewController: UIViewController {
//your code...
}
You simply need to put a switch-case on buttonName's title and present the relevant controller act1 or act2. Create act1 and act2 using the 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
}
}
}
Don't forget to set Act1ViewController and Act2ViewController as storyboardID of the respective controllers in the storyboard.

Related

Unable to pass image between view controllers

As asked on the question. On my first VC, I have set up a function that passes an image to another VC as follows.
#objc func imageTapped(tapGestureRecognizer1: UITapGestureRecognizer) {
let passimage = tapGestureRecognizer1.view as! UIImageView
let alertController = UIAlertController(title: "...", message: "...", preferredStyle: UIAlertController.Style.alert)
let ok = UIAlertAction(title: "...", style: UIAlertAction.Style.default, handler: {(action) -> Void in
self.performSegue(withIdentifier: "Shop", sender: self)
print("image tapped")
let sb = self.storyboard?.instantiateViewController(withIdentifier: "shopVC") as! shopVC
sb.loadimage = passimage.image
self.present(sb, animated: true, completion: nil)
})
alertController.addAction(ok)
alertController.addAction(UIAlertAction(title: "取消(留在此版面)", style: UIAlertAction.Style.cancel, handler: nil))
self.present(alertController, animated: true, completion: nil)
}
whilst on the other VC, I've set up an Image View which would read the image as shown here.
class shopVC: UIViewController {
#IBOutlet weak var clothimage: UIImageView!
var loadimage: UIImage?
override func viewDidLoad() {
clothimage.image = loadimage
super.viewDidLoad()
}
I know the "passimage.image" is what I want as it shows up in the debugging window - I can't seem to place it in the second VC.
for change ViewController you use two Option same Time, When used individually, each:
// optionOne
self.performSegue(withIdentifier: "Shop", sender: self)
print("image tapped")
// optionTwo
let sb = self.storyboard?.instantiateViewController(withIdentifier: "shopVC") as! shopVC
sb.loadimage = passimage.image
self.present(sb, animated: true, completion: nil)
if use Option One, you must override below function from firstVC and set image directly:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let vc = segue.destination as? shopVC {
vc.image = yourImage
}
}
and if you use present for show ShopVC nothing change code just comment performSegue function.
That means only the following code:
let sb = self.storyboard?.instantiateViewController(withIdentifier: "shopVC") as! shopVC
sb.loadimage = passimage.image
self.present(sb, animated: true, completion: nil)
Need to call super.viewDidLoad() first and then setting up clothimage

While present navigation then presented viewcontroller bar button not coming, why in swift

my viewcontroller flow will be like below:
navigationcontroller -> logoutviewcontroller ->(present)
viewcontroller -> (present) logoutviewcontroller (bar button coming)
|
loginviewcontroller
|
(present) logoutviewcontroller (bar button not coming)
logoutVC code:
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.leftBarButtonItem = UIBarButtonItem(title: "logout", style: .plain, target: self, action: #selector(handleLogout))
}
#objc func handleLogout(){
let vc = self.storyboard?.instantiateViewController(withIdentifier: "ViewController") as! ViewController
self.present(vc, animated: true, completion: nil)
}
loginVC code: when presenting logoutVC coming but bar button not coming.. why
#IBAction func loginButn(_ sender: Any) {
guard let email = emailTf.text, let password = passwordTf.text
else {
print("form is not valid")
return
}
Auth.auth().signIn(withEmail: email, password: password, completion: { (user, error) in
if error != nil{
print("login adding error: \(String(describing: error))" as Any)
return
}
print("login sucessfully")
let vc = self.storyboard?.instantiateViewController(withIdentifier: "LogOutViewController") as! LogOutViewController
self.present(vc, animated: true, completion: nil)
})
}
please suggest here with code
If by bar button you mean the navigationController's navigationBar, you don't need to present the ViewController but you need to use
self.navigationController?.pushViewController(vc, animated: true)

Present new ViewController after Interstitial Ad is closed - Swift 4

I am trying to make this button segue to a new ViewController once the user closes the interstitial ad. The segue code works without the ad code and the ad code works without the segue code. Whichever one is put first happens first, but I can't figure out how to get them to work together. You'll notice that I tried moving self.present... to various places with no success (see noted out lines).
#IBAction func adButton(_ sender: UIButton) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let myVC = storyboard.instantiateViewController(withIdentifier: "newVC")
//self.present(myVC, animated: true, completion: nil)
if (interstitial.isReady) {
interstitial.present(fromRootViewController: self)
interstitial = createAd()
//self.present(myVC, animated: true, completion: nil)
}
self.present(myVC, animated: true, completion: nil)
}
The interstitial object has a delegate (GADInterstitialDelegate) protocol that you can conform to in order to be notified when the ad has been dismissed. There are several delegate methods addressing the ad state which you can review in the documentation.
class ViewController:UIViewController, GADInterstitialDelegate {
override func viewDidLoad() {
super.viewDidLoad()
interstitial = GADInterstitial(adUnitID: "ca-app-pub-3940256099942544/4411468910")
interstitial.delegate = self
let request = GADRequest()
interstitial.load(request)
}
#IBAction func adButton(_ sender: UIButton) {
if (interstitial.isReady) {
interstitial.present(fromRootViewController: self)
} else {
self.present(myVC, animated: true, completion: nil)
}
}
//Tells the delegate the interstitial is to be animated off the screen.
func interstitialWillDismissScreen(_ ad: GADInterstitial) {
print("interstitialWillDismissScreen")
}
//Tells the delegate the interstitial had been animated off the screen.
func interstitialDidDismissScreen(_ ad: GADInterstitial) {
print("interstitialDidDismissScreen")
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let myVC = storyboard.instantiateViewController(withIdentifier: "newVC")
self.present(myVC, animated: true, completion: nil)
}
}
Google Ads Docs: Interstitial Ads

How do i load a viewcontroller from a xib file?

I tried to load a viewcontroller from a custom cell using delegates.But i get nil from the set delegate!
Here is a sample if anyone can help!
1. In Cell
protocol hotelFindDelegate{
func modalDidFinished(modalText: String)
}
class hotelFindCell: UITableViewCell {
var delegate:hotelFindDelegate?
#IBAction func findButton(sender: AnyObject) {
self.delegate!.modalDidFinished("HELLO")
print("Damn nothing works")
}
2. In Main View
class MainViewController:hotelFindDelegate {
let modalView = hotelFindCell()
override func viewDidLoad() {
super.viewDidLoad()
self.modalView?.delegate = self
}
func modalDidFinished(modalText: String){
let viewController:UIViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("HotelListVC") as UIViewController
self.presentViewController(viewController, animated: false, completion: nil)
self.modalView.delegate = self
print(modalText)
}
To load view controller from XIB do the following steps.
let settingVC : SettingsViewController = SettingsViewController(nibName :"SettingsViewController",bundle : nil)
later on you can push the same view controller object like
self.navigationController?.pushViewController(settingsVC, animated: true)
NSBundle.mainBundle().loadNibNamed("viewController", owner: self, options: nil)

how to close a tabbar swift

I have a login view controller which on a correct login will open a tabbar controller with the following method:
self.dismissViewControllerAnimated(true, completion: { self.loadHomeScreen()})
func loadHomeScreen()
{
emailField.text = ""
passwordField.text = ""
self.presentViewController(UIStoryboard.tabbarController()!, animated: true, completion: nil)
}
private extension UIStoryboard {
class func mainStoryboard() -> UIStoryboard { return UIStoryboard(name: "Main", bundle: NSBundle.mainBundle()) }
class func tabbarController() -> UITabBarController? {
return mainStoryboard().instantiateViewControllerWithIdentifier("TabbarControllerID") as? UITabBarController
}
}
I think my login view controller is still in the background? I have a logout button as the rightbarbutton item on each navbar in my tabgroup. I want to be able to close the tabbar on this button press. How can I achieve this. I can't find any examples. Would I be using a pop command?
UPDATE:
#IBAction func tryLogout(sender: UIBarButtonItem) {
self.dismissViewControllerAnimated(true, completion: nil)
let storyboard = UIStoryboard(name: "main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("login") as! UIViewController
self.presentViewController(vc, animated: true, completion: nil)
}