【问题标题】:UINavigationController pushes several of the same viewControllerUINavigationController 推送几个相同的viewController
【发布时间】:2016-04-29 10:45:53
【问题描述】:

我的应用中有一个 UIButton,按下它会显示下一个视图控制器。有时,由于后台进程,UI 会锁定并且应用程序会冻结片刻。发生这种情况时,用户可能会多次点击按钮,因为在第一次点击时没有立即发生任何事情,并且当这种情况发生时,UINavigationController 会再次将 ViewController 推到自身之上,因此您必须返回几次才能获得回到家。这是我的代码:

- (void)viewDidLoad {
    [super viewDidLoad];
    self.pushVCButton.multipleTouchEnabled = NO;
}

- (IBAction)pushVCButtonPressed:(id)sender {
    self.pushVCButton.enabled = NO;
    ViewController *viewController = [[ViewController alloc] init];
    [self.navigationController pushViewController:viewController animated:YES];
    self.pushVCButton.enabled = YES;
}

如何让它永远不会推送多个 viewController 实例?

【问题讨论】:

    标签: ios objective-c iphone uinavigationcontroller


    【解决方案1】:

    您确实应该尝试使后台进程不在 UI 线程上运行,但如果您不能尝试仅在视图消失或侦听推送动画完成时将启用按钮设置为是:

    - (IBAction)pushVCButtonPressed:(id)sender {
        self.pushVCButton.enabled = NO;
        ViewController *viewController = [[ViewController alloc] init];
        [self.navigationController pushViewController:viewController animated:YES];
        // Hack: wait for this view to disappear to enable the button
        //self.pushVCButton.enabled = YES;
    }
    
    - (void)viewDidDisappear:(BOOL)animated {
        [super viewDidDisappear:animated]
        self.pushVCButton.enabled = YES;
    }
    

    还要确保该操作没有被调用两次。

    【讨论】:

      猜你喜欢
      • 2023-03-14
      • 1970-01-01
      • 2011-01-25
      • 2013-12-18
      • 1970-01-01
      • 2012-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多