gpt4 book ai didi

objective-c - 在外部水龙头上关闭模态视图表单 Controller

转载 作者:太空狗 更新时间:2023-10-30 03:08:52 26 4
gpt4 key购买 nike

我将模态视图 Controller 呈现为表单,并在单击取消按钮(它是一个条形按钮项)时将其关闭。当我在该 View 之外点击时,我需要关闭它。请帮我引用一下。注意:我的模态视图 Controller 带有一个导航 Controller 。

@cli_hlt,@Bill Brasky 感谢您的回答。当点击发生在作为表单的模态视图之外时,我需要关闭它。我在下面粘贴我的代码。

-(void)gridView:(AQGridView *)gridView didSelectItemAtIndex:(NSUInteger)index  
{
if(adminMode)
{
CHEditEmployeeViewController *editVC = [[CHEditEmployeeViewController alloc] initWithNibName:@"CHEditEmployeeViewController" bundle:nil];
editVC.delegate = self;
editVC.pickedEmployee = employee;
editVC.edit = TRUE;
editVC.delegate = self;
UINavigationController *navigationController = [[UINavigationController alloc]initWithRootViewController:editVC];
navigationController.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentModalViewController:navigationController animated:YES];

return;
} //the above code is from the view controller which presents the modal view. Please look at the below code too which is from my modal view controller. Please guide me in a proper way. -(void)tapGestureRecognizer {

UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapBehind:)];
[recognizer setNumberOfTapsRequired:1];
recognizer.cancelsTouchesInView = NO; //So the user can still interact with controls in the modal view
[self.view addGestureRecognizer:recognizer];

}

- (void)handleTapBehind:(UITapGestureRecognizer *)sender
{
if (sender.state == UIGestureRecognizerStateEnded)
{
CGPoint location = [sender locationInView:nil]; //Passing nil gives us coordinates in the window

//Then we convert the tap's location into the local view's coordinate system, and test to see if it's in or outside. If outside, dismiss the view.

if (![self.view pointInside:[self.view convertPoint:location fromView:self.view.window] withEvent:nil])
{
[self dismissModalViewControllerAnimated:YES];
[self.view.window removeGestureRecognizer:sender];
}

}
}

最佳答案

我知道这是一个老问题,但这是可能的,尽管“正确”的答案是这样说的。由于这是我寻找它时的第一个结果,所以我决定详细说明:

这是你的做法:

您需要向 View Controller 添加一个属性,从您想要模态呈现的位置开始,在我的例子中是“tapBehindGesture”。

然后在viewDidAppear

if(!tapBehindGesture) {
tapBehindGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapBehindDetected:)];
[tapBehindGesture setNumberOfTapsRequired:1];
[tapBehindGesture setCancelsTouchesInView:NO]; //So the user can still interact with controls in the modal view
}

[self.view.window addGestureRecognizer:tapBehindGesture];

这是 tapBehindDetected 的实现

- (void)tapBehindDetected:(UITapGestureRecognizer *)sender
{

if (sender.state == UIGestureRecognizerStateEnded)
{
//(edited) not working for ios8 above
//CGPoint location = [sender locationInView:nil]; //Passing nil gives us coordinates in the window

CGPoint location = [sender locationInView: self.presentingViewController.view];

//Convert tap location into the local view's coordinate system. If outside, dismiss the view.
if (![self.presentedViewController.view pointInside:[self.presentedViewController.view convertPoint:location fromView:self.view.window] withEvent:nil])
{
if(self.presentedViewController) {
[self dismissViewControllerAnimated:YES completion:nil];
}
}
}
}

请记住在 viewWillDisappear 上从 view.window 中删除 tapBehindGesture 以避免在未分配的对象中触发 handleTapBehind。

关于objective-c - 在外部水龙头上关闭模态视图表单 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9102497/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com