2010-05-05 128 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中的窗口中,我遇到了同樣的問題,並且這個問題有效。奇怪,因爲後來我將它們從超級視圖中移除,但它仍在工作。