V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
iOS 开发实用技术导航
NSHipster 中文版
http://nshipster.cn/
cocos2d 开源 2D 游戏引擎
http://www.cocos2d-iphone.org/
CocoaPods
http://cocoapods.org/
Google Analytics for Mobile 统计解决方案
http://code.google.com/mobile/analytics/
WWDC
https://developer.apple.com/wwdc/
Design Guides and Resources
https://developer.apple.com/design/
Transcripts of WWDC sessions
http://asciiwwdc.com
Cocoa with Love
http://cocoawithlove.com/
Cocoa Dev Central
http://cocoadevcentral.com/
NSHipster
http://nshipster.com/
Style Guides
Google Objective-C Style Guide
NYTimes Objective-C Style Guide
Useful Tools and Services
Charles Web Debugging Proxy
Smore
shippo7
V2EX  ›  iDev

如何切换 UIContainerView 中的子视图?

  •  
  •   shippo7 · 2016-04-16 05:50:05 +08:00 · 3002 次点击
    这是一个创建于 2903 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我在 MainViewController 中有一个 UIContainerView 已经加载 aViewController ,我想在 aViewController 中点击一个按钮后, UIContainerView 显示 bViewController 。

    已经有如下代码: MainViewController.swift:

    func showContentController( content: UIViewController ) {
        addChildViewController( content )
        content.view.frame = containerView.frame;
        view.addSubview( content.view )
        content.didMoveToParentViewController(self)
    }
    
    func hideContentController( content: UIViewController ) {
        content.willMoveToParentViewController(nil)
        content.view.removeFromSuperview()
        content.removeFromParentViewController()
    }
    

    aViewController.swift:

    @IBAction func pushButton(sender: UIButton) {
        OffersMainViewController().hideContentController(self)
        let storyboard = UIStoryboard(name: "StoryboardID", bundle: nil)
        var vc: UIViewController = storyboard.instantiateViewControllerWithIdentifier("bViewController") as UIViewController
        OffersMainViewController().showContentController(vc)
    }
    

    这是点击按钮会报错, containerView.frame 的值为 nil

    我应该如何实现想要的功能呢?

    8 条回复    2016-04-16 18:09:27 +08:00
    shippo7
        1
    shippo7  
    OP
       2016-04-16 06:33:08 +08:00 via iPhone
    是不是 vc 之间要写 delegate ,不能直接这样调用?
    shippo7
        2
    shippo7  
    OP
       2016-04-16 08:01:14 +08:00
    就是要写 delegate ,都散了吧
    kobe1941
        3
    kobe1941  
       2016-04-16 09:18:47 +08:00
    swift 不清楚, OC 里边有个这样的方法:
    - (void)transitionFromViewController:(UIViewController *)fromViewController toViewController:(UIViewController *)toViewController duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options animations:(void (^ __nullable)(void))animations completion:(void (^ __nullable)(BOOL finished))completion NS_AVAILABLE_IOS(5_0);
    free9fw
        4
    free9fw  
       2016-04-16 10:56:21 +08:00
    frame 重新设置一下
    shippo7
        5
    shippo7  
    OP
       2016-04-16 11:55:52 +08:00
    @free9fw

    我试过手动设置 frame 也是没用的,因为直接用 MainViewController().function 这样不会回到原来的 MainViewController 中,而是生成了一个新的 MainViewController()类,其中的任何组件都没有在视图上初始化,所以 frame 是空的

    用正常的方法,两个 View Controller 之间只能通过 delegate 传值
    CommandZi
        6
    CommandZi  
       2016-04-16 17:01:16 +08:00
    CommandZi
        7
    CommandZi  
       2016-04-16 17:01:35 +08:00
    free9fw
        8
    free9fw  
       2016-04-16 18:09:27 +08:00
    @shippo7 或者直接自定义转场
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3491 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 11:01 · PVG 19:01 · LAX 04:01 · JFK 07:01
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.