学习《Beginning iPhone 4 Development 》第6章关于View Switcher这个例子的问题

我在学习《Beginning iPhone 4 Development Exploring the iOS SDK》(中文版的名称为《iPhone 4与iPad开发基础教程》)这本书
有一个关于第6章View Switcher这个例子中 - (IBAction)switchViews:(id)sender 这个函数中处理动画flip效果的一个问题

请看代码

- (IBAction)switchViews:(id)sender
{
    [UIView beginAnimations:@"View Flip" context:nil];
    [UIView setAnimationDuration:1.25];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
 
    if (self.yellowViewController.view.superview == nil) //如果要换入黄色view
    {
        。。。。。 //如果没有则创建

        [UIView setAnimationTransition:
         UIViewAnimationTransitionFlipFromRight
                               forView:self.view cache:YES];    //设定动画方式
  
        [blueViewController viewWillAppear:YES]; //不明白这里为什么是 蓝色的将要出现,而不是黄色视图将要出现,(以下同)
        [yellowViewController viewWillDisappear:YES];
  
        [blueViewController.view removeFromSuperview];
        [self.view insertSubview:yellowViewController.view atIndex:0]; //去掉蓝色的,换入黄色的view

        [yellowViewController viewDidDisappear:YES];
        [blueViewController viewDidAppear:YES];
    }

 。。。。
}

以上内容是我在如下地址引用的:

http://www.cocoachina.com/bbs/simple/?t88356.html

由于我也遇到了同样的问题,所以问题我也不再描述。Cocoa China博客园升级中。。。

1、我在下面地址中找到了《Beginning iPhone 4 Development 》的源代码:

https://github.com/ronco/Beginning-iPhone-4-Development

- (IBAction)switchViews:(id)sender
{
  [UIView beginAnimations:@"View Flip" context:NULL];
  [UIView setAnimationDuration:1.25];
  [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
 
  if (self.yellowViewController.view.superview == nil) {
    if (self.yellowViewController == nil) {
      YellowViewController *yellowController = [[YellowViewController alloc] initWithNibName:@"YellowView" bundle:nil];
      self.yellowViewController = yellowController;
      [yellowController release];
    }
  
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES];
    [yellowViewController viewWillAppear:YES];
    [blueViewController viewWillDisappear:YES];
    [blueViewController.view removeFromSuperview];
    [self.view insertSubview:yellowViewController.view atIndex:0];
    [blueViewController viewDidDisappear:YES];
    [yellowViewController viewDidAppear:YES];
  }
  ...
 
2、英文原版没看到红色的四行代码;
3、这位仁兄的博客
4、我在BlueViewController和YellowViewController中的viewWillAppear,viewDidAppear,viewWillDisappear,viewDidDisappear打印输出文字,最后发现,不需要红色的四行代码,这几个函数照样被调用,只是调用的顺序为:blueViewController viewWillDisappear,yellowViewController viewWillAppear,blueViewController viewDidDisappear,yellowViewController viewDidAppear;
5、

viewWillAppear:

Notifies the view controller that its view is about to be added to a view hierarchy.

- (void)viewWillAppear:(BOOL)animated
Parameters
animated

If YES, the view is being added to the window using an animation.

Discussion

This method is called before the receiver’s view is about to be added to a view hierarchy and before any animations are configured for showing the view. You can override this method to perform custom tasks associated with displaying the view. For example, you might use this method to change the orientation or style of the status bar to coordinate with the orientation or style of the view being presented. If you override this method, you must call super at some point in your implementation.

For more information about the how views are added to view hierarchies by a view controller, and the sequence of messages that occur, see“Responding to Display-Related Notifications”.

Note: If a view controller is presented by a view controller inside of a popover, this method is not invoked on the presenting view controller after the presented controller is dismissed.

Availability
  • Available in iOS 2.0 and later.
Related Sample Code
Declared In
UIViewController.h
  综上,我认为没必要要红色的四行代码,系统会自动调用。
  注:编译环境,iOS5.0 Xcode4.2
原文地址:https://www.cnblogs.com/StarMud/p/2429424.html