如何以编程方式从视图控制器移动到故事板中的其他视图控制器

时间:2012-12-07 10:21:57

标签: iphone objective-c

我在故事板上创建了两个视图控制器我想以编程方式从一个视图控制器转到另一个视图控制器。我在一个视图控制器中创建了一个按钮。我知道从一个视图控制器转到另一个视图控制器非常容易,只需将按钮从oneviewcontroller拖到第二个视图控制器。但我想以编程方式进行,因为如果条件为true,我在按钮中有if和else条件然后我想将它移动到下一个视图控制器这是代码。

-(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 
    {

    }
}

3 个答案:

答案 0 :(得分:3)

将第一个视图控制器中的行拖动到第二个并设置segue的标识符:

.m文件中:

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

}

答案 1 :(得分: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
     }
}

更新:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle: nil]; 
SecondViewController *objSecondViewController = [storyboard instantiateViewControllerWithIdentifier:@"SecondViewController"];
[self.navigationController pushViewController:objSecondViewController animated:YES];

答案 2 :(得分:0)

SecondViewController *secondviewController = [[SecondViewController alloc] init];
[self presentviewController:secondviewController animation:YES completion:nil];

希望这会有所帮助......