ios: use notifications instead of class methods (maybe tidier)
authorkoda
Wed, 28 Mar 2012 00:42:57 +0200
changeset 6829 60b039d88dab
parent 6828 6a5d33bff0b0
child 6830 d2df090453aa
ios: use notifications instead of class methods (maybe tidier)
project_files/HedgewarsMobile/Classes/MapConfigViewController.m
project_files/HedgewarsMobile/Classes/SchemeWeaponConfigViewController.h
project_files/HedgewarsMobile/Classes/SchemeWeaponConfigViewController.m
--- a/project_files/HedgewarsMobile/Classes/MapConfigViewController.m	Mon Mar 26 20:29:37 2012 -0400
+++ b/project_files/HedgewarsMobile/Classes/MapConfigViewController.m	Wed Mar 28 00:42:57 2012 +0200
@@ -21,7 +21,6 @@
 
 #import "MapConfigViewController.h"
 #import <QuartzCore/QuartzCore.h>
-#import "SchemeWeaponConfigViewController.h"
 
 #define scIndex         self.segmentedControl.selectedSegmentIndex
 #define isRandomness()  (segmentedControl.selectedSegmentIndex == 0 || segmentedControl.selectedSegmentIndex == 2)
@@ -309,7 +308,7 @@
             mission = @"";
             [self sliderChanged:nil];
             self.slider.enabled = YES;
-            [SchemeWeaponConfigViewController fillInstanceSections];
+            [[NSNotificationCenter defaultCenter] postNotificationName:@"fillsections" object:nil];
             break;
 
         case 1: // Map
@@ -318,7 +317,7 @@
             staticmap = @"map Bamboo";
             mission = @"";
             self.slider.enabled = NO;
-            [SchemeWeaponConfigViewController fillInstanceSections];
+            [[NSNotificationCenter defaultCenter] postNotificationName:@"fillsections" object:nil];
             break;
 
         case 2: // Maze
@@ -327,7 +326,7 @@
             mission = @"";
             [self sliderChanged:nil];
             self.slider.enabled = YES;
-            [SchemeWeaponConfigViewController fillInstanceSections];
+            [[NSNotificationCenter defaultCenter] postNotificationName:@"fillsections" object:nil];
             break;
 
         case 3: // Mission
@@ -336,7 +335,7 @@
             staticmap = @"map Bamboo";
             mission = @"";
             self.slider.enabled = NO;
-            [SchemeWeaponConfigViewController emptyInstanceSections];
+            [[NSNotificationCenter defaultCenter] postNotificationName:@"emptysections" object:nil];
             break;
 
         default:
--- a/project_files/HedgewarsMobile/Classes/SchemeWeaponConfigViewController.h	Mon Mar 26 20:29:37 2012 -0400
+++ b/project_files/HedgewarsMobile/Classes/SchemeWeaponConfigViewController.h	Wed Mar 28 00:42:57 2012 +0200
@@ -53,7 +53,7 @@
 @property (nonatomic,retain) UISegmentedControl *topControl;
 @property (nonatomic,assign) BOOL sectionsHidden;
 
-+(void) fillInstanceSections;
-+(void) emptyInstanceSections;
+-(void) fillSections;
+-(void) emptySections;
 
 @end
--- a/project_files/HedgewarsMobile/Classes/SchemeWeaponConfigViewController.m	Mon Mar 26 20:29:37 2012 -0400
+++ b/project_files/HedgewarsMobile/Classes/SchemeWeaponConfigViewController.m	Wed Mar 28 00:42:57 2012 +0200
@@ -26,8 +26,6 @@
 #define LABEL_TAG 57423
 #define TABLE_TAG 45657
 
-static SchemeWeaponConfigViewController *controllerInstance;
-
 @implementation SchemeWeaponConfigViewController
 @synthesize listOfSchemes, listOfWeapons, listOfScripts, lastIndexPath_sc, lastIndexPath_we, lastIndexPath_lu,
             selectedScheme, selectedWeapon, selectedScript, scriptCommand, topControl, sectionsHidden;
@@ -138,7 +136,16 @@
     [aTableView release];
 
     [super viewDidLoad];
-    controllerInstance = self;
+
+    // display or hide the lists, driven by MapConfigViewController
+    [[NSNotificationCenter defaultCenter] addObserver:self
+                                             selector:@selector(fillSections)
+                                                 name:@"fillsections"
+                                               object:nil];
+    [[NSNotificationCenter defaultCenter] addObserver:self
+                                             selector:@selector(emptySections)
+                                                 name:@"emptysections"
+                                               object:nil];
 }
 
 #pragma mark -
@@ -308,37 +315,37 @@
 }
 
 #pragma mark -
-#pragma mark called externally to empty or fill the sections completely
-+(void) fillInstanceSections {
-    if (controllerInstance.sectionsHidden == YES) {
-        controllerInstance.sectionsHidden = NO;
+#pragma mark called by an NSNotification to empty or fill the sections completely
+-(void) fillSections {
+    if (self.sectionsHidden == YES) {
+        self.sectionsHidden = NO;
         NSIndexSet *sections = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, 1)];
-        UITableView *aTableView = (UITableView *)[controllerInstance.view viewWithTag:TABLE_TAG];
+        UITableView *aTableView = (UITableView *)[self.view viewWithTag:TABLE_TAG];
         [aTableView insertSections:sections withRowAnimation:UITableViewRowAnimationFade];
         aTableView.scrollEnabled = YES;
-        [[controllerInstance.view viewWithTag:LABEL_TAG] removeFromSuperview];
+        [[self.view viewWithTag:LABEL_TAG] removeFromSuperview];
     }
 }
 
-+(void) emptyInstanceSections {
-    if (controllerInstance.sectionsHidden == NO) {
-        controllerInstance.sectionsHidden = YES;
+-(void) emptySections {
+    if (self.sectionsHidden == NO) {
+        self.sectionsHidden = YES;
         NSIndexSet *sections = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, 1)];
-        UITableView *aTableView = (UITableView *)[controllerInstance.view viewWithTag:TABLE_TAG];
+        UITableView *aTableView = (UITableView *)[self.view viewWithTag:TABLE_TAG];
         [aTableView deleteSections:sections withRowAnimation:UITableViewRowAnimationFade];
         aTableView.scrollEnabled = NO;
 
-        CGRect frame = CGRectMake(0, 0, controllerInstance.view.frame.size.width * 80/100, 60);
+        CGRect frame = CGRectMake(0, 0, self.view.frame.size.width * 80/100, 60);
         UILabel *theLabel = [[UILabel alloc] initWithFrame:frame
                                                   andTitle:NSLocalizedString(@"Missions don't need further configuration",@"")];
-        theLabel.center = CGPointMake(controllerInstance.view.frame.size.width/2, controllerInstance.view.frame.size.height/2);
+        theLabel.center = CGPointMake(self.view.frame.size.width/2, self.view.frame.size.height/2);
         theLabel.numberOfLines = 2;
         theLabel.tag = LABEL_TAG;
         theLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth |
                                     UIViewAutoresizingFlexibleTopMargin |
                                     UIViewAutoresizingFlexibleBottomMargin;
 
-        [controllerInstance.view addSubview:theLabel];
+        [self.view addSubview:theLabel];
         [theLabel release];
     }
 }
@@ -364,6 +371,7 @@
 }
 
 -(void) viewDidUnload {
+    [[NSNotificationCenter defaultCenter] removeObserver:self];
     self.listOfSchemes = nil;
     self.listOfWeapons = nil;
     self.listOfScripts = nil;