views:

148

answers:

4

Hello everybody, I have a seachButton in the navigation bar which upon hitting calls following method:

- (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];
}

It should load SearchViewController.xib which contains a view with UISearchBar and two buttons. When I call the search method for the first time, the view appears very quickly with some weird animation, when I call it again, the animation is alright. Does anyone have a clue what could be wrong?

A: 

A UIViewController does not load its view until the view method is called. I guess the problem is the view is not loaded when you call the animation first time. You can try to modify you code like this:

if (nil == searchViewController) {
    searchViewController = [[SearchViewController alloc] initWithNibName:@"SearchViewController" bundle:nil];
    searchViewController.view;
}
zonble
unfortunately it didn't make the trick :/
BobC
A: 

Try putting the animation code in the viewDidLoad function. This ensures all assets and views from the nib file have been loaded and are able to be used by your app.

- (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];
}
chris
hmm, no change here. Could be the problem that I'm adding the superview is a UIWebView?
BobC
Forgot that viewDidLoad should be implemented in the SearchViewController.m. This means a reshuffle of some of your code. I've edited the code above to reflect this. Hope this helps.
chris
chris, still the same problems. I start to believe that something else is my code is blocking the animation :(
BobC
A: 

Try putting the code for animations and view loading in the viewDidAppear: method instead of viewDidLoad: (which Chris suggested above).

In general, I have had problems doing view related things in viewDidLoad:. But doing those in viewDidAppear: has always helped (I might be missing some subtlety that causes this behavior).

AJJ
A: 

Add both views to window in appdelegate, I had the same problem you had and this worked. Strange because later on I remove them from the superview but it is still working.

Rene