Programmatically navigate to another view controller/scene
I already found the answer
Swift 4
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController = storyBoard.instantiateViewController(withIdentifier: "nextView") as! NextViewController
self.present(nextViewController, animated:true, completion:nil)
Swift 3
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController = storyBoard.instantiateViewControllerWithIdentifier("nextView") as NextViewController
self.presentViewController(nextViewController, animated:true, completion:nil)
Swift programmatically navigate to another view controller/scene
Swift 5
The default modal presentation style is a card. This shows the previous view controller at the top and allows the user to swipe away the presented view controller.
To retain the old style you need to modify the view controller you will be presenting like this:
newViewController.modalPresentationStyle = .fullScreen
This is the same for both programmatically created and storyboard created controllers.
Swift 3
With a programmatically created Controller
If you want to navigate to Controller created Programmatically, then do this:
let newViewController = NewViewController()
self.navigationController?.pushViewController(newViewController, animated: true)
With a StoryBoard created Controller
If you want to navigate to Controller on StoryBoard with Identifier "newViewController", then do this:
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let newViewController = storyBoard.instantiateViewController(withIdentifier: "newViewController") as! NewViewController
self.present(newViewController, animated: true, completion: nil)
How to Navigate from one View Controller to another using Swift
Create a swift file (SecondViewController.swift) for the second view controller
and in the appropriate function type this:
let secondViewController = self.storyboard.instantiateViewControllerWithIdentifier("SecondViewController") as SecondViewController
self.navigationController.pushViewController(secondViewController, animated: true)
Swift 2+
let mapViewControllerObj = self.storyboard?.instantiateViewControllerWithIdentifier("MapViewControllerIdentifier") as? MapViewController
self.navigationController?.pushViewController(mapViewControllerObj!, animated: true)
Swift 4
let vc = UIStoryboard.init(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: "IKDetailVC") as? IKDetailVC
self.navigationController?.pushViewController(vc!, animated: true)
Navigate from a ViewController to an another on didSelectRowAt programmatically
Here is a complete answer:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let newViewController = NewViewController()
self.navigationController?.pushViewController(newViewController, animated: true)
}
How to Navigate between Viewcontrollers without any action from user swift
I think u do not require a segue for this. U can just push navigation Controller.
let storyBoardHome = UIStoryboard(name:storyBoardName, bundle: nil)
let 1stViewController = storyBoardHome.instantiateViewController(withIdentifier: <1stViewControllerID>)
let 2ndViewController = storyBoardHome .instantiateViewController(withIdentifier: 2ndViewControllerID)
self.navigationController?.pushViewController(1stViewController/2ndViewController, animated: true)
Navigate one collection view controller to another collection view controller?
I did by my own... here I am attaching my code 1st I created struct
import Foundation
struct City {
var image:String = ""
var name:String = ""
init(image: String, name: String){
self.image = image
self.name = name
}
}
after that I enter code for my 1st cell view controller
import UIKit
private let reuseIdentifier = "Cell"
class cityCollectionViewController: UICollectionViewController {
var cities : [City] = [ City(image: "Ankara", name: "Ankara"),
City(image: "Antalya", name: "Antalya"),
City(image: "Aydin", name: "Aydin"),
City(image: "Bodrum", name: "Bodrum"),
City(image: "Canakkale", name: "Canakkale"),
City(image: "Cappadocia", name: "Cappadocia"),
City(image: "Efes", name: "Efes"),
City(image: "Eskisehir", name: "Eskisehir"),
City(image: "Fethiye", name: "Fethiye"),
City(image: "Istanbul", name: "Istanbul"),
City(image: "Izmir", name: "Izmir"),
City(image: "Mardin", name: "Mardin"),
City(image: "Nemrut", name: "Nemrut"),
]
override func viewDidLoad() {
super.viewDidLoad()
}
// MARK: UICollectionViewDataSource
override func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return cities.count
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
var cell = collectionView.dequeueReusableCell(withReuseIdentifier: "dataCell", for: indexPath) as! cityCollectionViewCell
cell.cityImageView.image = UIImage(named: cities[indexPath.row].image )
cell.cityNameLabel.text = cities[indexPath.row].name
return cell
}
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let imageDetailVC = self.storyboard?.instantiateViewController(withIdentifier: "collectCollectionViewController") as! collectCollectionViewController
/
self.navigationController?.pushViewController(imageDetailVC, animated: true)
}
}
created a data cell and gave the outlets
import UIKit
class cityCollectionViewCell: UICollectionViewCell {
@IBOutlet weak var cityImageView: UIImageView!
@IBOutlet weak var cityNameLabel: UILabel!
}
This is for my second cell view controller
import UIKit
private let reuseIdentifier = "Cell"
class collectCollectionViewController: UICollectionViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
var obj = cityCollectionViewController()
var app1 = secondCollectionViewCell()
override func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return obj.cities.count
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Datacell1", for: indexPath) as! secondCollectionViewCell
let city1 = obj.cities[indexPath.row]
cell.image11.image = UIImage(named: obj.cities[indexPath.row].image)
cell.label11.text = obj.cities[indexPath.row].name
return cell
}
}
This code for my second data view cell
import UIKit
class secondCollectionViewCell: UICollectionViewCell {
@IBOutlet weak var image11: UIImageView!
@IBOutlet weak var label11: UILabel!
}
Switch programmatically from a viewcontroller to another
Your first bit of code doesn't work because EndViewController
is a type. You should replace this with secondViewController
that you declared above.
The second bit of code doesn't work because you are creating an 'empty' EndViewController type and not instantiating the storyboard view .
I'm guessing the third bit of code is failing because your storyboard isn't called "EndViewController" (unless you've renamed it). Try changing this to "Main" instead.
Related Topics
How to Trap on Uiviewalertforunsatisfiableconstraints
Setting Direction for Uiswipegesturerecognizer
Xcode 9 - "Fixed Width Constraints May Cause Clipping" and Other Localization Warnings
How to Set Cg_Context_Show_Backtrace Environmental Variable
Xcode Quits Unexpectedly Every Time I Open My Project
Emulating Aspect-Fit Behaviour Using Autolayout Constraints in Xcode 6
Core Animation Progress Callback
How to Convert a Double into a Byte Array in Swift
How to Test If a String Is Empty in Objective-C
Developing for iOS Device in Windows Environment with Flutter
iOS - Uiimageview - How to Handle Uiimage Image Orientation
Attempt to Insert Non-Property List Object When Trying to Save a Custom Object in Swift 3
Uitableview - Change Section Header Color
How to Fix "No Valid 'Aps-Environment' Entitlement String Found for Application" in Xcode 4.3