gpt4 book ai didi

ios - 关闭 View 后执行的功能不能完全工作 IOS

转载 作者:行者123 更新时间:2023-11-28 19:57:49 25 4
gpt4 key购买 nike

我正在开发一个需要用户输入代码的 iphone 应用程序。我有一个第一个 viewController,当用户使用 -->

触摸按钮时,它将调用我的 passwordPrompt View Controller
   [self performSegueWithIdentifier:@"passcodePrompt" sender:self];

密码提示显示正常,我可以输入我的密码,然后当我单击确定时,密码提示 View 被关闭-->

   [self dismissViewControllerAnimated: YES completion:nil]; 

现在当我关闭这个 View Controller 时,我从密码提示 Controller 调用我的 firstViewController 中的一个函数(我尝试使用 dismissViewControllerAnimated 的完成选项并尝试从 viewDidUnload 调用我的函数)-->

    FirstViewController *firstViewControl = [[FirstViewController alloc] init];

[firstViewControl returnPass:[NSString stringWithFormat:@"%@%@%@%@",digit1.text,digit2.text,digit3.text,digit4.text]];

这个函数在我的 FirstViewController 中执行得很好,但即使它执行了那些代码行 -->

[passButton setTitle:@"Password is Correct" forState:UIControlStateNormal];

[(AppDelegate *)[[UIApplication sharedApplication] delegate] deactivateMoveDetection];

infoText.text = @"application unlocked until fully closed by user";

infoText.textColor = [UIColor blackColor];

passButton.tag = 0;

(调试显示它确实执行了这些行而没有任何错误,奇怪的是,在执行函数之后,调试器将我带回到在密码提示 Controller 中启动关闭的函数)

标题未更改,passButton.tag 未设置为 0,信息文本未更改,他的颜色也未更改。

我已经搜索了 2 天了,但没有成功。谢谢你的帮助

------ passcodePrompt.m --------

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];
appDelegate = [[UIApplication sharedApplication]delegate];
dig1 = false;
dig2 = false;
dig3 = false;
dig4 = false;
ok.enabled = false;
// Do any additional setup after loading the view.
}

-------- 解决方案------

我添加到我的 passcodePrompt.h -->

@property (nonatomic, retain) FirstViewController *delegate;

为了能够使用 segueWithIdentifier 将 firstViewController 设置为我的密码提示的代表,我必须将其添加到我的 FirstViewcontroller.m -->

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
PasscodePrompt *passprompt = [segue destinationViewController];
passprompt.delegate = self;
}

从 passwordPrompt.m,我在 FirstViewController 中执行了我的函数 -->

[self.delegate returnPass:[NSString stringWithFormat:@"%@%@%@%@",digit1.text,digit2.text,digit3.text,digit4.text]];

最佳答案

您的问题可能出在这段代码中:

FirstViewController *firstViewControl = [[FirstViewController alloc] init];

[firstViewControl returnPass:[NSString stringWithFormat:@"%@%@%@%@",digit1.text,digit2.text,digit3.text,digit4.text]];

您没有引用同一个 FistViewController,您正在创建一个新的。这解释了为什么该方法被调用并且你的断点被击中,也解释了为什么你的原始 View Controller 没有更新。在这种情况下,您有两个独特的 View Controller ,第一个是正确的,而您要更新的第二个是一个全新的。

关于如何实现委托(delegate),请参阅我对这个问题的回答,这样您的 FirstViewController 就可以成为呈现的 View Controller 的委托(delegate),并且在您要关闭呈现的 View Controller 之前,它可以告诉它是委托(delegate)(原始的 FirstViewController)返回密码。

Calling method from viewcontroller to class xcode

关于ios - 关闭 View 后执行的功能不能完全工作 IOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25532737/

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