ios - 导航 Controller 工具栏在显示 UIActionSheet 后变为空白

标签 ios cocoa-touch uikit

在 iOS 6.1(模拟)和 6.1.3(物理设备)上运行的 iOS 应用程序中,我们使用以下代码从 View 中显示 UIActionSheet:

  UIActionSheet *actionCreateNewComment = [[UIActionSheet alloc] initWithTitle:CLocalised(@"EditExistingComment") delegate:self cancelButtonTitle:CLocalised(@"No") destructiveButtonTitle:nil otherButtonTitles: CLocalised(@"Yes"), nil];
[actionCreateNewComment setActionSheetStyle:UIActionSheetStyleBlackOpaque];
[actionCreateNewComment setTag:ActionSheetTagNewComment];
[actionCreateNewComment showFromToolbar:self.navigationController.toolbar];
[actionCreateNewComment release];

View 设置工具栏:

UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:control];
[self setToolbarItems:[NSArray arrayWithObject:item]];

其中 controlUISegmentedControl

处理按钮操作的代码是:

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    bool shouldAddNewComment = buttonIndex == [actionSheet cancelButtonIndex];
    [self navigateToCommentScreen:shouldAddNewComment];
}

每次显示此操作表时,工具栏都会变为“空白”。不仅适用于该 View ,还适用于其他 View 。

以下是工作流程的示例: Flowbetween Views

请注意,如果我在没有 UIActionSheet 的情况下从 Instr 导航到 Comment,但使用相同的代码,则不会出现问题。

这里的问题是在“Instr” View 中工具栏是空的。 如果我再弹出两个 View 以返回“列表”,其工具栏也将为空:

ListEmpytToolbar

即使我随后通过分配和初始化新表单然后推送它们导航回 WO 和 Instr,工具栏仍然是空的。

“列表” View 中还有另一个导航选项,显示“摘要” View 。根据数据,此 View 将在工具栏中显示有或没有按钮: Stats of summary view

“摘要按钮”和“摘要为空”都是预期 View 。在显示 UIActionSheet 之后,在 Inst 和 Comment 之间导航之前,会显示“隐藏摘要”。 但是,如果我导航到图片中显示为“Summary Empty”的“Summary” View (由于数据的缘故),那么工具栏将再次开始在任何地方工作。或者至少在我再次从 Instr View 中显示 UIActionSheet 之前。

在我看来,这有一些奇怪的事情:

  • 如果我导航到工具栏为空的 View ,该 View 会隐藏工具栏(请参见图片:“SummaryEmpty”),那么工具栏将按预期在其他 View 中显示。
  • 我们使用相同的代码在应用程序的其他 View 中显示操作表,没有任何问题
  • 如果我在模拟 iOS 5.1 或模拟 iOS 5.0 上运行应用程序,则不会出现问题

在关闭操作表或如何显示它时,我是否错过了某些内容?

如果您需要更多信息,请告诉我。

更新: 如果我在操作表按钮事件之后强制调用 [self setToolbarItems:nil] ,然后使用与之前相同的项目更新它,工具栏将按预期显示。 这感觉不是正确的做法。

2013年5月28日更新: 感谢@LOP_Luke,我发现如果我不从 -(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex 导航到“注释” View ,而只是返回工具栏将再次开始工作。 那么,当我导航到另一个 View 且 clickedButtonAtIndex 仍在调用堆栈上时,会出现某种问题吗?

更新2013-05-29: 如果我添加一个方法:

-(void)commentScreenYes{
  [self vanigateToCommentScreen:YES];
}

并将按钮代码更改为:

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    bool shouldAddNewComment = buttonIndex == [actionSheet cancelButtonIndex];
    if(shouldAddNewComment){
        [self performSelectorOnMainThread:@selector(commentScreenYes) withObject:nil waitUntilDone:NO];
        return;
    }
    [self navigateToCommentScreen:shouldAddNewComment];
}

然后它将适用于取消按钮,但不适用于是按钮。取消按钮还将使工具栏在被"is"按钮“破坏”后再次工作。如果我通过此流程管道两个按钮(通过添加新方法),它们都会起作用。 而且,如果我在 iOs 5.1 上运行它,无论我选择哪个流程,它都会工作。

最佳答案

– setToolbarItems method 是特定于导航 Controller 的每个 subview Controller 的 UIViewController 方法。假设[self navigateToCommentScreen:shouldAddNewComment]将新的 View Controller 推送到导航堆栈上,您也必须将新 View Controller 的工具栏项目设置为单独的调用,无论操作表正在做什么。例如,

-(void) navigateToCommentScreen:(bool) shouldAddComment{
    CommentViewController* viewController = [[CommentViewController alloc] init];

    [self.navigationController pushViewController:viewController animated:YES];

    [viewController setToolbarItems:self.toolbarItems];
}

否则新的 View Controller 将没有工具栏项并且工具栏将为空白。

编辑

我创建了一个带有 Root View Controller 的导航 Controller ,该 Controller 具有带有 UISegmented 控件的工具栏。我通过 UIActionSheet 委托(delegate)方法将一个新的 View Controller 推送到导航堆栈上,然后将其弹出。工具栏正是 Root View Controller 上应有的样子(分段控件按预期淡入)。您必须对 Controller 执行一些更复杂的操作来设置导致工具栏项消失的工具栏项。如果没有看到 viewController 的源方法,就很难判断问题是什么。确保您没有将工具栏项设置为零或以某种方式使用空数组重置它们。

关于ios - 导航 Controller 工具栏在显示 UIActionSheet 后变为空白,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16422991/

相关文章:

iphone - iOS 4 上的 UITableView 背景 View

cocoa /iPhone : BackgroundColor and Opaque Properties

ios - 使用 SwiftUI 添加谷歌登录

iOS map 样式配置向上滑动?

ios - 在ios模拟器中查看Console.log

ios - 在 IBAction 中将 UIVIew 与 UIButton 相关联

ios - 转换回登录 View Controller

ios - 在 Today 扩展中安排本地通知

iphone - 计算 Objective-C 中的网络时间协议(protocol)(偏移量)

iphone - NSZombieEnabled 不报告导致 EXC_BAD_ACCESS 错误的对象类型