gpt4 book ai didi

ios - 呈现 SecondViewController 时重复调用 ViewDidAppear

转载 作者:行者123 更新时间:2023-11-29 02:14:59 25 4
gpt4 key购买 nike

我正在做一个练习,在 ViewController 中呈现一个 SecondViewController。

这就是我调用 View Controller 的方式。

- (void)viewDidAppear:(BOOL)animated
{
NSLog(@"View Did Appear" );

SecondViewController* secViewCtrl = [ [ SecondViewController alloc ] init ];

[ self presentViewController:secViewCtrl animated:NO completion:nil ];

}

当我运行代码时。 SecondViewController 继续呈现。当我从 ViewDidLoad 调用它时,我无法访问。我该如何解决这个问题?

例如:

ViewController.m

- (void)viewDidLoad
{

[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

}

- (void)viewDidAppear:(BOOL)animated
{
NSLog(@"View Did Appear" );

SecondViewController* secViewCtrl = [ [ SecondViewController alloc ] init ];

[ self presentViewController:secViewCtrl animated:NO completion:nil ];

}

SecondViewController.m

- (void)viewDidLoad
{

[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

[ self.view setBackgroundColor:[ UIColor colorWithRed:64.0/255.0 green:106.0/255.0 blue:128.0/255.0 alpha:1.0 ] ];

UIButton* backBtn = [ [ UIButton alloc ] init ];

[ backBtn setFrame:CGRectMake(0, 0, 60, 44) ];

[ backBtn setImage:[ UIImage imageNamed:@"Circle.png" ] forState:UIControlStateNormal ];

[ backBtn setBackgroundColor:[ UIColor colorWithRed:0 green:0 blue:0 alpha:0.2 ] ];

[ self.view addSubview:backBtn ];

}

我可以看到 SecondViewController 的颜色代码。但是当我在 ViewController 中记录 ViewDidLoad 时。它不断重复。这使我不使用任何按钮操作。

最佳答案

看到发生的事情,我会说 SecondViewControllerViewController 的子类。

然后,当 SecondViewController 出现时,它将再次调用其父类(super class)的出现方法,这将导致无限循环。

如果是这种情况,只需将 SecondViewController 设置为 UIViewController 子类而不是 ViewController

关于ios - 呈现 SecondViewController 时重复调用 ViewDidAppear,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28855256/

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