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

发布于 2025-01-08 03:27:40 字数 1029 浏览 3 评论 0原文

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

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]; 
}

In one of my functions, from a button press, I am adding a subview, then typing text to a textview in that subview. I want to then take that text back to the original function and save it to a variable. The problem is that I have multiple buttons that all use this subview but with different variables, so it can't just run the same function for all of them after it's released.

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]; 
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

假面具 2025-01-15 03:27:40

这就是代表的魔力最能发挥的作用。

- (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(接口)文件中声明您的委托协议。 这里有一个教程为您详细解释了这个概念< /a>.

我希望这对你有帮助!

This is what the magic of delegates is best for.

- (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];    
}

You also need to declare your delegate protocol in your main view controller's .h (interface) file. Here's a tutorial that explains this concept a bit more for you.

I hope this helps you out!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文