How do I create a segue that can be called from a button that is created programmatically?
Here is how to set up a segue so that it can be called programmatically.
- Control drag from the ViewController icon in the first view controller to the second view controller.
- Click on the segue arrow between the two view controllers, and in the Attributes Inspector on the right, give the segue an Identifier (tableau in your case).
- Then you can trigger the segue with performSegueWithIdentifier in your code.
You can read more about setting up and using segues here.
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 set segue from button made programmatically
In your Action:
method (which you should probably rename to action:
), you can just call performSegueWithIdentifier:sender:
on the view controller that has the segue attached in the storyboard.
How to segue when a new programmatically created button is pressed?
You never set the target for the button.
Add following line in your code.
button.addTarget(self, action: "segueToCreate:", forControlEvents: UIControlEvents.TouchUpInside)
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.
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)
}
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)
Adding a segue from a programmatically defined button
I don't think you can see that button in the storyboard. It is ok to add it programmatically though. Just create a segue in the storyboard without attaching it to any particular button (just drag from one view controller to another, and create the segue). Give the segue an identified (in the attributes inspector), for example "CategorySegue". Then do this:
-(void) categoryButtonDidPress:(UIBarButtonItem *)sender {
[self performSegueWithIdentifier:@"CategorySegue" sender:nil];
}
Adding button programmatically to perform segue
In Interface Builder, create a segue.
Since you can't connect the segue to the button (it doesn't exist yet), simply connect the segue to the starting ViewController.
Then, when you create the button:
// ....
[but addTarget:self action:@selector(someMethod) forControlEvents:UIControlEventTouchUpInside];
// ....
And in the someMethod:
-(void)someMethod
{
[self performSegueWithIdentifier:@"segueIdentifier" sender:self];
}
Related Topics
Index of a Substring in a String With Swift
Error-Handling in Swift-Language
How to Move Enemy Towards a Moving Player
How to Generate a Random Number in a Range (10...20) Using Swift
Why Are Emoji Characters Like 👩👩👧👦 Treated So Strangely in Swift Strings
Getting "File Not Found" in Bridging Header When Importing Objective-C Frameworks into Swift Project
Iterate Over Collection Two At a Time in Swift
How to Fix Cocoapod .Modulemap File Not Found
How to Check If a String Contains Another String in Swift
Instantiated Optional Variable Shows as Nil in Xcode Debugger
Object X of Class Y Does Not Implement Methodsignatureforselector in Swift
Strange Swift Numbers Type Casting
What Is a Good Example to Differentiate Between Fileprivate and Private in Swift3