gpt4 book ai didi

ios - 有没有办法在结束功能之前等待或暂停直到 subview 被释放?

转载 作者:行者123 更新时间:2023-11-29 13:40:08 24 4
gpt4 key购买 nike

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

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

最佳答案

这就是委托(delegate)的魔力所在。

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

您还需要在主视图 Controller 的 .h(接口(interface))文件中声明您的委托(delegate)协议(protocol)。 Here's a tutorial that explains this concept a bit more for you .

希望对您有所帮助!

关于ios - 有没有办法在结束功能之前等待或暂停直到 subview 被释放?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9401516/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com