0

I have two view controller named ViewController and SecondViewController. From the first view controller i m trying to call my SecondViewController after 5 second. but it will giving me black screen instead of my expected view.

Here is my view controller code,

`

#import "ViewController.h"
#import "SecondViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
   [super viewDidLoad];
   [self performSelector:@selector(loadingNextView) withObject:nil afterDelay:5.0f];

}

-(void)loadingNextView{
    SecondViewController *SVC = [[SecondViewController alloc] initWithNibName:nil bundle:nil];
    SVC.view.backgroundColor = [UIColor redColor];
   [self presentViewController:SVC animated:YES completion:NULL];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end`

Please suggest. If i used button for navigation then its work but my needs is to call automatically on timer.

4 Answers4

1

If your SecondViewController is in storyboard instead of alloc init you have use instantiateViewControllerWithIdentifier

[self.storyboard instantiateViewControllerWithIdentifier:@"SecondViewController"];

Dont forget to give the identifier for the controller in the storyboard. So the method will be

-(void)loadingNextView{
    [self.storyboard instantiateViewControllerWithIdentifier:@"SecondViewController"];
    SVC.view.backgroundColor = [UIColor redColor];
   [self presentViewController:SVC animated:YES completion:NULL];
}
Anil Varghese
  • 41,329
  • 9
  • 90
  • 110
0

You need to initialize the controller exact from the storyboard.

- (void)loadingNetView
{
    UIStoryboard storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    NSString *identifier = @"SecondController"; // you need to set identifier in storyboard
    SecondViewController *controller = [storyboard instantiateViewControllerWithIdentifier:identifier];
}
Daniyar
  • 2,852
  • 2
  • 23
  • 38
0

If you are using storyboard then you cannot initialise the controller with the nib Method "initWithNibName" basically searches for an Xib file, which you do not have as you are using a storyboard, better initialise it in the following way ..

 -(void)callNextView{

    UIStoryboard * storyboard = [UIStoryboard storyboardWithName:@"yourStoryBoarddName" bundle:nil];

    yourViewController *VC = [storyboard instantiateViewControllerWithIdentifier:identifier];
   [self presentViewController:VC animated:YES completion:NULL];
}
Geet
  • 2,417
  • 2
  • 18
  • 39
0

If you are not using the storyboard, you can simply init the new controller like this

SecondViewController *SVC = [[SecondViewController alloc]init];
[SVC setBackgroundColor: [UIColor redColor]];
[self presentViewController: SVC animated: YES completion: nil];
Luca D'Alberti
  • 4,434
  • 3
  • 26
  • 41
  • My issue resolved thanks i just do with correct storyboard name – Jatin Tamakuwala Oct 30 '14 at 10:30
  • -(void)loadingNextView{ UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; NSString *identifier = @"SecondViewController"; SecondViewController *SVC = [storyboard instantiateViewControllerWithIdentifier:identifier]; //[self.storyboard instantiateViewControllerWithIdentifier:@"SecondViewController"]; [self presentViewController:SVC animated:YES completion:NULL]; } – Jatin Tamakuwala Oct 30 '14 at 10:31