0

I have programmatically constructed a UIButton in view1 which is currently displayed. The goal is when the button is clicked, view1 will switch to view2. I know how to achieve this by using segue, but since I can't control-drag the UIButton into my .m file, how do I make this work?

Gherbi Hicham
  • 2,416
  • 4
  • 26
  • 41
GiddensA
  • 21
  • 7

2 Answers2

0

Add a target to your button

[myButton addTarget:self 
             action:@selector(myAction) 
   forControlEvents:UIControlEventTouchUpInside];

and then segue

- (void)myAction {
   [self performSegueWithIdentifier:"identifier" sender:nil];
}
Wyetro
  • 8,439
  • 9
  • 46
  • 64
0
[button addTarget:self
           action:@selector(buttonPressed:)
 forControlEvents:UIControlEventTouchUpInside];

then implement a method in that view controller like so: (assign an identifier to the segue in the storyboard)

- (void)buttonPressed:(id)sender
{
    [self performSegueWithIdentifier:@"segueName" sender:nil];
}

or using the identifier of a view controller in the storyboard

- (void)buttonPressed:(id)sender
{
    UIViewController *viewController = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewController"];
    [self presentViewController:viewController animated:YES completion:nil];
}
quad16
  • 184
  • 5
  • 20