popViewControllerAnimated 不起作用,但后退按钮起作用

发布于 2024-08-31 06:47:27 字数 2250 浏览 3 评论 0原文

我正在创建一个基于实用程序模板的应用程序。主屏幕由带有多个按钮的菜单组成,每个按钮都会创建不同的翻转视图。在其中一个翻转视图中,我还配置了一个导航控制器,只要我激活了导航栏,它就可以完美工作...我可以推动视图,但我必须使用“后退”按钮返回到我的翻转视图-侧视图,这将是导航控制器的根。如果我尝试使用正确配置按钮的“popViewControllerAnimated”而不是导航栏的“后退”按钮返回,就会出现问题。我的应用程序由于某种原因崩溃了,我无法理解为什么。

我可以只使用导航栏中的“后退”按钮,然后忘记这个问题,但我更喜欢有自己的按钮以便返回。

我的应用程序由以下部分组成:

我的 APPDelegate.m:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

    MenuViewController *menuController = [[MenuViewController alloc] initWithNibName:@"MenuView" bundle:nil];
    self.menuViewController = menuController;
    [menuController release];

    menuViewController.view.frame = [UIScreen mainScreen].applicationFrame;
    [window addSubview:[menuViewController view]];
    [window makeKeyAndVisible];

    return YES;
}

MenuViewController.m 开始我的翻转视图:

- (IBAction)showFuelUpliftView {    

    FuelUpliftViewController *controller = [[FuelUpliftViewController alloc] 
                                            initWithNibName:@"FuelUpliftView" bundle:nil];
    controller.delegate = self;
    controller.title = @"Fuel Uplift";

    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:controller];
    [navController setNavigationBarHidden: NO];
    navController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [self presentModalViewController:navController animated:YES];

    [navController release];
    [controller release];
}

FuelUpliftViewController.m,我在其中使用按钮推动 NavigationController 的第二个视图:

- (IBAction)showFuelUplift2View:(id)sender {
    UIViewController *controller = [[UIViewController alloc] initWithNibName:@"FuelUplift2View" bundle:nil];
    controller.title = @"Settings";
    [self.navigationController pushViewController:controller animated:YES];
    [controller release];
}

最后,我的 FuelUplift2ViewController.m,其中应用程序尝试返回时崩溃:

- (IBAction)backFromFuelUplift2View {
    [self.navigationController popViewControllerAnimated:YES];
}

我不知道这一切是否有意义,我目前正在开始我的第一个应用程序,并且由于传统的试错方法仍在学习。尽管如此,我看不出这个问题的原因,并且希望能得到任何帮助。

非常感谢,

马努

I am creating an application based on the Utility template. The main screen consists of a menu with several buttons, each of which creates a different flip-side view. In one of those flip-side views I also configured a Navigation Controller, which works perfectly as long as I have the NavigationBar activated... I can push the view but I have to use the "back" button to return to my flip-side view, which would be the root of the Navigation Controller. The problem comes if I try to go back using "popViewControllerAnimated", properly configured with a button, instead of the "back" button of the NavigationBar. My application crashes for some reason and I am not able to understand why.

I could just use the "back" button in the NavigationBar and forget about the problem, but I would prefer to have my own button in order to go back.

My app consists of the following:

My APPDelegate.m:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

    MenuViewController *menuController = [[MenuViewController alloc] initWithNibName:@"MenuView" bundle:nil];
    self.menuViewController = menuController;
    [menuController release];

    menuViewController.view.frame = [UIScreen mainScreen].applicationFrame;
    [window addSubview:[menuViewController view]];
    [window makeKeyAndVisible];

    return YES;
}

MenuViewController.m starting my flip-side view:

- (IBAction)showFuelUpliftView {    

    FuelUpliftViewController *controller = [[FuelUpliftViewController alloc] 
                                            initWithNibName:@"FuelUpliftView" bundle:nil];
    controller.delegate = self;
    controller.title = @"Fuel Uplift";

    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:controller];
    [navController setNavigationBarHidden: NO];
    navController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [self presentModalViewController:navController animated:YES];

    [navController release];
    [controller release];
}

FuelUpliftViewController.m, where I push the second view of the NavigationController with a button:

- (IBAction)showFuelUplift2View:(id)sender {
    UIViewController *controller = [[UIViewController alloc] initWithNibName:@"FuelUplift2View" bundle:nil];
    controller.title = @"Settings";
    [self.navigationController pushViewController:controller animated:YES];
    [controller release];
}

And finally, my FuelUplift2ViewController.m, where the app crashes when trying to go back:

- (IBAction)backFromFuelUplift2View {
    [self.navigationController popViewControllerAnimated:YES];
}

I do not know if all this makes sense, I am currently beginning with my first application and am still learning thanks to the traditional trial an error method. Nevertheless, I cannot see the reason for this problem and would appreciate any help I can get.

Thanks very much,

Manu

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

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

发布评论

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

评论(2

疯狂的代价 2024-09-07 06:47:28

我终于能够解决我的问题了。

为了在我的 UINavigationController 中创建第二个视图,我这样做了:

- (IBAction)showFuelUplift2View:(id)sender {
    UIViewController *controller = [[UIViewController alloc] initWithNibName:@"FuelUplift2View" bundle:nil];
    controller.title = @"Settings";
    [self.navigationController pushViewController:controller animated:YES];
    [controller release];
}

我从另一个论坛获取了该代码,它确实立即起作用了,所以我无法想象我会遇到问题。通过上面的代码,如果我理解正确的话,我正在为第二个视图创建一个新的 UIViewController,这就是为什么我能够切换到第二个视图并有一个可用的“后退”按钮。

无论如何,为了配置我自己的按钮返回,我应该编写以下内容:

FuelUplift2ViewController *controller = [[FuelUplift2ViewController alloc] initWithNibName:@"FuelUplift2View" bundle:nil];

显然我正在正确初始化一个随机 UIViewController,但由于我没有正确指示它是哪一个(FuelUplift2ViewController),我返回到的方法第一个视图无法正常工作。

这可能是一个非常基本的问题,但我花了几个小时才意识到这个问题,我很高兴我可以自己找到它,只要遵循我的代码并使用一点常识即可。

I was finally able to solve my problem.

In order to create the second view in my UINavigationController, I had this:

- (IBAction)showFuelUplift2View:(id)sender {
    UIViewController *controller = [[UIViewController alloc] initWithNibName:@"FuelUplift2View" bundle:nil];
    controller.title = @"Settings";
    [self.navigationController pushViewController:controller animated:YES];
    [controller release];
}

I took that code from another forum and it did work straight away, so I could not imagine that I was going to have problems with it. With the above code, if I understand it correctly, I'm creating a new UIViewController for my second view, and that's why I was able to switch to that second view and have a working "Back" button.

Anyhow, in order to configure my own button to go back, I should have written the following:

FuelUplift2ViewController *controller = [[FuelUplift2ViewController alloc] initWithNibName:@"FuelUplift2View" bundle:nil];

Apparently I was initializing correctly a random UIViewController, but as I was not indicating properly which one it was (FuelUplift2ViewController), my method to return to the first view could not work properly.

This was maybe a very basic question, but it took me several hours to realize the problem and I am glad I could find it by myself, juts following my code and using a little bit of common sense.

若沐 2024-09-07 06:47:28

当询问崩溃问题时,最好显示您收到的消息。

尽管“self.delegate”行让我感到怀疑,但您显示的代码中没有明显的错误。您应该检查该属性是如何声明的,以及是否双重释放它。

When asking about a crash, it is a very good idea to show the message you get.

There is nothing obviously wrong in the code you show, although the "self.delegate" line makes me suspicious. You should check how the property is declared, and if you are double releasing it.

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