在视图/视图控制器之间传递变量

时间:2009-11-18 21:53:44

标签: iphone variables views

您好我是ObjectiveC / IPhoneSDK的新手,我正在非正式地尝试自己研究它。我基本上想要做的是从一个视图中有12个星座。当用户点击一个时,它会进入第二个视图(带动画)并加载它在UILabel中点击的星座的名称,就是这样。

以下是我的代码: Lovescopes =第1页 星座=第2页

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];
}

1 个答案:

答案 0 :(得分:0)

从我可以看到,您似乎正在实现自己的导航视图控制器功能。也许您应该查看本指南:View Controller Programming Guide

如果您使用UINavigationController,那么当用户点击黄道带时,您只需将其传递给详细视图控制器(例如通过设置属性),然后使用[navigationController pushViewController:detailView animated:YES];推送视图控制器

希望这有帮助!