问题是,一旦文档准备好,按钮似乎会重新生成。我无法真正定义“准备就绪”,但我编写了一个测试应用程序并注意到以下几点:
- 设置 NavigationItem 的右栏按钮在 iOS6 中不起作用
- 扫描视图层次结构并搜索 UIToolbar 的实例并设置工具栏的按钮有效。
只有当显示的文档足够快地“准备好”时,上述技巧才有效。大文件需要更长的时间。我想出了一个两步解决方案:
这是一些代码:
这是来自 ViewDidAppear():
设置一个持续隐藏按钮的计时器。
NSTimer oTimer = NSTimer.CreateRepeatingTimer(0.2, this.HidePrintButton);
NSRunLoop.Current.AddTimer(oTimer, NSRunLoopMode.Default);
private void HidePrintButton()
{
if(this.oNavItem == null)
{
return;
}
this.InvokeOnMainThread(
delegate {
this.oNavItem.SetRightBarButtonItems( new UIBarButtonItem[0], false );
} );
}
这将搜索导航项:
/// <summary>
/// Finds the first navigation item inside a view hierachy.
/// </summary>
/// <param name='oCurrentView'>the view to start searching from</param>
/// <param name='oItem'>will be set if the navigation item was found</param>
public static void FindNavigationItem(UIView oCurrentView, ref UINavigationItem oItem)
{
if(oItem != null || oCurrentView == null || oCurrentView.Subviews == null || oCurrentView.Subviews.Length <= 0)
{
return;
}
// Check if a UINavigationBar was found. This will contain the UINavigationItem.
if(oCurrentView is UINavigationBar)
{
UINavigationBar oBar = (UINavigationBar)oCurrentView;
if(oBar.Items != null && oBar.Items.Length > 0)
{
oItem = oBar.Items[0];
}
return;
}
// Recursively loop all sub views and keep on searching.
foreach (var oSubView in oCurrentView.Subviews)
{
FindNavigationItem(oSubView, ref oItem);
if(oItem != null)
{
break;
}
}
}