1

なぜこれが機能しないのか誰かに説明してもらえますか?本当に単純な問題だと思いますが、どういうわけかうまくいきません。

私の問題:firstViewControllerに値「Hello!」のNSStringがあります。次に、この値をsecondViewControllerに表示したいのですが、出力がnullです。

これは私の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];
}

編集:

あなたfvcsvcはカプセル化されており、プロパティを使用する場合を除いて、お互いについて直接の知識を持っていません(少なくとも、それがOOPで物事を実行しようとする方法です)。だから、あなたはあなたがあなたのへのハンドルを持つことを可能にする@propertyあなたの中にを作成することで正しかったが、単にそのポインタをに初期化するだろう。次に、目的のオブジェクト(この場合は。)を指すようにそのプロパティを設定する必要があります。はのインスタンス内からビルドされているため、渡す正しいハンドルはです。svcfvc[[BITsecondViewController alloc]init]nilfvcsvcBITFirstViewControllerself

于 2013-03-09T14:51:58.747 に答える