Bronze & Silver Challenge (prepared for low memory warning)


#1

HypnoAppDelegate.m

[code]- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.

HypnosisViewController *hvc = [[HypnosisViewController alloc] init];
TimeViewController *tvc = [[TimeViewController alloc] init];
MapViewController *mvc = [[MapViewController alloc] init];
UITabBarController *tabBarController = [[UITabBarController alloc] init];

NSArray *viewControllers = [NSArray arrayWithObjects:hvc, tvc, mvc, nil];
[tabBarController setViewControllers:viewControllers];

[[self window] setRootViewController:tabBarController];

self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;

}[/code]

HypnosisViewController.h

[code]#import <Foundation/Foundation.h>
#import “HypnosisView.h”

@interface HypnosisViewController : UIViewController

@property (nonatomic)HypnosisView *v;
@property (nonatomic)UISegmentedControl *sc;

// Used for low memory warning
@property (nonatomic)NSInteger selectedSegmentIndex;

  • (void)changeCircleColor;

@end[/code]

HypnosisViewController.m

[code]#import “HypnosisViewController.h”

@implementation HypnosisViewController

@synthesize v, sc, selectedSegmentIndex;

  • (void)loadView
    {
    CGRect frame = [[UIScreen mainScreen] bounds];
    v = [[HypnosisView alloc] initWithFrame:frame];

    sc = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@“Red”, @“Green”, @“Blue”, nil]];
    [sc setSegmentedControlStyle:UISegmentedControlStyleBar];
    [sc setCenter:CGPointMake([v center].x, 20)];
    [sc addTarget:self action:@selector(changeCircleColor) forControlEvents:UIControlEventValueChanged];

    [sc setSelectedSegmentIndex:[self selectedSegmentIndex]];
    [self changeCircleColor];

    [v addSubview:sc];

    [self setView:v];
    }

  • (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self)
    {
    UITabBarItem *tbi = [self tabBarItem];

      [tbi setTitle:@"Hypnosis"];
      
      UIImage *i = [UIImage imageNamed:@"Hypno.png"];
      
      [tbi setImage:i];
      
      selectedSegmentIndex = -1;
    

    }

    return self;
    }

  • (void)viewDidLoad
    {
    [super viewDidLoad];

    NSLog(@“HypnosisViewController loaded its view”);
    }

  • (void)changeCircleColor
    {
    // Set the selectedSegmentIndex value for low memory warnings
    [self setSelectedSegmentIndex:[sc selectedSegmentIndex]];

    [v changeCircleColor:[sc selectedSegmentIndex]];
    }

@end[/code]

HypnosisView.h

[code]#import <Foundation/Foundation.h>

@interface HypnosisView : UIView

@property (nonatomic,strong) UIColor *circleColor;

  • (void)changeCircleColor:(NSInteger)colourRef;

@end[/code]

(Part of) HypnosisView.m

- (void)changeCircleColor:(NSInteger)colourRef { switch (colourRef) { case 0: [self setCircleColor:[UIColor redColor]]; break; case 1: [self setCircleColor:[UIColor greenColor]]; break; case 2: [self setCircleColor:[UIColor blueColor]]; break; default: [self setCircleColor:[UIColor lightGrayColor]]; break; } }