2011-09-13 80 views
4

我是iPhone开发的新手。任何人都可以告诉我如何解除UIActionSheet控制,当我在它外面轻拍?UIActionSheet关闭时点击/水龙头

在我的actionSheet中,我只有datePicker控件,它现在弹出了标签栏控件,现在当用户点击它时,它应该关闭而不是使用actionSheet的取消按钮。 问候

+1

UIActionSheet:UIView。我希望U不能得到Touch控制器OutSide – Srinivas

+0

现在看来这是iOS7的默认行为。 – JohnK

+0

即使在iOS6中,它也不是iOS 7 iPhone上的默认行为,只有在iPad – Alex

回答

0

我有你创建一个透明的按钮,使您的视图相同的大小,并将它送回的建议。创建一个方法来解除操作表并将其与大按钮挂钩。我从来没有尝试过,但我认为这是值得一试的,因为如果这样做的话,你可以将它设置为视图中的一种通用方法,当用户敲击文本框之外(根据HIG)和UIActionSheet时,关闭键盘。

但是,我不知道是否适合像popover一样关闭UIActionSheet。不管行动表是否需要一些输入?否则,为什么苹果会为您提供一个取消按钮?

6

试试这个神奇:

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapOut:)]; 
    tap.cancelsTouchesInView = NO; 
    [actionSheet.window addGestureRecognizer:tap]; 
    [tap release]; 

而且这种方法:

-(void)tapOut:(UIGestureRecognizer *)gestureRecognizer { 
    CGPoint p = [gestureRecognizer locationInView:self.actionSheet]; 
    if (p.y < 0) { 
     [self actionPickerDone:nil]; 
    } 
} 
0

添加工具栏与按钮sismiss日期选择器和actionSheet

toolbarPicker = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)]; 
     toolbarPicker.barStyle=UIBarStyleBlackOpaque; 
     [toolbarPicker sizeToFit]; 
     NSMutableArray *itemsBar = [[NSMutableArray alloc] init]; 
     //calls DoneClicked 
     UIBarButtonItem *bbitem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(DoneClicked)]; 
     [itemsBar addObject:bbitem]; 
     [toolbarPicker setItems:itemsBar animated:YES]; 

     [menu addSubview:toolbarPicker]; 

,并添加这些动作的完成按钮

-(BOOL)closeDatePicker:(id)sender{ 
[menu dismissWithClickedButtonIndex:0 animated:YES]; 
[txtDate resignFirstResponder]; 

return YES; 

}

当完成按钮被点击

//动作 - (IBAction为)DoneClicked { [自closeDatePicker:自]; menu.frame = CGRectMake(0,44,320,416);

}

-2

按照IOS 6,你可以实现它:

  1. 检查buttonIndex(只运行你的东西,如果buttonIndex> = 0)。点击外部动作表将使buttonindex = -1。
  2. 实施didDismissWithButtonIndex

请注意,这只是你实现UIActionSheetDelegate后的作品。

- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex { 
    NSLog(@"action sheet was dismissed"); 
} 

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
    //if user click outside the action sheet, button index will be -1 
    if(buttonIndex>0) 
    { 
     NSString *buttonTitle = [actionSheet buttonTitleAtIndex:buttonIndex]; 
    } 
} 
+0

上,如果在操作表单外部单击,则不会使用按钮索引-1调用该代理。该文档提到如果取消按钮索引未设置,则将传递-1的按钮索引。 – ram

9

一个更简单的解决方案就是添加一个取消按钮。这将自动启用点击背景到关闭:(斯威夫特)

alertController.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: nil)) 
+0

尼斯提醒提示:D – Vahid

相关问题