ios - 在 UIActionSheetDelegate 方法中无法呈现 subview

标签 ios objective-c delegates uiactionsheet

我有以下代码(据推测)在操作表按钮触发时呈现 subview :

- (void) actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex
{    
    if (buttonIndex ==0) {
        [self.view addSubview:self.customView];
        //
        // heavy lifting of method here
        //
        // self.customView removes itself from superview before actionSheet:dismissWithButtonIndex: finishes
    }
}

在完成所有“繁重的工作”之后, subview 将自身从 View 层级中移除(操作表委托(delegate)方法完成之前)。

唉!!我发现 subview 永远不会显示在屏幕上。 事实上,如果我阻止添加的 subview 自行关闭并设置断点,我发现它确实显示了,但只有在 UIActionSheet 委托(delegate)方法完成之后。 p>

最初,我认为这是因为 subview 在 actionSheet:clickedButtonAtIndex: UIActionSheet 委托(delegate)方法中呈现 - 导致操作表阻止 View 的呈现。

查看其他可用方法,actionSheet:didDismissWithButtonIndex: 似乎可以解决我的问题,因为此方法在动画结束且 View 隐藏后调用(根据苹果文档)。仍然没有运气!

在方法完成之前我如何呈现这个 subview 的任何想法。

最佳答案

您似乎是在用相同的方法 (actionSheet:didDismissButtonIndex:) 添加和删除 subview 。这意味着该操作是在主运行循环的同一周期内完成的。请注意, View 渲染仅在操作结束时完成,这意味着“addSubview”被“removeFromSuperview”覆盖,从而导致 View 根本不被渲染。

您可以做的是在操作表关闭之前但在单击按钮之后调用的委托(delegate)方法中的“addSubview”,然后在操作表关闭之后调用的委托(delegate)方法中调用“removeFromSuperview”解雇:


-(void)actionSheet:(UIActionSheet *)actionSheet willDismissWithButtonIndex:(NSInteger)buttonIndex {
    [self.view addSubview:self.subviewToAdd];
    self.subviewToAdd.center=CGPointMake(160,100);
}

-(void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex {
    [self.subviewToAdd removeFromSuperview];
}

在此示例中,您将看到您的 View 只是闪烁,因为添加/删除操作在操作表关闭动画时间中受到限制。您可以通过更改 actionSheet:didDismissWithButtonIndex: 将其设置为在较长时间后删除:以这种方式添加一个小延迟:

-(void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex {
    [self.subviewToAdd performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:1.0];
    //[self.subviewToAdd removeFromSuperview];
}

关于ios - 在 UIActionSheetDelegate 方法中无法呈现 subview ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17207854/

相关文章:

ios - 向 UIActivityViewController 添加一个明显的(自定义)按钮

ios - xcodebuild 无法使用 XCTestExpectation 运行异步测试?

ios - 我如何将数据从 View Controller 传递到容器 View ?

c# - 我破坏了封装吗?

ios - 如何在 Swift 中为每一帧的流媒体和录制视频添加实时时间戳?

iphone - CGContextRestoreGState 好像没有恢复之前的状态

ios - 圆形,3 色渐变,动画进度 View

javascript - Parse.Query 和组合问题

android - 有没有办法在本地编译 Air Mobile 应用程序?

ios - 委托(delegate)有问题,我的委托(delegate)总是零,tvOS,swift 2.0