You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

iOS开发求助:Alert触发后异常返回父控制器,内购Alert加载失败

Hey there, let's dig into this weird issue you're facing—auto-popping back to the original ViewController when presenting the first alert, plus the in-app purchase alert showing up in the wrong place is definitely frustrating. Let's break down possible causes and actionable fixes:

1. Check for Accidental Pop/Dismiss Logic in createAlert()

First, comb through every line in your createAlert() function, especially the alert’s button actions or completion handlers. It’s super easy to accidentally slip in a navigationController?.popViewController(animated: true) or dismiss(animated: true) somewhere—maybe a leftover line from testing, or a mislinked IBAction. Even a typo could trigger this unexpected navigation.

Add breakpoints at all the alert’s action closures and the completion block of present(_:animated:completion:) to see exactly which line is triggering the pop.

2. Ensure Alerts Are Presented From the Correct ViewController

The root of the "alert showing in original VC" problem is likely that you’re presenting the alert from the wrong VC context. Make sure you’re using the active tab’s ViewController (the one that should display the alerts) to call present(_:animated:completion:), not a reference to the original VC or a global root VC.

For example, in your tab’s ViewController, present alerts like this:

func createFirstAlert() {
    let alert = UIAlertController(title: "First Alert", message: "Your message here", preferredStyle: .alert)
    
    let continueAction = UIAlertAction(title: "Continue", style: .default) { [weak self] _ in
        // Safely call the purchase alert from the same tab VC
        self?.showPurchaseAlert()
    }
    
    alert.addAction(continueAction)
    // Use `self` (the current tab VC) to present
    self.present(alert, animated: true, completion: nil)
}

func showPurchaseAlert() {
    let purchaseAlert = UIAlertController(title: "In-App Purchase", message: "Would you like to buy this?", preferredStyle: .alert)
    let buyAction = UIAlertAction(title: "Buy", style: .default) { _ in
        // Execute your in-app purchase logic here
    }
    let cancelAction = UIAlertAction(title: "Cancel", style: .cancel)
    purchaseAlert.addAction(buyAction)
    purchaseAlert.addAction(cancelAction)
    self.present(purchaseAlert, animated: true, completion: nil)
}

Using [weak self] here also prevents retain cycles and ensures you’re referencing the correct VC instance when the closure runs.

3. Verify the TabViewController’s Lifecycle & Navigation Stack

Sometimes, unexpected navigation can happen if the TabViewController or its child VCs have lifecycle methods (like viewDidAppear(_:), viewWillDisappear(_:)) that accidentally modify the navigation stack. Check if any of these methods have code that pops or dismisses the VC.

You can also log the navigation stack before presenting the first alert to confirm the original VC is still at the bottom:

print("Current navigation stack: \(navigationController?.viewControllers ?? [])")

If the stack changes unexpectedly when the alert is presented, that’s a clue something’s modifying it behind the scenes.

4. Fix the In-App Purchase Alert Context

Since the purchase alert is showing up in the original VC, it’s likely that when you trigger it, the reference to your tab VC has been lost or replaced. Make sure in the first alert’s action closure, you’re still holding a valid reference to the tab VC (hence the [weak self] to avoid deallocation).

Avoid using global references like UIApplication.shared.keyWindow?.rootViewController to present alerts—this can lead to presenting on the wrong VC if the window hierarchy changes unexpectedly.

5. Debug with Breakpoints & Logs

Set breakpoints in:

  • The start of createAlert()
  • The alert’s action closures
  • Any navigation-related code (like popViewController, dismiss)

As you step through, check:

  • What’s the value of self.navigationController? Is it the correct navigation stack?
  • Which VC is actually presenting the alert? (Check alert.presentingViewController after calling present)

This will help you pinpoint exactly where the navigation is being triggered and where the alert presentation context goes wrong.


内容的提问来源于stack exchange,提问作者LHGAMES

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 06:50:21