Reputation: 31
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.
Upvotes: 0
Views: 937
Reputation: 3003
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];
}
Upvotes: 0
Reputation: 4859
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];
Upvotes: 0
Reputation: 2437
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];
}
Upvotes: 0
Reputation: 42977
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];
}
Upvotes: 1