2013-03-09 56 views
1

任何人都可以請向我解釋爲什麼這不起作用?我認爲這是一個真正的簡單問題,但由於某種原因,我沒有得到它。來自其他UIViewController的NSString爲空

我的問題:我有一個NSString在我的firstViewController值「你好!」。現在我想在我的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. 
} 

@end

+0

您正在添加第二個視圖控制器,但您從不在其上設置「fvc」,因此它爲空。 – 2013-03-09 14:54:37

回答

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中執行操作的方式)之外,並沒有直接瞭解每個其他對象。因此,您在svc中創建了一個@property,可以讓您獲得fvc的句柄,但[[BITsecondViewController alloc]init]只會將該指針初始化爲nil。然後,您需要將該屬性設置爲指向預期的對象,在本例中爲fvc。由於svc正在從BITFirstViewController的實例內部生成,因此要通過的正確句柄是self

+0

工作!謝謝,但你願意(簡單地)解釋。 – Eloy 2013-03-09 15:02:31

+0

是的,我編輯了我的答案以包含解釋。 – 2013-03-09 15:26:08

+0

非常感謝!我知道我錯過了一些東西。我一直試圖讓這個工作幾天。我嘗試了幾種方法,但直到今天,我才意識到我的UIViewcontroller是問題所在。所以我創建了這個'svc'和'fvc'設置。我只是認爲我的NSMutableArray沒有在我的其他程序中正確設置,因爲所有條目在返回到我的'fvc'時都消失了。 – Eloy 2013-03-09 15:30:29