0

My app runs fine for a while in simulator, although after a few touches, it crash and burn.

Here is the code

-(IBAction)extraCrisp:(id)sender
{
    myViewController *myView = [[myViewController alloc]
                                    initWithNibName:@"myViewController"
                                    bundle:nil];
    // get the view that's currently showing
    UIView *currentView = self.view;
    // get the the underlying UIWindow, or the view containing the current view
    UIView *theWindow = [currentView superview];

    UIView *newView = myView.view;

    // remove the current view and replace with myView1
    [currentView removeFromSuperview];
    [theWindow addSubview:newView];

    // set up an animation for the transition between the views
    CATransition *animation = [CATransition animation];
    [animation setDuration:0.5];
    [animation setType:kCATransitionPush];
    [animation setSubtype:kCATransitionFromLeft];
    [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];

    [[theWindow layer] addAnimation:animation forKey:@"SwitchToView1"];
    [myView release];

}

So, what am I doing wrong here? Any help would be highly praised. Thanx

Krishnabhadra
  • 34,169
  • 30
  • 118
  • 167
andy
  • 17
  • 4
  • What are you trying to do with this code, Show one view first,and on a button click bring the second view with a transition animation, right? – Krishnabhadra Apr 28 '11 at 11:22
  • Do you have a crashlog for us? – Jake Apr 28 '11 at 11:34
  • @Krishnabhadra Yes, that is exactly what I want to do. – andy Apr 28 '11 at 11:53
  • I recive the EXC_BAD_ACCESS, sry, bit of a beginner...What kind of crashlog do you need? Or where to find..Again, sry for beginner stuff. Still learning. Thank you so much. – andy Apr 28 '11 at 11:55

1 Answers1

0

Well Andy, your code certainly tells that you are a beginner..To implement your requirement there are much cleaner and safer methods available. For a multiview application, normally everyone uses either UINavigationController or UITabBarController. In your case UINavigationController is your friend and if you can spend one hour this tutorial will get you started..I recommend you learn to use those method other than continuing with the way you are doing now...and stop reading this answer..

OK now your code..This is the first part..

myViewController *myView = [[myViewController alloc]
                                    initWithNibName:@"myViewController"
                                    bundle:nil];
// get the view that's currently showing
UIView *currentView = self.view;
// get the the underlying UIWindow, or the view containing the current view
UIView *theWindow = [currentView superview];

UIView *newView = myView.view;

// remove the current view and replace with myView1
[currentView removeFromSuperview];
[theWindow addSubview:newView];

I rewrite your code this way..

myViewController *myView = [[myViewController alloc]
                                        initWithNibName:@"myViewController"
                                        bundle:nil];
UIWindow *currentWindow = [[UIApplication sharedApplication]keyWindow];
[currentWindow addSubview:myView.view];

now you are releasing myView after adding it to UIWindow. If you comment that line I think your crash will go..Normally UIViewController added to UIWindow are released in dealloc of application delegate file..

Now your animation..

// set up an animation for the transition between the views
CATransition *animation = [CATransition animation];
[animation setDuration:0.5];
[animation setType:kCATransitionPush];
[animation setSubtype:kCATransitionFromLeft];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];

[[myView layer] addAnimation:animation forKey:@"SwitchToView1"];

Again..forget this code and read up UINavigationController..You are doing everything wrong there..

Krishnabhadra
  • 34,169
  • 30
  • 118
  • 167
  • How do I add different animations other than "drop down" whilst using UINavigationController method? – andy Apr 28 '11 at 14:24
  • you are getting drop down animation for UINavigationController? Normally UINavigationController has left to right transition.. – Krishnabhadra Apr 29 '11 at 03:28
  • http://stackoverflow.com/questions/1406037/custom-animation-for-pushing-a-uiviewcontroller – Krishnabhadra Apr 29 '11 at 03:29
  • I am not getting the drop down animation, no. I replied the wrong way... I was wondering if its possible to get that effect with this kind of transitions. I followed the link youve posted and also Adams tutorial. Which works fine. Although, as I said, my wish is to animate with a drop down effect, left to right (and reverse) and down to up. I read about block animations, but Im not quite sure how to do it. Thanks – andy May 04 '11 at 22:00