-1

I have create two view controller onto storyboard I wanted to go from one view controller to another view controller programmatically. I have create a button in one view controller. I know it very easy to go from one view controller to another just drag line from button from oneviewcontroller to second view controller. but I want to do it programmatically because I have if and else conditions in button when if condition true then I want to move it to next view controller this is the code.

-(IBAction)loadView:(id)sender {
    if([username.text isEqualToString:@"adnan"] && [password.text isEqualToString:@"bhatti123"] )
    {
        // here I want to write that code which reach me to second view controller which I implement on storyboard 

    }   
    else 
    {

    }
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
Adnan Qadir
  • 35
  • 1
  • 6

3 Answers3

3

Drag line from first view controller to second and set the identifier for segue:

In the .m file:

if([username.text isEqualToString:@"adnan"] && [password.text isEqualToString:@"bhatti123"] )
{
    [self performSegueWithIdentifier:@"nextView" sender:sender];
}   
else 
{

}
ifau
  • 2,100
  • 1
  • 21
  • 29
  • you're great man you know what i want love u man thanks a lot – Adnan Qadir Dec 07 '12 at 12:22
  • How to pass values to next view controller using this method? – Aniruddha Aug 08 '14 at 05:02
  • @Aniruddha, you need write method `prepareForSegue:sender:` in first view controller and get `destinationViewController`, which is an instance of next view controller. You can access its properties and pass values. ([Example](http://stackoverflow.com/a/8597092/683917)). – ifau Aug 08 '14 at 17:28
  • Thanks for the reply :). I got to know that `[performSegue.....]` will in turn calls the `prepareforSegue....`. – Aniruddha Aug 09 '14 at 06:15
2
-(IBAction)loadView:(id)sender
{
      if([username.text isEqualToString:@"adnan"] && [password.text isEqualToString:@"bhatti123"] )
      {
         // here i want to write that code which reach me to second view controller which i implement on storyboard 
            SecondViewController *objSecondViewController = [[SecondViewController alloc]initWithNibName:@"SecondViewController" bundle:nil];
            [self.navigationController pushViewController:objSecondViewController animated:YES];
            [objSecondViewController release];
     }   
     else 
     { 
        //write your else part code here
     }
}

UPDATE :

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle: nil]; 
SecondViewController *objSecondViewController = [storyboard instantiateViewControllerWithIdentifier:@"SecondViewController"];
[self.navigationController pushViewController:objSecondViewController animated:YES];
Paras Joshi
  • 20,427
  • 11
  • 57
  • 70
  • BUT HOW I CAN KNOW ON STORYBOARD THAT THIS IS THE NIB FILE ON STORYBOARD THERE ARE MORE THEN ONE VIEW CONTROLLER ON STORYBOARD THEN HOW I INIT SECONDVIEWCONTROLLER WITH INITWITHNIBNAME – Adnan Qadir Dec 07 '12 at 10:25
  • then simply set the class and push or present it.. also when you create nib at that time you give the name to the nib right?? – Paras Joshi Dec 07 '12 at 10:26
  • @AdnanQadir also see this answer dude http://stackoverflow.com/questions/7889510/ios-5-storyboard-programmatically-determine-path :) – Paras Joshi Dec 07 '12 at 10:31
  • can i need to define property for storyboard secondviewcontroller dude – Adnan Qadir Dec 07 '12 at 10:49
  • @AdnanQadir Please fix your caps lock key. – Mick MacCallum Dec 07 '12 at 10:50
  • no instead of it use your viewcontroller name dude try and do something with this code – Paras Joshi Dec 07 '12 at 10:51
  • i set set breakpoint and run the debugger and debugger giving sigabrd error at this line [self.navigationController pushViewController:objSecondViewController animated:YES]; – Adnan Qadir Dec 07 '12 at 10:53
  • for this type of issues see this tutorial http://iphonedevelopment.blogspot.com/2009/03/debugging.html but i think u not assign name of class or instance properly dude.. just see the code and set class which you want to go to nextview... – Paras Joshi Dec 07 '12 at 10:57
  • also refer this one.. http://stackoverflow.com/questions/7889510/ios-5-storyboard-programmatically-determine-path – Paras Joshi Dec 07 '12 at 11:00
  • @ParasJoshi how to pass values to next view controller using this method? I tried `SecondViewController *sc = [[SecondViewController alloc] init]; sc.strName = @"Abcd"; [self presentviewController:sc animation:YES completion:nil];` – Aniruddha Aug 08 '14 at 05:02
  • @Aniruddha you used storyboard in your project ? – Paras Joshi Aug 08 '14 at 05:09
  • Yes, I'm using Storyboard. I have 2 classes A and B. From A, I want to pass the value to B. This is how I done it but it is not working. `B *bb = [[B alloc]init]; b.strName = @"Abcd"; [self performSegueWithIdentifier:@"segueName" sender:nil];` – Aniruddha Aug 08 '14 at 05:12
  • @Aniruddha try it out this one in `- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender` method `B *bb=segue.destinationViewController;b.strName = @"Abcd";` – Paras Joshi Aug 08 '14 at 10:30
  • I got to know how to do it. `[performSegueWithI....]` in turn calls `prepareForSegue...`. Thanks for the reply :) – Aniruddha Aug 08 '14 at 10:53
0
SecondViewController *secondviewController = [[SecondViewController alloc] init];
[self presentviewController:secondviewController animation:YES completion:nil];

Hope this helps...

lakshmen
  • 28,346
  • 66
  • 178
  • 276
  • BUT HOW I CAN KNOW ON STORYBOARD THAT THIS IS THE NIB FILE ON STORYBOARD THERE ARE MORE THEN ONE VIEW CONTROLLER ON STORYBOARD THEN HOW I INIT SECONDVIEWCONTROLLER WITH INITWITHNIBNAME – Adnan Qadir Dec 07 '12 at 10:26
  • dear there are more then one view controller on storyboard i don't wanna create new uiview using object – Adnan Qadir Dec 07 '12 at 10:27