简体   繁体   English

如何在不使用Swift中的Storyboard的情况下以编程方式加载UIViewController

[英]How to load a UIViewController programmatically without using Storyboard in Swift

I have a ViewController called MyViewController and I want to load it programmatically without a stroyboard or xib. 我有一个名为MyViewController的ViewController,我想以编程方式加载它而不使用stroyboard或xib。

As a follow up squestion, I would like to load a MyTableViewController inside a navigation controller. 作为后续问题,我想在导航控制器中加载MyTableViewController。 Again, I need to do it programmatically without using Storyboard or xib. 同样,我需要以编程方式执行它而不使用Storyboard或xib。

Please help. 请帮忙。

I was able to solve my issue by removing the Storyboard and using the following code in AppDelegate 通过删除Storyboard并在AppDelegate中使用以下代码,我能够解决我的问题

    import UIKit
    import CoreData

    @UIApplicationMain
    class AppDelegate: UIResponder, UIApplicationDelegate {

        var window: UIWindow?
        var navigationController: UINavigationController?

        func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
            // Override point for customization after application launch.
            self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
            // Override point for customization after application launch.
            self.window!.backgroundColor = UIColor.whiteColor()
            self.window!.makeKeyAndVisible()

            let myViewController: MyViewViewController? = MyViewViewController()
            self.navigationController = UINavigationController(rootViewController: myViewController!)
            self.window!.rootViewController = self.navigationController
            return true
        }
 ...
}

暂无
暂无

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

相关问题 如何从故事板以编程方式加载 UIViewController? - How to load UIViewController programmatically from storyboard? Swift-以编程方式加载情节提要 - Swift - Load storyboard programmatically 如何在不使用情节提要的情况下在iOS Swift 3中以编程方式创建UICollectionView类? - How to make UICollectionView class programmatically in iOS Swift 3 without using storyboard? 如何使用故事板中的视图以编程方式创建UIViewController - How to Create a UIViewController programmatically with view from storyboard 如何以编程方式从UINavigationController(storyboard)到UIViewController - How to segue from UINavigationController (storyboard) to UIViewController programmatically 如何使用情节提要segue将UIViewController连接到UIView,而无需任何代码? - How to connect UIViewController to UIView using storyboard segue, without any code? ios - 如何在iOS Swift 3中以编程方式推送和呈现给UIViewController而无需segue - How to push and present to UIViewController programmatically without segue in iOS Swift 3 在Storyboard中以编程方式访问UIViewController - Accessing UIViewController programmatically in Storyboard 使用StoryBoard在容器视图中加载UIViewController - Load UIViewController inside a Container View using StoryBoard 如何快速使用代码移动到现有的UIViewController(在情节提要中) - How to move to an existing UIViewController (in storyboard) with code in swift
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM