2013-11-02 63 views
0

我有一個名爲GettingHere的NSObject,它有一個NSString *內容。沒有從對象中訪問值

然後我有我編程方式創建一個按鈕,一個UIViewController如下(此按鈕如預期運行):

byAirButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
byAirButton.tag = 1; 
byAirButton.frame = CGRectMake(25, 140, 280.f, 40.f); 
UIImage *airButton = [UIImage imageNamed:@"gettingHereByAirButton.png"]; 
[byAirButton setBackgroundImage:airButton forState:UIControlStateNormal]; 
[self.view addSubview:byAirButton]; 
[byAirButton addTarget:self action:@selector(byAirButtonClicked) forControlEvents:UIControlEventTouchUpInside]; 

對於動作:@selector(byAirButtonClicked),我做到以下幾點。 gettingHere是GettingHere對象的一個​​實例。

- (void) byAirButtonClicked 
{ 
    gettingHere.content = @"This is how to get here by Air"; 
    NSLog(@"Content: %@", gettingHere.content); 
    [self performSegueWithIdentifier:@"gettingHereSegue" sender:self]; 
} 

的想法是設置爲我GettingHere對象的內容,然後就打電話,從下一個視圖(GettingHereViewController),當用戶點擊byAirButton。 此NSLog顯示正在設置內容。

在我prepareForSegue,我做了以下內容:

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    if ([segue.identifier isEqualToString:@"gettingHereSegue"]) 
    { 
     NSLog(@"Content to be passed: %@", gettingHere.content); 

     GettingHereViewController *vc = (GettingHereViewController *)segue.destinationViewController; 
     vc.gettingHere.content = gettingHere.content; 
    } 
} 

的SEGUE工作正常,但NSLog的顯示了我gettingHere對象值爲(空)。

任何人都可以告訴我我哪裏錯了嗎?我已經多次瀏覽過它,但無法弄清楚我出錯的地方。

編輯:這裏是我如何實例化GettingHere對象。

在SubNavViewController.h

#import "GettingHereContent.h" 

@interface SubNavViewController : UIViewController 
@property GettingHereContent *gettingHere; 

在SubNavViewController.m

#import "SubNavViewController.h" 
#import "GettingHereViewController.h" 

#import "GettingHereContent.h" 

@interface SubNavViewController() 
@end 

@implementation SubNavViewController 
@synthesize gettingHere; 

這裏是我如何創建GettingHere對象: GettingHere.h

#import <Foundation/Foundation.h> 
@interface GettingHereContent : NSObject 
@property (nonatomic, strong) NSString *content; 
@end 

GettingHere .m

#import "GettingHereContent.h" 
@implementation GettingHereContent 
@synthesize content; 
@end 
+0

getHere定義在哪裏?它是VC的財產嗎?在prepareForSegue中嘗試sender.gettingHere.content: –

+0

不,sender.gettingHere.content什麼也不做。發件人。沒有選擇。是的,我定義在這裏UIViewController。h文件:(需要注意標記爲STACKOVERFLOW不允許這些註釋 - 但它們都是現在所需的)#import「GettingHereContent.h」 interface SubNavViewController:UIViewController 屬性GettingHereContent * gettingHere;然後我在.m中合成它,如下所示: #import「GettingHereContent.h」 interface SubNavViewController() end implementation SubNavViewController @ynthesize gettingHere; – heyred

+0

你可以添加getHere創建的代碼片段嗎? –

回答

0

你永遠不會分配init你的getsHere屬性。從這裏回答:試試這個在您的VC

gettingHere = [[GettingHereContent alloc] init];

而且你的init方法不要忘了釋放它alloc + init with synthesized property - does it cause retain count to increase by two? @interface富:酒吧{ SomeClass的*喇嘛; } @property(nonatomic,retain)SomeClass * bla; @end

@implementation Foo @synthesize bla; - (id)init { ... bla = [[SomeClass alloc] init]; ... } - (void)dealloc {bla release]; ... [super dealloc]; }

+0

只是alloc啓動了我的gettingHere並釋放它,但仍然沒有。我再次嘗試並找出問題所在。 – heyred