gpt4 book ai didi

ios - popToRootViewControllerAnimated 什么都不做,无法让应用程序返回主屏幕

转载 作者:行者123 更新时间:2023-11-28 20:07:25 26 4
gpt4 key购买 nike

我无法让 [self.navigationController popToRootViewControllerAnimated:YES]; 工作(无法让我的任何应用程序返回主屏幕)。我尝试创建一个新的空白测试项目,但它仍然无法正常工作,我就是这样做的:

一个。使用向导创建单个 vue 应用

B.通过 Xcode(添加新文件)添加了一个名为 cHome

UIViewController

C.在主 Storyboard上添加了一个按钮

D.通过拖放为此添加了一个 Action ,称之为测试

E.在生成的方法上,我添加了以下代码:

- (IBAction)atest:(id)sender
{
if (mHome == nil)
{
mHome = [[cHome alloc] initWithNibName:@"cHome" bundle:[NSBundle mainBundle]];
}

[self presentModalViewController: mHome animated:NO];
}

G.我向 attest.xib

添加了一个按钮

H.将 - (IBAction)atest:(id)sender; 添加到 header 并使用 UI 将其链接到按钮

我。将以下代码添加到 attest.m

- (IBAction)atest:(id)sender
{
[self.navigationController popToRootViewControllerAnimated:YES];
}

J.在代码上打断点

K。当我运行应用程序时,在第一个屏幕上按按钮,然后在下一个屏幕上按按钮,断点关闭,但应用程序不会返回到前屏幕代码

ViewController.h

#import <UIKit/UIKit.h>
#import "cHome.h"

@interface ViewController : UIViewController
{
cHome *mHome;
}
- (IBAction)atest:(id)sender;

@end

ViewController.m

#import "ViewController.h"

@implementation ViewController

- (IBAction)atest:(id)sender {
if ( mHome==nil)
{
mHome = [[ cHome alloc]
initWithNibName:@"cHome"
bundle:[NSBundle mainBundle]];
}

[self presentModalViewController: mHome animated:NO];

}

cHome.h

#import <UIKit/UIKit.h>

@interface cHome : UIViewController
- (IBAction)atest:(id)sender;
@end

cHome.m

#import "cHome.h"

@implementation cHome

- (IBAction)atest:(id)sender
{
[self.navigationController popToRootViewControllerAnimated:YES];
}

@end

最佳答案

因为 View Controller 是模态显示的,所以您需要关闭它,而不是弹出到根目录,如果您按下了 Controller ,这将是合适的:

所以替换这段代码:

- (IBAction)atest:(id)sender
{
[self.navigationController popToRootViewControllerAnimated:YES];
}

用这个:

- (IBAction)atest:(id)sender
{
[self dismissViewControllerAnimated:YES completion:nil];
}

或者,如果您想弹出 View Controller ,则必须先推送它。因此,请确保 ViewController 是 Storyboard中 UINaviagionController 的 Root View Controller ,然后按如下方式更新代码:

// In `ViewController.m`
- (IBAction)atest:(id)sender
{
if (mHome == nil)
{
mHome = [[cHome alloc] initWithNibName:@"cHome" bundle:[NSBundle mainBundle]];
}

[self.navigationController pushViewController:mHome animated:YES];
}

// In cHome.m
- (IBAction)atest:(id)sender
{
[self.navigationController popToRootViewControllerAnimated:YES];
}

关于ios - popToRootViewControllerAnimated 什么都不做,无法让应用程序返回主屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21631404/

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