1

谁能向我解释为什么这不起作用?我认为这是一个非常简单的问题,但由于某种原因我没有得到它。

我的问题:我的 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.
}

@结尾

4

1 回答 1

2

在 BITfirstViewController.m 中试试这个

-(IBAction)showSecondView:(id)sender;
{

    BITsecondViewController *svc = [[BITsecondViewController alloc]init];
    svc.fvc = self;  // you need to set this property as you prepare the svc
    [self.navigationController pushViewController:svc animated:YES];
}

编辑:

您的fvcandsvc是封装的,除了使用属性(至少这是我们尝试在 OOP 中做事的方式)之外,没有关于彼此的直接知识。@property所以,你在你的中创建一个是正确的,svc它允许你拥有你的句柄fvc,但[[BITsecondViewController alloc]init]只会将该指针初始化为nil. 然后您需要将该属性设置为指向预期的对象,在本例中为fvc. 由于svc是从 的实例内部构建的BITFirstViewController,因此要传入的正确句柄是self

于 2013-03-09T14:51:58.747 回答