2014-04-11 43 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>這是我的前視圖。