2014-04-11 25 views
2

我在我的应用程序中使用SWRevealViewController,并遇到问题。我在场景中有一个文本框,如果我在键盘打开时向左滑动,出现菜单,但不会关闭键盘。如何在左侧滑动时关闭键盘? 我试图SWRevealViewController在刷卡时关闭键盘

UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard)]; 
swipe.direction = UISwipeGestureRecognizerDirectionLeft; 
[self.view addGestureRecognizer:swipeRecognizer]; 

-(void)dismissKeyboard 
{ 
    [self.textField resignFirstResponder]; 
} 

,但它不工作,我想是因为我已经在使用一个panGestureRecognizer为revealViewcontroller即[self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];

我也使用UITapGestureRecognizer但它仅适用于自来水不刷卡。 我希望这个问题有一个非常简单的解决方案,任何人都可以帮忙吗?

谢谢,FrontView中的

+1

看到我的答案希望可以帮助你.. –

+1

看到我更新了我的答案检查它 –

回答

3

我认为ü需要使用应用程序委托的委托方法之一有可能会委托的方法有哪些但你需要做的财产以后像下面

不添加任何手势

使用该委托中在appDelegate 删除所有宏开始#if不`吨需要一个

放在应用程序委托一个突破点以下的委托方法这个方法 每次SWRevealViewController移动开孔的或切片..

- (void)revealController:(SWRevealViewController *)revealController didMoveToPosition:(FrontViewPosition)position 
{ 
    // NSLog(@"%@: %@", NSStringFromSelector(_cmd), [self stringFromFrontViewPosition:position]); 
    if(position == FrontViewPositionRight) //check the where to move 
    { 
     UINavigationController *viewController = revealController.frontViewController; 

    if([viewController.visibleViewController isKindOfClass:[FrontViewController class]]) 
     { 
     [(FrontViewController *)viewController.visibleViewController dismissKeyboard]; //where this is the method declared in the FrontViewController.h file 
     } 

    } 
} 

有一个警告,它仍然工作放破发点,并在FrontViewController.m

-(void)dismissKeyboard 
{ 
     if([self.textField isFirstResponder]) //check 
     [self.textField resignFirstResponder]; 
} 
+0

感谢您的答案,但它不适用于我....它说'FrontViewController没有可见的@interface声明选择器dismissKeyboard' ....你知道哪些是侧边菜单出现时调用的第一个方法? –

+0

当然,我在.h文件中添加了 - (void)dismissKeyboard :)但我没有添加委托...现在错误消失了,但方法永远不会被调用:(...如果它为你工作,那么你必须做别的事情..你可以分享你的项目(只是工程的例子)?谢谢 –

+0

oky你是否删除了#if条件...?并且还将代理设置为Appdelegate,如果你正在使用演示项目,那么它添加到应用程序委托确保你添加了这一行'revealController.delegate = self; //在'didFinishLaunchingWithOptions''中添加这行代码' –

0

检查当前位置和写,如果条件隐藏键盘的代码。

+0

你能举个例子,我应该使用哪种方法?谢谢 –

0

您确定self.textField是当前的第一响应者吗?如果你试图辞去UIGestureRecognizer中的第一个响应者,那么代表的方法– gestureRecognizerShouldBegin:

显然在该函数中返回YES。

1

检查 希望这有助于ü...

FrontViewController.h

-(void)dismissKeyboard; //add this 

试试这个

[[[self revealViewController] view] endEditing:YES] 

- (void)revealController:(SWRevealViewController *)revealController didMoveToPosition:(FrontViewPosition)position 

或根据您的需要任何其他委托方法添加此。

-1

试试这个:

请添加以下代码SWRevealViewContoller.m的方法 - (BOOL)_panGestureShouldBegin

[自我。查看endEditing:YES];所有的

1

首先,你需要更换revealToggle:您自定义的方法方法是这样的:

UIBarButtonItem *barBtn = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"reveal-icon.png"] style:UIBarButtonItemStyleBordered target:self action:@selector(myMethod)]; 

self.navigationItem.leftBarButtonItem = barBtn; 

,比你的方法:

-(void)myMethod{ 
[self.view endEditing:YES]; 
SWRevealViewController *reveal = self.revealViewController; 
[reveal revealToggleAnimated:YES]; 
} 

它肯定会工作。

+0

这对我来说是一个很好的解决方案,因为我的应用程序结构也是如此在AppDelegete中实现委托时会发生同样的情况。非常感谢 :) – ayon

3

我有同样的问题。

我将self.revealController.delegate = self;添加到我用作前视图的View Controller中,并且调用委托方法。

我用 - (void)revealController:(SWRevealViewController *)revealController willMoveToPosition:(FrontViewPosition)position{} 委托方法,我在此委托方法中写道[textField endEditing:YES];

还向我的ViewController.h添加了<SWRevealViewControllerDelegate>这是我的前视图。