简体   繁体   English

View Controller未加载Storyboard View

[英]View Controller not loading Storyboard View

I am new to iOS development. 我是iOS开发的新手。 I have a story board with 2 view controllers, I link them up to UIViewController classes, it works when I try load the second class from the first class (like a splash screen going into the main menu) but then the 'menu' does not load the view from the storyboard. 我有一个带有2个视图控制器的故事板,我将它们链接到UIViewController类,它在我尝试从第一个类加载第二个类时起作用(就像进入主菜单的启动画面)但是'菜单'没有从故事板加载视图。 It loads a black screen. 它加载黑屏。 I have assigned the class to the viewcontroller in the right hand sidebar of the storyboard and coloured the screen red to see if it loads the class, it does, but if I take the red out, it loads a black screen, not the desired screen from the storyboard. 我已经将类分配给了故事板右侧边栏中的viewcontroller,并将屏幕变为红色以查看它是否加载了类,但是如果我将红色输出,它会加载黑屏,而不是所需的屏幕从故事板。

Screen 1's (Splash)'s code: 屏幕1(Splash)的代码:

func switchScreen() {

    let secondViewController:vcMainLogin = vcMainLogin()
    self.presentViewController(secondViewController, animated: true, completion: nil)

}

override func viewDidLoad() {
    super.viewDidLoad()
    NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "switchScreen", userInfo: nil, repeats: false)    

}

Screen 2's (login / menu)'s code: 屏幕2(登录/菜单)的代码:

override func viewDidLoad() {
    super.viewDidLoad()
    self.view.backgroundColor = UIColor.redColor()
    // Do any additional setup after loading the view.
}

It is loading because the colour of the screen turns red, but when I take that out it loads black, not the screen from the storyboard. 它正在加载,因为屏幕的颜色变为红色,但当我拿出它时,它会加载黑色,而不是故事板中的屏幕。

You need to instantiate the view controller from the storyboard and not create it via the initialize function. 您需要从故事板中实例化视图控制器,而不是通过初始化函数创建它。 To do this you need to assign your view controller a storyboardID in interface builder (view -> utilities -> identity inspector). 为此,您需要在界面构建器(视图 - >实用程序 - >身份检查器)中为视图控制器分配storyboardID。 In this example I load a view controller with a storyboard ID of "VC2" from the storyboard named "MainStoryboard". 在这个例子中,我从名为“MainStoryboard”的故事板中加载一个故事板ID为“VC2”的视图控制器。

let mainStoryboard = UIStoryboard(name: "MainStoryboard", bundle: NSBundle.mainBundle())
let vc : UIViewController = mainStoryboard.instantiateViewControllerWithIdentifier("VC2") as UIViewController

Here is the equivalent objective-c code for reference 这是等效的Objective-c代码供参考

UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"mainStoryboard" bundle:[NSBundle mainBundle]];
UIViewController *vc = [mainStoryboard instantiateViewControllerWithIdentifier:@"VC2"];

I am not yet used to Swift. 我还不习惯斯威夫特。 However, it looks ot me as if you load just instanticate a brand new mcMainLogin view controller instead of initializing it from a nib file or loading it from a story board. 但是,它看起来好像你只是加载一个全新的mcMainLogin视图控制器,而不是从一个nib文件初始化它或从故事板加载它。 Naturally it would be empty (black). 当然它会是空的(黑色)。

Go back to the references or wait for somebody with Swift experience to tell you how to load the view controller from the storyboard. 回到引用或等待有Swift经验的人告诉你如何从故事板加载视图控制器。

If you create all your view controllers programmatically and do the navigation programatically then there is no need for storyboards at all. 如果以编程方式创建所有视图控制器并以编程方式进行导航,则根本不需要故事板。 You'd be far better of either complying to the storyboard concept including navigation at least in 90+ % of all navigation events or work with individutal nib/xib files. 您可以更好地遵守故事板概念,包括导航至少90%以上的导航事件或使用个性化的nib / xib文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM