在视图/视图控制器之间传递变量
问题描述:
您好我是ObjectiveC/iPhonehoneSDK的新手,我正在非正式地尝试自行研究它。我基本上想要做的是从一个角度看有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 [email protected]"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
答
从我可以看到它好像你正在实现自己的导航功能视图控制器。也许你应该看看这个指南:View Controller Programming Guide
如果你使用一个UINavigationController,你只需要在用户点击十二宫时做,就是把它传递给详细视图控制器(通过设置一个属性,例如)然后使用[navigationController pushViewController:detailView animated:YES];
推视图控制器。
希望这会有所帮助!