0

I have the following code which (supposedly) presents a subview when triggered by an action sheet button:

- (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
    }
}

After doing all the 'heavy lifting' the subview removes itself from the view heirachy (before the completion of the action sheet delegate method).

Alas!! I'm finding that the subview never gets shown on screen. Indeed, if I stop the added subview from dismissing itself and set breakpoints, I find that it is DOES get displayed, but only AFTER the UIActionSheet delegate method is completed.

Initially, I thought this was because the subview was being presented in the actionSheet:clickedButtonAtIndex: UIActionSheet delegate method - causing the action sheet to block the presentation of the view.

Looking at other methods available, actionSheet:didDismissWithButtonIndex: seemed like it might solve my problems as This method is invoked after the animation ends and the view is hidden (per Apple Docs). Still no luck!

Any thoughts of how I might present this subview before the method is completed.

4

1 回答 1

3

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

您可以做的是在解除操作表之前但在单击按钮之后调用的委托方法中的“addSubview”,然后在解除操作表后调用的委托方法中调用“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];
}

在此示例中,您将看到您的视图只是闪烁,因为添加/删除操作在操作表解除动画时间中受到限制。您可以通过更改 actionSheet:didDismissWithButtonIndex: 将其设置为在较长时间后删除,这样会增加一点延迟:

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

于 2013-06-20T07:55:11.397 回答