ios - UINavigationController 上的 ViewDidAppear 在向后导航时未被调用

标签 ios xamarin.ios monotouch.dialog

我有一个 UITabBarController,它承载 5 个 UINavigationControllers(我们称它们为 N1 - N5)。每个 UINavigationControllers 都有导致 UITableViewController 被推送到导航堆栈的 UI 元素(我使用 MonoTouch.Dialog DialogViewController 来实现这些 UITableViewControllers)。我们称它们为 T1 - T5。

当我在选项卡之间导航时,ViewDidAppear 方法按预期在 N1 - N5 中的每一个上被调用。但是当我触摸一个 UI 元素时,比如 N1,这会导致 T1 被推送到导航堆栈,然后尝试使用后退按钮返回,N1 的 ViewDidAppear 方法不会被调用.

有趣的是,如果我“切换”到另一个选项卡(比如 N2),然后“切换回”到 N1,ViewDidAppear 将被正常调用。即使我将 T1 插入导航堆栈,如果我进行相同的 Tab 操作,N1 的 ViewDidAppear 仍将被调用。

N1 的 MonoTouch 代码如下所示:

public class CalendarPage : UINavigationController
{
    private DialogViewController dvc;

    public override void ViewDidAppear (bool animated)
    {           
        // initialize controls
        var now = DateTime.Today;
        var root = new RootElement("Calendar")
        {
            from it in App.ViewModel.Items
                where it.Due != null && it.Due >= now
                orderby it.Due ascending
                group it by it.Due into g
                select new Section (((DateTime) g.Key).ToString("d")) 
                {
                    from hs in g
                        select (Element) new StringElement (((DateTime) hs.Due).ToString("d"),
                            delegate 
                            {
                                ItemPage itemPage = new ItemPage(this, hs);
                                itemPage.PushViewController();
                            })
                        { 
                            Value = hs.Name
                        }                                                    
                }
        };

        if (dvc == null)
        {
            // create and push the dialog view onto the nav stack
            dvc = new DialogViewController(UITableViewStyle.Plain, root);
            dvc.NavigationItem.HidesBackButton = true;  
            dvc.Title = NSBundle.MainBundle.LocalizedString ("Calendar", "Calendar");
            this.PushViewController(dvc, false);
        }
        else
        {
            // refresh the dialog view controller with the new root
            var oldroot = dvc.Root;
            dvc.Root = root;
            oldroot.Dispose();
            dvc.ReloadData();
        }
        base.ViewDidAppear (animated);
    }
}

最佳答案

我明白了这是怎么回事。当在内部 DialogViewController(在 ItemPage 中创建)上按下后退按钮时,外部 DialogViewController(上面的“T1”)现在是第一响应者,而不是 UINavigationController(“N1”)。我的困惑源于我关闭了那个外部 DialogViewController 上的后退按钮,所以我假设我一直弹出到 UINavigationController (N1),而我还在 DialogViewController (T1) 中。

我通过在内部 DialogViewController(本例中为 ItemPage)上创建一个 ViewDissapearing 事件并检查我是否实现了所需的行为(刷新“T1”的内容)弹出 - 如果是,则调用父 Controller 的 ViewDidAppear 方法。

        actionsViewController.ViewDissapearing += (sender, e) => 
        {
            if (actionsViewController.IsMovingFromParentViewController)
                controller.ViewDidAppear(false);
        };

请注意这段代码的有趣之处在于实际起作用的属性是 IsMovingFromParentViewController,而不是 IsMovingToParentViewController正在导航回来)。我想这可能是 MT.Dialog 中的一个错误,但由于后向紧凑的原因无法修复。

我希望这最终能帮助到某人...

关于ios - UINavigationController 上的 ViewDidAppear 在向后导航时未被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10158960/

相关文章:

ios - 似乎无法隐藏我的 UIImageView

ios - SwiftUI 使用菜单时禁用递归触发器日志记录

c# - MonoTouch 和 "Applications are expected to have a root view controller at the end of application launch"错误

ios - 如何删除 UITableViewCell NSIBPrototypingLayoutConstraint 并将我自己的添加到 subview ?

ios - 我的 iOS 应用程序需要很长时间才能回到前面

ios - 在设备上调试时应用程序崩溃

c# - 如何将 LUT(颜色查找表)应用于 IOS 中的 CGImage 或 UIImage?

c# - iOS:流式传输大文件

c# - MonoTouch.Dialog 支持(字母)索引吗?

c# - 如何使用 Monotouch 对话框显示 UIPickerView?