2010-05-05 129 views
0

我有在命中调用下面的方法在导航栏中seachButton:UIView的动画不工作第一次

- (IBAction)search:(id)sender 
{ 
if (nil == searchViewController) 
    searchViewController = [[SearchViewController alloc] initWithNibName:@"SearchViewController" bundle:nil]; 

searchViewController.view.backgroundColor = [UIColor clearColor]; 

[UIView beginAnimations:nil context:nil]; 
[UIView setAnimationDuration:1.0]; 
[UIView setAnimationTransition:UIViewAnimationTransitionCurlDown 
         forView:searchViewController.view 
         cache:NO]; 

[self.view addSubview:searchViewController.view]; 
[UIView commitAnimations]; 
} 

应该加载SearchViewController.xib其中包含的UISearchBar和两个按钮的视图。当我第一次调用搜索方法时,视图很快出现一些奇怪的动画,当我再次调用它时,动画就没有问题。有没有人有线索可能是错的?

回答

0

UIViewController在调用view方法之前不会加载它的视图。我想问题是当你第一次调用动画的时候,视图不会被加载。你可以尝试修改你这样的代码:

if (nil == searchViewController) { 
    searchViewController = [[SearchViewController alloc] initWithNibName:@"SearchViewController" bundle:nil]; 
    searchViewController.view; 
} 
+0

不幸的是,它并没有使窍门:/ – BobC 2010-05-06 12:30:36

0

尝试把动画代码在viewDidLoad中的功能。这确保了nib文件中的所有资源和视图都已加载,并且可以被您的应用程序使用。

- (IBAction)search:(id)sender 
{ 
if (nil == searchViewController) 
    searchViewController = [[SearchViewController alloc] initWithNibName:@"SearchViewController" bundle:nil]; 

[self.view addSubview:searchViewController.view]; 
} 

-(void)viewDidLoad 
{ 
self.view.backgroundColor = [UIColor clearColor]; 
[UIView beginAnimations:nil context:nil]; 
[UIView setAnimationDuration:1.0]; 
[UIView setAnimationTransition:UIViewAnimationTransitionCurlDown 
        forView:self.view 
        cache:NO]; 
[UIView commitAnimations]; 
} 
+0

嗯,这里没有改变。可能是我添加superview的问题是一个UIWebView? – BobC 2010-05-06 12:37:17

+0

忘记了viewDidLoad应该在SearchViewController.m中实现。这意味着你的一些代码重新洗牌。我已经编辑了上面的代码来反映这一点。希望这可以帮助。 – chris 2010-05-06 17:00:44

+0

chris,仍然是同样的问题。我开始相信别的东西是我的代码阻止动画:( – BobC 2010-05-09 12:39:03

0

尝试把代码动画和视图装载在viewDidAppear:方法而不是viewDidLoad:(其克里斯上面所建议的)。

一般而言,我在viewDidLoad:中查看相关内容时遇到问题。但在viewDidAppear:这样做一直有帮助(我可能会错过一些导致此行为的微妙因素)。

1

将两个视图添加到appdelegate中的窗口中,我遇到了同样的问题,并且这个问题有效。奇怪,因为后来我将它们从超级视图中移除,但它仍在工作。