#import "Untitled19ViewController.h"@implementation Untitled19ViewController/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
*//*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
}-(void)viewWillAppear:(BOOL)animated
{
float f=[[NSUserDefaults standardUserDefaults] floatForKey:@"slidervalue"];
slider.value=f;
NSLog(@"1");
}-(void)viewWillDisappear:(BOOL)animated
{
NSLog(@"2");

float f=slider.value;
[[NSUserDefaults standardUserDefaults] setFloat:f forKey:@"slidervalue"];

}
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

// Release any cached data, images, etc that aren't in use.
}- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
    [super dealloc];
}@end

解决方案 »

  1.   

    很正常,就是appdelegate的dealloc也不会调用。
    如果你要退出前干点什么的话,在applicationwillterminate中做事情
      

  2.   

    我出现了和楼主一样的问题,按Home键退出时并不调用viewWillDisappear,楼主解决这个问题了吗??请教一下
      

  3.   

    单单一个视图控制器的视图,是不会调用的- (void)viewWillDisappear:(BOOL)animated
    - (void)viewDidDisappear:(BOOL)animated 这两个的,因为视图控制器控制的视图没变,就算运行后打开应用程序出现的还是那个视图,退出到后台也一样。
      

  4.   

    程序退出就不进这个函数的!
    调用的是这个- (void)applicationWillTerminate:(UIApplication *)application
    {
        NSLog(@"%@", NSStringFromSelector(_cmd));
    }
      

  5.   

    Home键的时候并不是退出,仅仅是进入后台而已,所以不进入那个代理方法很正常。applicationDidEnterBackground这个进入后台的代理方法会被触发
      

  6.   

    ios4.0以上,一般不会调用applicationWillTerminate
    in iOS 4.0 and later, this method is called instead of the applicationWillTerminate: method when the user quits an application that supports background execution. You should use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. You should also disable updates to your application’s user interface and avoid using some types of shared system resources (such as the user’s contacts database). It is also imperative that you avoid using OpenGL ES in the background.
      

  7.   

    按home键,调用的是这个
    - (void)applicationWillTerminate:(UIApplication *)application
    {
    }
     
      

  8.   

    这个问题我也觉得奇怪,
    也许是apple的问题吧。
      

  9.   

    我尝试了一下,发现是如此:
    在网上找到其它帖子,大概也是这样说的。
    http://stackoverflow.com/questions/4289617/why-isnt-viewwilldisappear-or-viewdidappear-being-calledhttp://stackoverflow.com/questions/6583834/iphone-why-not-deallocate-in-viewwilldisappear-and-allocate-in-viewwillappear如果你的viewController是window上第一个加进去的;
    那么退出应用程序,根本不会进入viewController的viewWillDisappear, 它的dealloc同样不会进去。
    我在应用程序委托的dealloc打了断点一样不进去。但是,如果是在这个viewController的基础上,再加入新的viewController,那么新的viewController的
    viewWillDisappear是会正常调用的。总结:可能是因为效率原因。第一个进去的viewController不做这样的处理,避免再次构造。