2011-11-08 27 views
1

私は目的のCプログラミングを初めて学び、私は本当にすばやくiPhoneアプリケーションを作成する必要がある位置にいます。 Xcodeを使用しています。4.2対物レンズの視点から視点への切り替え

NSString変数をあるビューから別のビューに転送する際に問題があります。 2つのビューが.hiにファーストクラスでの.hと.mファイルのクラスの2つの異なるセットに

ある私は

を持っているfirstviewの.mファイルでこの

@interface firstview : UIViewController { 
NSString *test; 
} 

-(IBAction)testbutton 
@end 

のようなものを持っています

-(IBAction)testbutton{ 
secondView *second; 
[second setText:text]; //set text is a function that will take an NSString parameter 
second= [[secondView alloc] initWithNibName:nil bundle:nil]; 
[self presentModalViewController:second animated:YES]; 
} 

はsecondViewの.hの中で私はあなたが権利を持っている

@interface secondView : UIViewController{ 
-IB 
} 
+0

あなたの質問はありますか? –

答えて

1

を書きましたあなたはsecondが有効なオブジェクトを指す前に-setText:に電話しようとしています。代わりに、次の操作を行います。

-(IBAction)testbutton{ 
    secondView *second; 
    second = [[secondView alloc] initWithNibName:nil bundle:nil]; 
    [second setText:text]; //set text is a function that will take an NSString parameter 
    [self presentModalViewController:second animated:YES]; 
} 

また、あなたはsecondViewクラスに与えるインタフェースが正しくないと不完全の両方に見える - 私はあなたが-IB部分をどうしようとしているのかわからないんだけど。また、通常のObjective-C命名規則に従い、secondViewの代わりにSecondViewの大文字でクラス名を開始すると、将来的に役立ちます。最後に、 "... View"で終わるView Controllerに名前を付けることをお勧めします。そのためView ControllerとUIViewを混同しやすくなります。あなたがそれをしないならば、あなたのtextのためのアクセサを合成する場合、コンパイラはIVARを作成します - インスタンス変数としてtextを宣言

@interface SecondViewController : UIViewController{ 
    NSString *text; 
} 
@property (retain, nonatomic) NSString *text; 
@end 

がオプションである:すべて一緒に、それは次のようになりますプロパティ。

+0

+1は16秒で殴られます。 – MusiGenesis

0
はこれにあなたのコードを変更

:あなたの元のバージョンで

-(IBAction)testbutton{ 
    secondView *second; 
    second = [[secondView alloc] initWithNibName:nil bundle:nil]; 
    [second setText:text]; //set text is a function that will take an NSString parameter 
    [self presentModalViewController:second animated:YES]; 
} 

を、あなたはそれにsetText:を呼び出すした後、あなたの2番目のビュー(すなわち、インスタンス)の初期化されました。あなたはそれを初期化する必要があります次にテキストを設定します。

0

割り当てして初期化した後、テキストを設定する必要があります。

-(IBAction) testButton { 
    secondView *second = [[[secondView alloc] initWithNibName:nil bundle:nil] autorelease]; 
    [second setText:text]; 
    [self presentModalViewController:second animated:YES]; 
} 
関連する問題