2013-02-25 33 views
0

在我的第一類(Secen1ViewController.h)我有屬性:無法在不同的class ios中設置屬性?

@property (nonatomic) BOOL myBoolean; 

在我的第二類(Secen2ViewController.m)我已經進口Secen1ViewController.h並已聲明的屬性:

@property (strong) Secen1ViewController *parent; 

我也initalized財產viewDidLoad中:

_parent = [[Secen1ViewController alloc] init]; 

,然後當按下按鈕,我設置屬性:

self.parent.myBoolean=YES; 

沒有顯示錯誤,但來自Scene1的myBoolean未設置爲yes。

+0

此投訴的常見原因是您引用了對象的兩個不同副本。每個'alloc'操作都會創建一個DIFFERENT對象,並且您不能在其中設置一個值並期望在另一箇中讀取它。 (對不起,如果這不是你的問題,但它非常常見。) – 2013-02-25 19:08:49

回答

0

首先添加@class Secen1ViewController (.h)#import Secen1ViewController in (.m)

設置你的myBoolean屬性

@property (assign,nonatomic) BOOL myBoolean; 

@synthesize it in (.m),並用它在你Secen1ViewController

self.parent = [[Secen1ViewController alloc] init]; 
self.parent.myBoolean = YES; 
+0

CalculatorViewController是一個錯字,但沒有在我的代碼中,我現在編輯了這個問題。我試着添加@class Secen1ViewController,我已經有導入,但沒有效果。我不知道它是否相關,但是這些類已經與Segue連接。 – 2013-02-25 17:56:43

+0

_parent.myBoolean = YES;?你如何知道是或否?與NSLog?請更詳細。 – 2013-02-25 17:58:43

+0

我試過了,但沒有改變。 NSLog說布爾值被設置爲NO。 NSLog(_myBoolean?@「Yes」:@「No」); – 2013-02-25 18:09:20

0

你合成你的布爾變量?顯然有可能你沒有在你的實現文件中聲明

@synthesize myBoolean = _myBoolean; 

+0

我也試過,雖然在ios6中沒有必要綜合,除非我使用自定義getter和setter。 – 2013-02-25 18:58:24

0

嘗試更換驗證碼:

_parent = [[Secen1ViewController alloc] init];  

與此代碼:

self.parent = [[Secen1ViewController alloc] init];  

並確保您綜合所有屬性。

+0

試過了,不起作用 – 2013-02-25 19:47:05

+0

Secen1ViewController是從UIViewController繼承的嗎? – ppalancica 2013-02-25 20:09:45

+0

是的,它繼承。 – 2013-02-25 20:27:29