【问题标题】:UIActionSheet showing up twiceUIActionSheet 出现两次
【发布时间】:2015-07-24 21:50:13
【问题描述】:

我正在函数 quizEnded 中创建 UIActionSheet。 其中一个按钮将用户发送到 Game Center。当他们从游戏中心返回时,在 viewdidappear 中再次调用 quizended。这是在旧的操作表之上创建第二个操作表。

https://www.youtube.com/watch?v=QrY1Awt3RX0

我不认为再次调用 quizEnded 会导致问题,而是我没有正确关闭第一个 UIActionSheet。

-(void)viewDidAppear {
// some code ...
if(self.quizDidEnd > 0){
        [BT_debugger showIt:self theMessage:@"@@@Calling quiz ended###"];

        [self quizEnded];
    }
}// end viewDidAppear

// initialize button array
-(void)quizEnded {
// some code ...
    _finishedButtons =  [[NSMutableArray alloc] init];

     // show either the reward screen or the finished screen or quit to menu

    if(earnedReward == TRUE && haveRewardScreen==TRUE) {
         [_finishedButtons addObject:NSLocalizedString(@"quizShowReward", @"Show Reward")];
        //[self showQuizRewardScreen];

    }else {
        if(haveFinishScreen==TRUE){

            [_finishedButtons addObject:NSLocalizedString(@"continue", @"Continue")];
            //[self showFinishScreen];

        }else {
            // if no finish screen then send back to rootviewcontroller
            [_finishedButtons addObject:NSLocalizedString(@"quit", @"Quit")];             }
    }

    if(_enableGameCenter) {
        [_finishedButtons addObject:NSLocalizedString(@"Game Center", @"Game Center")];
    }

    // initialize UIAction sheet
    UIActionSheet *actionSheet = [[UIActionSheet alloc]
                                  initWithTitle:quizResultMessage
                                  delegate:self
                                  cancelButtonTitle:nil
                                  destructiveButtonTitle:nil
                                  otherButtonTitles: nil];


    //add the buttons to the action sheet
    for (int i = 0; i < [_finishedButtons count]; i++) {
        [actionSheet addButtonWithTitle:[_finishedButtons objectAtIndex:i]];
    }

    actionSheet.destructiveButtonIndex = [_finishedButtons count];

    [actionSheet setActionSheetStyle:UIActionSheetStyleBlackOpaque];



    //is this a tabbed app?
    if([appDelegate.rootApp.tabs count] > 0){
        [actionSheet showFromTabBar:[appDelegate.rootApp.rootTabBarController tabBar]];
    }else{
        [actionSheet showInView:self.view];
    }

    [actionSheet release];

}//self.quizDidEnd == 0


}// end quizEnded 

-(void)actionSheet:(UIActionSheet *)actionSheet  clickedButtonAtIndex:(NSInteger)buttonIndex {


[BT_debugger showIt:self theMessage:@"!!!action sheet called!!!"];

NSString *buttonTitle = [actionSheet buttonTitleAtIndex:buttonIndex];

//quit only used if no finished or reward screen present
if([buttonTitle isEqual:NSLocalizedString(@"quit", @"Quit")]){

    // if no finish screen then send back to rootviewcontroller
    [self.navigationController popToRootViewControllerAnimated:YES];
    [BT_debugger showIt:self theMessage:@"Quit tapped"];
}


//show reward
if([buttonTitle isEqual:NSLocalizedString(@"quizShowReward", @"Show Reward")]){
    [self showQuizRewardScreen];
    [BT_debugger showIt:self theMessage:@"Show Reward Tapped"];
}

//show continue
if([buttonTitle isEqual:NSLocalizedString(@"continue", @"Continue")]){
    [self showFinishScreen];
    [BT_debugger showIt:self theMessage:@"Continue Tapped"];
}

//show game center
if([buttonTitle isEqual:NSLocalizedString(@"Game Center", @"Game Center")]){  
    [BT_debugger showIt:self theMessage:@"game center tapped"];
    [self showGameCenter];
    _altend = 0;

}

}// end 

【问题讨论】:

  • 与问题无关,但我认为actionSheet.destructiveButtonIndex = [_finishedButtons count]; 应该是actionSheet.destructiveButtonIndex = [_finishedButtons count] - 1
  • 你在哪里将quizDidEnd设置为0?
  • quizDidEnd 在 vi​​ewDidLoad 中设置为 0。然后在测验结束时翻转为 1(5,10,20 个问题)。
  • 7usam 好呼叫计数 -1。不过,dismissWithClickedButtonIndex 仍然没有得到任何东西。
  • 一次,通过在 clickedButtonAtIndex 处保持断点:方法检查它调用了多少次并打印 buttonTitle 是什么。

标签: iphone ios uiactionsheet ipad


【解决方案1】:

如果是打开菜单的手势,请确保将所有代码放入其中:

    if(gestureRecognizer.state == UIGestureRecognizerStateBegan) { }

否则,可能会出现手势发生变化的第二部分 (UIGestureRecognizerStateChanged),这会导致操作表重复触发。

【讨论】:

    【解决方案2】:

    您并没有从我所看到的内容中忽略操作表... 您必须在操作表上调用 - (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated 才能将其关闭。

    【讨论】:

      【解决方案3】:
      if(gesture.state == UIGestureRecognizerStateBegan){
          UIActionSheet *sheet;
      
          sheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"" destructiveButtonTitle:nil otherButtonTitles:@"", nil];
          [sheet showInView:self.view];
      }
      

      【讨论】:

        【解决方案4】:

        添加这个:

        if(recognizer.state == UIGestureRecognizerStateBegan){
                UIActionSheet *as = [[UIActionSheet alloc] initWithTitle:@""
            }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-07-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-11-24
          • 2020-09-09
          相关资源
          最近更新 更多