-1

i have a button that is created programmatic and i need to perform a segue on click that will send data with prepareForSegue, but this code wont trigger it.

UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
UIViewController * vc = [mainStoryboard instantiateViewControllerWithIdentifier:@"AddToCalendarViewController"];

[self.navigationController pushViewController:vc animated:YES];

i have tried this:

[self performSegueWithIdentifier:@"addToCalendar" sender:self];

but since the button is created grammatically i don't have any segue identifier to call, and using the following to create it did not work

UIStoryboardSegue * segue = [[UIStoryboardSegue alloc] initWithIdentifier:@"addtoCalendar" source:self destination:vc];
rob180
  • 901
  • 1
  • 9
  • 29
  • So is your problem that you aren't able to pass data or that you can't get it to segue to the next ViewController? – Bryan Linton Sep 30 '14 at 15:51
  • 1
    There are other ways to present a view controller besides a segue. – CrimsonChris Sep 30 '14 at 15:52
  • the segue works, but it wont trigger the prepareForSegue to send the data. I wanted to use segue because i wanted to keep the navigation bar from the previews controller, and that was the only away i know so far. (i started objective-c 1 month ago) – rob180 Sep 30 '14 at 15:55

2 Answers2

2

UIStoryboardSegue must not be created programmatically. Create a segue by connecting your view controller to the destination view controller in the storyboard and put its identifier to @"addToCalendar" (always in the storyboard). Then call it as you are already doing.

DeFrenZ
  • 2,172
  • 1
  • 20
  • 19
0

If you don't have an actual storyboard segue to call when the button is pressed, then you need to call addTarget:action:forControlEvents, where action is a selector to the method on target to be called.

Specifically, you first need to create a method to push the destination view controller, like so:

- (void)addToCalendar {
    DestinationiewController * vc = [[DestinationViewController alloc] initWithNibName:@"destinationViewController" bundle:nil;
    [self.navigationController pushViewController:vc animated:YES];
}

and then in your source view controller's viewDidLoad (or wherever you're programmatically instantiating the button), you need to add

[self.button addTarget:self action:@selector(@"addToCalendar") forControlEvents: UIControlEventTouchUpInside];

which will call the addToCalendar method you defined above.

NRitH
  • 13,441
  • 4
  • 41
  • 44