CS185c
Chris Pollett
Mar 7, 2012
@class SwitchViewController; @interface AppDelegate : UIResponder <UIApplicationDelegate> @property (strong, nonatomic) UIWindow *window; @property (strong, nonatomic) SwitchViewController * switchViewController; @end
#import "AppDelegate.h" #import "SwitchViewController.h" @implementation AppDelegate @synthesize window = _window; @synthesize switchViewController; - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Override point for customization after application launch. self.switchViewController = [[SwitchViewController alloc] initWithNibName:@"SwitchView" bundle:nil]; UIView *switchView = self.switchViewController.view; CGRect switchViewFrame = switchView.frame; switchViewFrame.origin.y += [UIApplication sharedApplication].statusBarFrame.size.height; switchView.frame = switchViewFrame; [self.window addSubview:switchView]; self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible]; return YES; }
#import <UIKit/UIKit.h> @class BlueViewController; //similar to declaring something external in C @class YellowViewController; @interface SwitchViewController : UIViewController @property (strong, nonatomic) YellowViewController *yellowViewController; @property (strong, nonatomic) BlueViewController *blueViewController; -(IBAction) switchViews:(id) sender; @end
#import "SwitchViewController.h"
#import "BlueViewController.h"
#import "YellowViewController.h"
@implementation SwitchViewController
@synthesize blueViewController;
@synthesize yellowViewController;
// ...
- (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.
if(self.blueViewController.view.superview == nil) {
self.blueViewController = nil;
} else {
self.yellowViewController = nil;
}
}
// ...
- (void)viewDidLoad {
// set up blue controller
self.blueViewController = [[BlueViewController alloc]
initWithNibName:@"BlueView" bundle:nil];
// initial use blue view
[self.view insertSubview:self.blueViewController.view atIndex: 0];
//index 0 behind everything
[super viewDidLoad];
}
- (void) switchViews:(id)sender
{
if(self.yellowViewController.view.superview == nil)
{
if(self.yellowViewController == nil) {
self.yellowViewController = [[YellowViewController alloc]
initWithNibName:@"YellowView" bundle:nil];
}
[self.blueViewController.view removeFromSuperview];
[self.view insertSubview:self.yellowViewController.view atIndex:0];
} else {
//check if not Blue View
if(self.blueViewController == nil) {
self.blueViewController = [[BlueViewController alloc]
initWithNibName:@"BlueView" bundle:nil];
}
[self.yellowViewController.view removeFromSuperview];
[self.view insertSubview:self.blueViewController.view atIndex:0];
}
}
//... rest of boilerplate code unchanged
@end
#import <UIKit/UIKit.h> @interface BlueViewController : UIViewController -(IBAction) blueButtonPressed: (id) sender; @end
#import "BlueViewController.h"
@implementation BlueViewController
-(IBAction) blueButtonPressed:(id) sender
{
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"Blue View Button Pressed"
message:@"You pressed the button on the blue view"
delegate:nil
cancelButtonTitle:@"Yep, I did"
otherButtonTitles:nil];
[alert show];
}
//... unchanged boiler plate
@end
- (void) switchViews:(id)sender
{
[UIView beginAnimations:@"View Flip" context:nil];
[UIView setAnimationDuration:1.25];
// next control speed at begin and end of animation
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
if(self.yellowViewController.view.superview == nil)
{
if(self.yellowViewController == nil) {
self.yellowViewController = [[YellowViewController alloc]
initWithNibName:@"YellowView" bundle:nil];
}
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache: YES];
/*
Other possibilities:
UIViewAnimationTransitionFlipFromLeft
UIViewAnimationTransitionCurlUp
UIViewAnimationTransitionCurlDown
*/
[self.blueViewController.view removeFromSuperview];
[self.view insertSubview:self.yellowViewController.view atIndex:0];
} else {
//check if not Blue View
if(self.blueViewController == nil) {
self.blueViewController = [[BlueViewController alloc]
initWithNibName:@"BlueView" bundle:nil];
}
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.view cache: YES];
[self.yellowViewController.view removeFromSuperview];
[self.view insertSubview:self.blueViewController.view atIndex:0];
}
[UIView commitAnimations];
}
GET / HTTP/1.0 newline newline
HTTP/1.1 200 OK Date: Wed, 07 Oct 2009 18:11:01 GMT Server: Apache/2.2.0 (Fedora) Last-Modified: Mon, 17 Nov 2008 23:09:56 GMT ETag: "f5678-2723-ad5ef900" Accept-Ranges: bytes Content-Length: 10019 Connection: close Content-Type: text/html ...CS Department homepage
@Override
public void onStart()
{
super.onStart();
ConnectivityManager cMgr = (ConnectivityManager)
this.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cMgr.getActiveNetworkInfo();
this.status.setText(netInfo.toString());
}
<uses-permission android:name="android.permission.INTERNET"></uses-permission> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"> The first is for general networking, the second is for the actual info we have above.