Passing Variables between views / view controllers
- by Dan
Hi I'm new to ObjectiveC / IPhoneSDK and I'm informally trying to study it on my own. What I'm basically trying to do is from one view there are 12 zodiac signs. When a user clicks one, it proceeds to the second view (with animation) and loads the name of the zodiac sign it clicked in a UILabel, that's it.
Here are my codes:
Lovescopes = 1st page
Horoscopes = 2nd page
Lovescopes4AppDelegate.h
#import <UIKit/UIKit.h>
#import "HoroscopesViewController.h"
#import "Lovescopes4AppDelegate.h"
@class Lovescopes4ViewController;
@interface Lovescopes4AppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
Lovescopes4ViewController *viewController;
HoroscopesViewController *horoscopesViewController;
}
-(void)loadHoroscope;
-(void)loadMainPage;
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet Lovescopes4ViewController *viewController;
@property (nonatomic, retain) HoroscopesViewController *horoscopesViewController;
@end
Lovescopes4AppDelegate.m
#import "Lovescopes4AppDelegate.h"
#import "Lovescopes4ViewController.h"
@implementation Lovescopes4AppDelegate
@synthesize window;
@synthesize viewController;
@synthesize horoscopesViewController;
-(void)loadHoroscope {
HoroscopesViewController *aHoroscopeViewController = [[HoroscopesViewController alloc] initWithNibName:@"HoroscopesViewController" bundle:nil];
[self setHoroscopesViewController:aHoroscopeViewController];
[aHoroscopeViewController release];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:window cache:YES];
[viewController.view removeFromSuperview];
[self.window addSubview:[horoscopesViewController view]];
[UIView commitAnimations];
}
-(void)loadMainPage {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:window cache:NO];
[horoscopesViewController.view removeFromSuperview];
[self.window addSubview:[viewController view]];
[UIView commitAnimations];
[horoscopesViewController release];
horoscopesViewController = nil;
}
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// Override point for customization after app launch
[window addSubview:viewController.view];
[window makeKeyAndVisible];
}
- (void)dealloc {
[viewController release];
[window release];
[super dealloc];
}
@end
Lovescopes4ViewController.h
#import <UIKit/UIKit.h>
#import "HoroscopesViewController.h"
@interface Lovescopes4ViewController : UIViewController {
HoroscopesViewController *hvc;
}
-(IBAction)loadAries;
@property (nonatomic, retain) HoroscopesViewController *hvc;
@end
Lovescope4ViewController.m
#import "Lovescopes4ViewController.h"
#import "Lovescopes4AppDelegate.h"
@implementation Lovescopes4ViewController
@synthesize hvc;
-(IBAction)loadAries {
NSString *selected =@"Aries";
[hvc loadZodiac:selected];
Lovescopes4AppDelegate *mainDelegate = (Lovescopes4AppDelegate *) [[UIApplication sharedApplication] delegate];
[mainDelegate loadHoroscope];
}
HoroscopesViewController.h
#import <UIKit/UIKit.h>
@interface HoroscopesViewController : UIViewController {
IBOutlet UILabel *zodiacLabel;
}
-(void)loadZodiac:(id)zodiacSign;
-(IBAction)back;
@property (nonatomic, retain) IBOutlet UILabel *zodiacLabel;
@end
HoroscopesViewController.m
#import "HoroscopesViewController.h"
#import "Lovescopes4AppDelegate.h"
@implementation HoroscopesViewController
@synthesize zodiacLabel;
/*
// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
// Custom initialization
}
return self;
}
*/
/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
}
*/
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
-(void)loadZodiac:(id)zodiacSign {
zodiacLabel.text = [NSString stringWithFormat: @"%@", zodiacSign];
}
-(IBAction)back {
Lovescopes4AppDelegate *mainDelegate = (Lovescopes4AppDelegate *) [[UIApplication sharedApplication] delegate];
[mainDelegate loadMainPage];
}