有没有办法在结束函数之前等待或暂停直到子视图被释放?

时间:2012-02-22 19:21:59

标签: ios viewcontroller

在我的一个功能中,按下按钮,我将添加一个子视图,然后在该子视图中的textview中键入文本。我想将该文本带回原始函数并将其保存到变量中。问题是我有多个按钮都使用这个子视图但是有不同的变量,所以它不能只是在它发布后为所有这些按钮运行相同的功能。

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 个答案:

答案 0 :(得分: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

我希望这会帮助你!