更改视图控制器后的问题 Swift

issue after change view controller Swift

我有这个代码:

start class

self.registrazioneButton = myButton(frame: CGRectMake(30, 359+41, 290, 40), title: "Sign In")
self.registrazioneButton!.addTarget(self, action: "registrazioneButtonTap:", forControlEvents: .TouchUpInside)
self.view.addSubview(registrazioneButton!)

func registrazioneButtonTap(sender: AnyObject) { //Effettua login
    var registrazione : registrazioneViewController = storyboard?.instantiateViewControllerWithIdentifier("registrazione") as! registrazioneViewController
    defaultFunction.flipRightViewController(self, destinationViewController: registrazione, duration: 1.0)
}

RegistrazioneViewController是这样的:

override func viewDidLoad() {
    super.viewDidLoad()
    self.registrazioneButton = myButton(frame: CGRectMake(30, 400, 290, 40), title: "Registrati")
    self.registrazioneButton!.addTarget(self, action: "registrazione", forControlEvents: .TouchUpInside)
    self.view.addSubview(registrazioneButton!)
}

func registrazione() {
      print("TEST")
}

DefaultFunction是这个

private static func flipViewController(currentViewController: UIViewController, destinationViewController: UIViewController,
    duration: NSTimeInterval, typeTransiction: UIViewAnimationTransition) {

    UIView.beginAnimations("View Flip", context: nil)
    UIView.setAnimationDuration(duration)
    UIView.setAnimationTransition(UIViewAnimationTransition.FlipFromRight, forView: self.view, cache: false)
    UIView.setAnimationTransition(typeTransiction, forView: currentViewController.view, cache: true)
    currentViewController.removeFromParentViewController()
    currentViewController.view.addSubview(destinationViewController.view)
    UIView.commitAnimations()
}

public static func flipRightViewController(currentViewController : UIViewController , destinationViewController : UIViewController , duration : NSTimeInterval){
    flipViewController(currentViewController, destinationViewController: destinationViewController, duration: duration, typeTransiction: UIViewAnimationTransition.FlipFromRight)
}

我的问题是,RegistrazioneViewController 中的 registrazioneButton 在我点击它时没有触发事件(方法 registrazione() 我不明白为什么。

要将 view controllerstart class 更改为 RegistrazioneViewController 我使用 DefaultFunction class.

中的方法 flipRightViewController

我哪里错了?

试试下面的代码。让我知道它是否有效

self.registrazioneButton!.addTarget(self, action: "registrazione:", forControlEvents: .TouchUpInside)

func registrazione(button: UIButton) {
    // do something
}

尚不清楚所需的设计是什么,但 flipViewController 并未使视图控制器层次结构与视图层次结构保持同步。因此,child 的视图控制器似乎超出了范围。即使您以某种方式将其保留在范围内(通过添加您自己对它的强引用),视图层次结构也会与视图控制器层次结构不同步。

用翻转动画呈现第二个视图控制器的最简单方法是:

let controller = storyboard!.instantiateViewControllerWithIdentifier("Second")
controller.modalTransitionStyle = .FlipHorizontal
showViewController(controller, sender: self)

当你关闭它时,它会翻转回来。

如果您真的想使用视图控制器包含将其呈现为当前视图的子视图,正如您的代码片段所暗示的那样,您需要执行所有必要的包含调用以保持视图层次结构与查看控制器层次结构。例如,如果你想添加一个视图控制器的视图作为另一个视图的子视图,你可以这样做:

let controller = storyboard!.instantiateViewControllerWithIdentifier("Second")
addChildViewController(controller)
UIView.transitionWithView(containerView, duration: 0.5, options: .TransitionFlipFromRight, animations:
    {
        controller.view.frame = self.containerView.bounds
        self.containerView.addSubview(controller.view)
    }, completion: { finished in
        controller.didMoveToParentViewController(self)
})

注意,顺序是:

  • 实例化child视图控制器
  • 在 parent 视图控制器上调用 addChildViewController
  • 根据需要添加子视图、配置和动画
  • 在 child 视图控制器上调用 didMoveToParentViewController

另外,当你想删除它时,顺序是:

  • 在 child 视图控制器上调用 willMoveToParentViewController(nil)
  • 删除子视图,根据需要动画化此过程
  • 在 child 视图控制器上调用 removeFromParentViewController()

如果这个 child 视图控制器接管了整个 chrome,现在您可能会坚持呈现视图控制器,如本答案顶部所示。如果您不想覆盖整个屏幕,您可以自定义 modalPresentationStyle.