From 380349c4d82bce0397fe85d614d0d289ec05f7bf Mon Sep 17 00:00:00 2001 From: "matthias.ringwald@gmail.com" Date: Sun, 6 Jan 2013 00:10:28 +0000 Subject: [PATCH] fix Preferences for iOS 3 (regression) --- PrefsBundle/PrefsViewController.m | 112 ++++++++++++++++++++---------- 1 file changed, 75 insertions(+), 37 deletions(-) diff --git a/PrefsBundle/PrefsViewController.m b/PrefsBundle/PrefsViewController.m index 2a76956bd..a9f144bbc 100644 --- a/PrefsBundle/PrefsViewController.m +++ b/PrefsBundle/PrefsViewController.m @@ -32,8 +32,8 @@ // adapted from Ryan Petrich's Activator preferences bundle // https://github.com/rpetrich/libactivator/blob/master/Preferences.m - #import +#include #import "BluetoothTableViewAdapter.h" #import "BluetoothController.h" @@ -42,6 +42,9 @@ @interface PSViewController : NSObject - (id)initForContentSize:(CGSize)size; - (id)navigationItem; +- (void)viewWillBecomeVisible:(void *)source; +- (void)viewWillAppear:(BOOL)animated; +- (void)viewDidDisappear:(BOOL)animated; @end @interface PSViewController (OS32) @@ -58,6 +61,7 @@ BluetoothController *bluetoothController; UIView *_wrapperView; // for < 3.2 UITableView *tableView; + BOOL initialized; @end @implementation BluetoothPSViewController @@ -65,6 +69,7 @@ - (id)initForContentSize:(CGSize)size { // NSLog(@"initForContentSize"); + initialized = NO; if ([PSViewController instancesRespondToSelector:@selector(initForContentSize:)]) { if ((self = [super initForContentSize:size])) { CGRect frame; @@ -93,6 +98,8 @@ [_wrapperView release]; + initialized = NO; + [super dealloc]; } @@ -101,6 +108,53 @@ return [super view] ? [super view] : _wrapperView; } +-(void)myInit{ + + if (initialized) return; + + initialized = YES; + + // NSLog(@"myInit"); + + ((UINavigationItem*)[super navigationItem]).title = @"BTstack"; + + UIView *view = [self view]; + + tableView = [[UITableView alloc] initWithFrame:view.bounds style:UITableViewStyleGrouped]; + tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; + + tableViewAdapter = [[BluetoothTableViewAdapter alloc] initWithTableView:tableView]; + tableView.dataSource = tableViewAdapter; + tableView.delegate = tableViewAdapter; + + [view addSubview:tableView]; + + [[BluetoothController sharedInstance] setListener:tableViewAdapter]; + [[BluetoothController sharedInstance] open]; + + // register for backgrounding events on iOS 4+ + NSString ** pUIApplicationDidEnterBackgroundNotification = nil; + NSString ** pUIApplicationWillEnterForegroundNotification = nil; + + pUIApplicationDidEnterBackgroundNotification = dlsym(RTLD_DEFAULT, "UIApplicationDidEnterBackgroundNotification"); + pUIApplicationWillEnterForegroundNotification = dlsym(RTLD_DEFAULT, "UIApplicationWillEnterForegroundNotification"); + + if (!pUIApplicationDidEnterBackgroundNotification) return; + if (!pUIApplicationWillEnterForegroundNotification) return; + + [[NSNotificationCenter defaultCenter] + addObserver:self + selector:@selector(didEnterBackground:) + name:*pUIApplicationDidEnterBackgroundNotification + object:nil]; + + [[NSNotificationCenter defaultCenter] + addObserver:self + selector:@selector(willEnterForeground:) + name:*pUIApplicationWillEnterForegroundNotification + object:nil]; +} + -(void)didEnterBackground:(id)object{ // NSLog(@"didEnterBackground"); // close connection to BTdaemon @@ -110,53 +164,37 @@ -(void)willEnterForeground:(id)object{ // NSLog(@"willEnterForeground"); // open connection to BTdaemon + [self myInit]; [[BluetoothController sharedInstance] open]; } --(void)viewWillAppear:(BOOL)animated { + +- (void)viewWillAppear:(BOOL)animated +{ // NSLog(@"viewWillAppear"); - // open connection to BTdaemon + [super viewWillAppear:animated]; + [self myInit]; [[BluetoothController sharedInstance] open]; } --(void) viewDidDisappear:(BOOL)animated { +- (void)viewWillBecomeVisible:(void *)source +{ + // NSLog(@"viewWillBecomeVisible %@", source); + [super viewWillBecomeVisible:source]; + [self myInit]; +} + +-(void)viewDidDisappear:(BOOL)animated { // NSLog(@"viewDidDisappear"); // close connection to BTdaemon [[BluetoothController sharedInstance] close]; + [super viewDidDisappear:animated]; } --(void)viewDidLoad { - // setup view - // NSLog(@"initialize"); - - ((UINavigationItem*)[super navigationItem]).title = @"BTstack"; - - UIView *view = [self view]; - - tableView = [[UITableView alloc] initWithFrame:view.bounds style:UITableViewStyleGrouped]; - tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; - - tableViewAdapter = [[BluetoothTableViewAdapter alloc] initWithTableView:tableView]; - tableView.dataSource = tableViewAdapter; - tableView.delegate = tableViewAdapter; - - [view addSubview:tableView]; - - // setup Bluetooth Controller - [[BluetoothController sharedInstance] setListener:tableViewAdapter]; - - // register for backrounding events - [[NSNotificationCenter defaultCenter] - addObserver:self - selector:@selector(didEnterBackground:) - name:UIApplicationDidEnterBackgroundNotification - object:nil]; - - [[NSNotificationCenter defaultCenter] - addObserver:self - selector:@selector(willEnterForeground:) - name:UIApplicationWillEnterForegroundNotification - object:nil]; +-(void)viewDidLoad +{ + // NSLog(@"viewDidLoad"); + [super viewDidLoad]; + [self myInit]; } - @end