2012-12-04 40 views
4

我這裏檢查的問題上堆棧溢出和我做同樣的方式,但仍然返回NULL傳遞變量空

的第一視圖

在firstviewcontroller.hi

@property (nonatomic, copy) NSString *Astring; 
在secondviewcontrolle的firstviewcontroller.m

#import "SecondViewController.h" 
... 
@synthesize Astring = _Astring; 
... 

- (IBAction)filterSearch:(id)sender { 
NSlog(@"%@",Astring) 

     } 

RM

#import firstviewcontroller.h 
... 
... 
FirstViewController *controller = [[FirstViewController alloc]initWithNibName:@"FirstViewController" bundle:nil]; 
controller.Astring = @"YES"; 

所以basicly我做一個變量在firstviewcontroller並在secondviewcontroller變量傳遞給第二個觀點,但它返回NULL總是...

是我的邏輯錯誤,或別的東西

+0

你的#import有問題,你好像倒了它們。它不會糾正這個問題,但問題會更容易理解。 – rdurand

+0

在NSLog嘗試self.Astring而不是隻是Astring – Yarlik

+0

@Yarlik 2bad它仍然是NULL – MichaelAngelo

回答

0

這是問題所在。你確實正在改變AString的字符串值,但在視圖控制器的新實例上。該行:FirstViewController *controller = [[FirstViewController alloc]initWithNibName:@"FirstViewController" bundle:nil];正在創建控制器的新實例。因此,現有控制器的視圖控制器具有相同的屬性。

你需要做的是找到一種方法來獲取firstViewController的實例。

在secondViewController.h中,添加此屬性。

#import "firstViewController.h" 
... 
@property (nonatomic, strong) firstViewController *firstController; 

然後,在secondViewController.m,你可以調用與firstController字符串的變化,其中將包含你以後的實例。我相信現在應該是這樣的:

firstController.Astring = @"YES" 

乾杯!

+0

我已經試過了,我再試一次,我放棄了哈哈tnx! – MichaelAngelo

+0

沒問題。但我同意其他人的觀點,一般來說,你的代碼沒有多大意義。我並不是想要得出任何結論,但你可能想要一本書來了解基本知識。 :-) – Josiah