swift - 使用动画更改 rootViewController 时 viewWillAppear/DidAppear 被调用两次

标签 swift cocoa-touch uiviewcontroller

这是我的动画/过渡的代码:

if let window = self.window where animated {
   window.rootViewController?.dismissViewControllerAnimated(true, completion: {
       UIView.transitionFromView(window.rootViewController!.view, toView: tabBarController.view, duration: 0.8, options: .TransitionFlipFromLeft, completion: { _ in
           window.rootViewController = tabBarController
       })
   })
}

如果没有动画代码,(只需设置rootViewControlller),就不会出现该问题。

知道为什么它会使 View 出现两次以及如何解决这个问题吗?

最佳答案

使用 UIViewController.transitionFromViewController1 ,而不是你的 UIView.transitionFromView.act 就像它:

//
//  AppDelegate.swift
//  helloapp
//
//  Created by quota on 2/18/16.
//  Copyright © 2016 liu. All rights reserved.
//

import UIKit


class ViewController1: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.greenColor()
        let button = UIButton()
        button.setTitle("transit", forState: .Normal)
        button.frame = CGRectMake(20, 20, 100,20)
        button.addTarget(self, action: "click:", forControlEvents: .TouchDown)
        view.addSubview(button)
    }
    func click(sender:UIButton!){
        let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
        if let window = appDelegate.window {
//            let v2 = ViewController2()

            if let r = window.rootViewController{
                r.transitionFromViewController(
                    appDelegate.v1!
                    ,toViewController:appDelegate.v2!,duration:0.8,options: .TransitionFlipFromLeft,animations:nil){_ in }
            }
        }
    }

}


class ViewController2: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.blueColor()
    }
    override func viewWillAppear(animated: Bool) {
        print(animated)
    }

}

class ViewController3: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.redColor()
    }
}

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    var v1 : ViewController1?
    var v2 : ViewController2?

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
        self.window!.rootViewController = ViewController3()
        v1 = ViewController1()
        v2 = ViewController2()
        v1!.view.frame = self.window!.frame
        self.window!.rootViewController?.addChildViewController(v1!)
        self.window!.rootViewController?.view.addSubview(v1!.view)
        self.window!.rootViewController?.addChildViewController(v2!)

        self.window?.makeKeyAndVisible()
        return true
    }
}

关于swift - 使用动画更改 rootViewController 时 viewWillAppear/DidAppear 被调用两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35478629/

相关文章:

objective-c - 使用 NSPredicate 过滤 NSManagedObjects 时引发异常

ios - 在不使用 Segue 的情况下在 ViewController 之间传递数据

ios - 从字符串数组填充 Collection View 单元格标签的最简单方法是什么?

if-statement - 为什么 Swift 的可选绑定(bind)在某些情况下会成功并返回 'nil'?

ios - RMUniversalAlert 错误 - UIAlertView+Blocks/UIAlertView+Blocks.h 未找到

iphone - MPMoviePlayerController 无响应

swift - 多个纹理之间的淡入淡出,SpriteKit

cocoa-touch - 更改UIWindow的rootViewController动画

ios - 以编程方式跳转到特定 View ,该 View 已经是 Storyboard的一部分

uiviewcontroller - 调整 Storyboard中 UIViewController 的大小