2012-02-22 12 views
1

私の機能の1つでは、ボタンを押すと、サブビューを追加してから、そのサブビューのテキストビューにテキストを入力します。そのテキストを元の関数に戻して変数に保存したいと思います。問題は、私はすべてこのサブビューを使用する複数のボタンを持っているが、異なる変数を持っているので、リリースされた後はすべて同じ機能を実行することはできません。関数を終了する前にサブビューが解放されるまで待機するか、一時停止する方法はありますか?

main view controller  
-(IBAction)Button1Pressed:(id)sender{ 
    NSString *TempString; 

    TextEditViewController* TextViewController = [[TextEditViewController alloc] initWithNibName:@"TextEditViewController" bundle:[NSBundle mainBundle]]; 
    [self.view addSubview:TextViewController.view]; 

    /* wait for subview to be released */ 

    SpecificString = TempString; 
} 

-(IBAction)Button2Pressed:(id)sender{ 
    NSString *TempString; 

    TextEditViewController* TextViewController = [[TextEditViewController alloc] initWithNibName:@"TextEditViewController" bundle:[NSBundle mainBundle]]; 
    [self.view addSubview:TextViewController.view]; 

    /* wait for subview to be released */ 

    DifferentSpecificString = TempString; 
} 

New view controller 
-(IBAction)doneButtonPressed:(id)sender{ 
    TempString = textView.text; 
    [self.view removeFromSuperview]; 
    [self.view release]; 
} 

答えて

1

これは代表者の魔法のために最善であるものです。

- (void) setSomeString: (NSString *) withThisString 
{ 
    // buttonPressed can be an instance variable, 
    // or you can do this some other way 
    switch(buttonPressed) 
    { 
     case 1 : 
      // you should always name variables and methods 
      // with lower case letters, that's the 
      // Objective C standard 
      specificString = [[NSString alloc] initWithString: withThisString]; 
     case 2: 
      // doing an alloc & init here makes a retained copy 
      // of the string passed in via the delegate method 
      differentSpecificString = [[NSString alloc] initWithString: withThisString]; 
    } 
} 

- (IBAction) button1Pressed: (id) sender 
{ 
    buttonPressed = 1; 

    TextEditViewController* textViewController = [[TextEditViewController alloc] initWithNibName:@"TextEditViewController" bundle:[NSBundle mainBundle]]; 
    textViewController.delegate = self; 
    [self.view addSubview:textViewController.view]; 
} 

- (IBAction) button2Pressed: (id) sender 
{ 
    buttonPressed = 2; 

    TextEditViewController* textViewController = [[TextEditViewController alloc] initWithNibName:@"TextEditViewController" bundle:[NSBundle mainBundle]]; 
    textViewController.delegate = self; 
    [self.view addSubview:textViewController.view];  
} 

また、メインビューコントローラの.h(インターフェイス)ファイルでデリゲートプロトコルを宣言する必要があります。 Here's a tutorial that explains this concept a bit more for you

私はこれがあなたを助けてくれることを願っています!

+0

少なくとも50個のボタンがありますが、それは私が推測することができますので、多くの場合です。ありがとう!チュートリアルをありがとう、私は最初に代理人についてちょっと混乱しました。 – Kevin

関連する問題