How to Create a Segue That Can Be Called from a Button That Is Created Programmatically

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.

Sample Image

You can read more about setting up and using segues here.

Segue and Button programmatically swift

Create seuge

Create Seuge

Assign identifier

Sample Image

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



Leave a reply



Submit