谁能向我解释为什么这不起作用?我认为这是一个非常简单的问题,但由于某种原因我没有得到它。
我的问题:我的 firstViewController 中有一个值为“Hello!”的 NSString。现在我想在我的 secondViewController 中显示这个值,但输出为空。
这是我的 NSLog
2013-03-09 foo[95381:c07] value of foo in BITfirstViewcontroller: Hello!
2013-03-09 foo[95381:c07] value of foo in BITsecondViewcontroller: (null)
BITAppDelegate.m
#import "BITAppDelegate.h"
#import "BITfirstViewController.h"
@implementation BITAppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
BITfirstViewController *fvc = [[BITfirstViewController alloc]init];
// Create navigationController and set InputViewController as root for navController
UINavigationController *navController = [[UINavigationController alloc]initWithRootViewController:fvc];
[self.window setRootViewController:navController];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
BITfirstViewController.h
#import <UIKit/UIKit.h>
@interface BITfirstViewController : UIViewController
@property (strong, nonatomic) NSString *foo;
-(IBAction)showSecondView:(id)sender;
@end
BITfirstViewController.m
#import "BITfirstViewController.h"
#import "BITsecondViewController.h"
@interface BITfirstViewController ()
@end
@implementation BITfirstViewController
@synthesize foo;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
foo = [NSString stringWithFormat:@"Hello!"];
NSLog(@"value of foo in BITfirstViewcontroller: %@",foo);
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:@selector(showSecondView:)
forControlEvents:UIControlEventTouchDown];
[button setTitle:@"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[self.view addSubview:button];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(IBAction)showSecondView:(id)sender;
{
BITsecondViewController *svc = [[BITsecondViewController alloc]init];
[self.navigationController pushViewController:svc animated:YES];
}
@end
BITsecondViewController.h
#import <UIKit/UIKit.h>
@class BITfirstViewController;
@interface BITsecondViewController : UIViewController
@property (nonatomic, retain) BITfirstViewController *fvc;
@end
BITsecondViewController.m
#import "BITsecondViewController.h"
#import "BITfirstViewController.h"
@interface BITsecondViewController ()
@end
@implementation BITsecondViewController
@synthesize fvc;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
NSLog(@"value of foo in BITsecondViewcontroller: %@", fvc.foo);
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@结尾