我有这段代码:
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)
}
我的问题是,registrazioneButton
中的RegistrazioneViewController
在我点击它时不会触发事件(方法registrazione()
,我不明白为什么。
要将view controller
从start class
更改为RegistrazioneViewController
,请使用flipRightViewController
课程中的DefaultFunction
方法。
我哪里错了?
答案 0 :(得分:0)
目前还不清楚所需的设计是什么,但flipViewController
并未使视图控制器层次结构与视图层次结构保持同步。因此,孩子的视图控制器似乎超出了范围。即使你把它放在范围内(通过添加你自己的强引用),视图层次结构也会与视图控制器层次结构不同步。
使用翻转动画呈现第二个视图控制器的最简单方法是:
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)
})
注意,顺序是:
addChildViewController
didMoveToParentViewController
另外,当你想删除它时,序列是:
willMoveToParentViewController(nil)
removeFromParentViewController()
如果这个子视图控制器正在接管完整的chrome,那么现在你可能会坚持使用这个答案的顶部显示视图控制器。如果您不想覆盖整个屏幕,则可以使用自定义modalPresentationStyle
。
答案 1 :(得分:-1)
请尝试以下代码。让我知道它是否有效
self.registrazioneButton!.addTarget(self, action: "registrazione:", forControlEvents: .TouchUpInside)
func registrazione(button: UIButton) {
// do something
}