Make Segue programmatically in Swift
You can do it like proposed in this answer:
InstantiateViewControllerWithIdentifier.
Furthermore I am providing you the code from the linked answer rewritten in Swift because the answer in the link was originally written in Objective-C.
let vc = UIStoryboard(name:"Main", bundle:nil).instantiateViewController(withIdentifier: "identifier") as! SecondViewController
vc.resultsArray = self.resultsArray
EDIT:
Since this answer draws some attention I thought I provide you with another more failsafe way. In the above answer the application will crash if the ViewController
with "identifier" is not of type SecondViewController
. In Swift you can prevent this crash by using optional binding:
guard let vc = UIStoryboard(name:"Main", bundle:nil).instantiateViewControllerWithIdentifier("identifier") as? SecondViewController else {
print("Could not instantiate view controller with identifier of type SecondViewController")
return
}
vc.resultsArray = self.resultsArray
self.navigationController?.pushViewController(vc, animated:true)
This way the ViewController
is pushed if it is of type SecondViewController
. If can not be casted to SecondViewController
a message is printed and the application remains on the current ViewController
.
Perform a segue programmatically
Segues are components of storyboard. If you don't have a storyboard, you can't perform segues. But you can use present view controller like this:
let vc = ViewController() //your view controller
self.present(vc, animated: true, completion: nil)
Creating a segue programmatically without a storyboard
I assume you meant a ViewController
by the word page. So initialize your ViewController
inside your func regSegue(){...}
and present it using present(_:animated:completion:)
method.
Example:
func regSegue(){
let page = PageViewController()
present(page, animated: true, completion: nil)
}
Segue and Button programmatically swift
Create seuge
Assign identifier
and your button target
@IBAction func button_clicked(_ sender: UIButton) {
self.performSegue(withIdentifier: "segueToNext", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "segueToNext" {
if let destination = segue.destination as? Modo1ViewController {
destination.nomb = nombres // you can pass value to destination view controller
// destination.nomb = arrayNombers[(sender as! UIButton).tag] // Using button Tag
}
}
}
How to add a segue programmatically?
Make sure your tableview delegate is set. If you are using storyboard, make sure delegate outlet in your storyboard is connected properly. If you are creating tableview by code, then you should do tableView.delegate=self;
to set the delegate.
Your code is fine.
And one more thing:
You might need to change this line:
performSegueWithIdentifier("showContent", sender: tableView)
you need to make the sender as the row but not the tableview,so that the prepare for segue will get the sender as row instead of whole tableview.
As you are calling the prepareForSegue overtime you select a row, it makes sense to make the row as sender in performSegueWithIdentifier.
So it would be:
let row=indexPAth.row
performSegueWithIdentifier("showContent", sender: row)
Make a segue to a tableview controller from a view controller
Okay to clarify that. There is no way to create a segue programmatically. Segues are the arrows on storyboard linking from one to another VC. They are called with: performSegue. This calls the function prepare.
If you want to show a new VC when hitting a button (without segue), then you use the present(VC(), animated: true, completion: nil) } inside the button function. The VC is presented modally.
@objc func editProfileButtonAction(sender: UIButton!) {
print("editProfileButtonAction")
present(EditProfileTableViewController(), animated: true, completion: nil)
}
Creating a segue programmatically
By definition a segue can't really exist independently of a storyboard. It's even there in the name of the class: UIStoryboardSegue
. You don't create segues programmatically - it is the storyboard runtime that creates them for you. You can normally call performSegueWithIdentifier:
in your view controller's code, but this relies on having a segue already set up in the storyboard to reference.
What I think you are asking though is how you can create a method in your common view controller (base class) that will transition to a new view controller, and will be inherited by all derived classes. You could do this by creating a method like this one to your base class view controller:
- (IBAction)pushMyNewViewController
{
MyNewViewController *myNewVC = [[MyNewViewController alloc] init];
// do any setup you need for myNewVC
[self presentModalViewController:myNewVC animated:YES];
}
and then in your derived class, call that method when the appropriate button is clicked or table row is selected or whatever.
Related Topics
Save and Load from Keychain | Swift
Uitableview Dynamic Cell Heights Only Correct After Some Scrolling
How to Make View the Size of Another View in Swiftui
How to Attach Debugger to iOS App After Launch
Check If Uicolor Is Dark or Bright
Usage of Protocols as Array Types and Function Parameters in Swift
How to Disable/Enable the Sleep Mode Programmatically in iOS
Since Xcode 8 and iOS10, Views Are Not Sized Properly on Viewdidlayoutsubviews
How to Create a Simple Checkbox in iOS
Objective-C 101 (Retain VS Assign) Nsstring
Using Custom Font in a Uiwebview
Nscache Doesn't Work with All Images When Loading for the First Time
How to Show a Collectionview Like Facebook Upload Image Using Swift 4
Cannot Subscript a Value of Type '[Nsobject:Anyobject]' with an Index of Type 'String'
Swiftui - iOS 13 Uiviewrepresentable of Wkwebview Gets Thread 1: Exc_Breakpoint Crash
In Swift, How to Let Other Apps Continue to Play Audio When My App Is Open