簡體   English   中英

Xcode 5 Storyboard-presentViewController不呈現我現有的Viewcontroller並顯示黑屏

[英]Xcode 5 Storyboard - presentViewController not presenting my existing Viewcontroller and giving black screen

我有兩個名為ViewController和SecondViewController的視圖控制器。 從第一個視圖控制器,即時通訊試圖5秒后調用我的SecondViewController。 但這會給我黑屏,而不是預期的視圖。

這是我的視圖控制器代碼,

`

#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`

請提出建議。 如果我使用按鈕進行導航,則它的工作是我的工作,但我的需要是按時自動調用。

如果您SecondViewController是在故事板代替alloc init你必須使用instantiateViewControllerWithIdentifier

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

不要忘記在情節提要中提供控制器的標識符 所以方法是

-(void)loadingNextView{
    [self.storyboard instantiateViewControllerWithIdentifier:@"SecondViewController"];
    SVC.view.backgroundColor = [UIColor redColor];
   [self presentViewController:SVC animated:YES completion:NULL];
}

您需要從情節提要中精確初始化控制器。

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

如果使用情節提要,則不能使用nib初始化控制器。方法“ initWithNibName”基本上會搜索Xib文件,而您使用情節提要時並沒有該文件,請按照以下方式進行初始化。

 -(void)callNextView{

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

    yourViewController *VC = [storyboard instantiateViewControllerWithIdentifier:identifier];
   [self presentViewController:VC animated:YES completion:NULL];
}

如果您不使用情節提要,則可以像這樣簡單地初始化新控制器

SecondViewController *SVC = [[SecondViewController alloc]init];
[SVC setBackgroundColor: [UIColor redColor]];
[self presentViewController: SVC animated: YES completion: nil];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM