OGeek|极客世界-中国程序员成长平台

标题: ios - 从 UIView 子类调用 popViewControllerAnimated [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 14:35
标题: ios - 从 UIView 子类调用 popViewControllerAnimated

我在 UIView 子类中创建了一个按钮。我需要通过此按钮调用 popViewControllerAnimated,但没有任何效果!而且我看不到 viewController 推回 rootViewController 。这是我的代码:

- (void)SomeFunction {

 backButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [backButton showsTouchWhenHighlighted];

        [backButton addTarget:self
                       actionselector(backToMainMenu)
             forControlEvents:UIControlEventTouchUpInside];

}

- (void)backToMainMenu {

    [self.window.rootViewController.navigationController popViewControllerAnimated:YES];

    NSLog(@"back");
}

我把代码改成这样:

UINavigationController *vc = self.window.rootViewController.navigationController;
    [vc.navigationController popViewControllerAnimated:YES];

但是什么也没发生。



Best Answer-推荐答案


我认为您需要使用将按钮作为参数的正确目标格式。所以像这样添加目标函数:

[backButton addTarget:self
               actionselector(backToMainMenu
     forControlEvents:UIControlEventTouchUpInside];

目标应该是这样的:

- (void) backToMainMenuUIButton *) sender{
  [self.navigationController popViewControllerAnimated:YES];
}

关于ios - 从 UIView 子类调用 popViewControllerAnimated,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29076259/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4