【问题标题】:Load A View Controller programmatically from a UIView从 UIView 以编程方式加载视图控制器
【发布时间】:2018-01-19 01:30:54
【问题描述】:

我在一个类中使用以下内容来调用 UIViewController。 UIViewController 加载,但加载后变得无响应。

let storyboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)

let homeC = storyboard.instantiateViewController(withIdentifier: 
"ViewJoeProfileController") as? ViewJoeProfileController
    if homeC != nil {
        homeC!.view.frame = (self.window!.frame)
        self.window!.addSubview(homeC!.view)
        self.window!.bringSubview(toFront: homeC!.view)
    } 
}

任何让 UIViewController 响应加载的建议都会有帮助!

【问题讨论】:

  • 你为什么用window?如果你想从另一个视图控制器中显示一个视图控制器,你可以简单地执行这个函数 present(VC, animated: true: completion: nil)developer.apple.com/documentation/uikit/uiviewcontroller/…
  • 检查这个Each window typically has a single root view object (managed by a corresponding view controller) that contains all of the other views representing your content. Using a single root view simplifies the process它来自苹果!!。我觉得你做错了什么。 developer.apple.com/library/content/documentation/WindowsViews/…
  • 因此,如果您想推送/弹出VC,您可能会从另一个VC, navigationController or TabbarController 执行此操作...您可能不需要处理窗口,除非您设置@987654328 @

标签: ios swift uiview viewcontroller


【解决方案1】:

如果你特别想添加到窗口中,你应该以正确的方式添加整个 ViewController:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
    guard let window = UIWindow(frame: UIScreen.mainScreen().bounds) else { return true }
    if let homeC = storyboard.instantiateViewController(withIdentifier: "ViewJoeProfileController") as? ViewJoeProfileController {
        window.rootViewController = mainViewController
        window.makeKeyAndVisible()
    }

    return true
}

但老实说,这种结构似乎仍然不正确。为什么要向窗口添加视图,而不是使用初始视图控制器,然后添加到它的视图,或者转入不同的视图控制器?。

【讨论】:

    【解决方案2】:

    尝试使用这个我需要制作根视图控制器

    let storyBoard = UIStoryboard.init(name: "Main", bundle: nil)
    let vc = storyBoard.instantiateViewController(withIdentifier: "ViewJoeProfileController") as! ViewJoeProfileController
    let nav = UINavigationController(rootViewController: vc)
    nav.isNavigationBarHidden = true
    self.window?.rootViewController=nav
    self.window?.makeKeyAndVisible()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-08
      • 2016-05-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多