Сбои приложения и утечки памяти повсюду - PullRequest
0 голосов
/ 28 апреля 2011

Мое приложение некоторое время нормально работает в симуляторе, хотя после нескольких касаний оно вылетает и горит.

Вот код

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

}

Итак, что я здесь не так делаю? Любая помощь будет высоко оценена. Thanx

1 Ответ

0 голосов
/ 28 апреля 2011

Ну, Энди, твой код определенно говорит о том, что ты новичок ... Для реализации твоего требования есть гораздо более чистые и безопасные методы. Для многооконного приложения обычно все используют либо UINavigationController , либо UITabBarController . В вашем случае UINavigationController - ваш друг, и если вы можете потратить один час , этот учебник поможет вам начать работу. Я рекомендую вам научиться использовать этот метод, кроме продолжения того, что вы делаете сейчас ... и прекрати читать этот ответ ..

Хорошо, теперь ваш код .. Это первая часть ..

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

Я переписываю твой код таким образом ...

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

теперь вы выпускаете myView после добавления его в UIWindow. Если вы прокомментируете эту строку, я думаю, что ваш сбой пойдет .. Обычно UIViewController, добавленный в UIWindow, высвобождается в dealloc файла делегата приложения.

Теперь твоя анимация ..

// 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"];

Снова .. забудьте этот код и прочитайте UINavigationController .. Вы там все делаете неправильно ..

...