2013-08-02 181 views
0

我有一個UITabBarController與四個相關的意見。在其中一個視圖中,有一個文本視圖只爲navigationbar留下空間:我想要做的是在navigationbar被觸摸時隱藏鍵盤。我怎樣才能做到這一點?我應該重寫UINavigationBartouchesBegan:方法嗎?觸摸導航欄時隱藏鍵盤

編輯


我發現這樣做是增加了UITapGestureRecognizer

UITapGestureRecognizer* tapRecon = [[UITapGestureRecognizer alloc] 
            initWithTarget:self action:@selector(navigationBarTap:)]; 
tapRecon.numberOfTapsRequired = 1; 
[self.navigationController.navigationBar addGestureRecognizer:tapRecon]; 

它調用方法

- (void)navigationBarTap:(UIGestureRecognizer*)recognizer { 
    [self.view endEditing:YES]; 
} 
+0

你發現你的觸摸? – iPatel

回答

2
UITapGestureRecognizer* tapRecon = [[UITapGestureRecognizer alloc] 
            initWithTarget:self action:@selector(navigationBarTap:)]; 
tapRecon.numberOfTapsRequired = 1; 
[self.navigationController.navigationBar addGestureRecognizer:tapRecon]; 


- (void)navigationBarTap:(UIGestureRecognizer*)recognizer { 
    [self.view endEditing:YES]; 

    // OR 

    for (id view in self.view.subviews) { 
     if ([view isKindOfClass:[UITextView class]] || [view isKindOfClass:[UITextField class]]) { 
      [view resignFirstResponder]; 
     } 
    } 
} 

也許這將幫助ü

+0

正是我在找什麼。謝謝,完美的作品。 –

+0

我的榮幸,我可以幫助別人。 – Warewolf

0

在你可以在你的touchesBegan方法的最佳途徑resignFirstResponder t o使鍵盤消失。希望這會有所幫助,歡呼吉姆。

0

未經測試。 將觸控手勢添加到UINavigationbar,並執行姿勢以響應應答者的動作。

UITapGestureRecognizer *gestureRecognizer=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hideKeyboard)]; 
    gestureRecognizer.numberOfTapsRequired=1; 
    gestureRecognizer.cancelsTouchesInView = NO; 
    [UInavigationbar addGestureRecognizer:gestureRecognizer]; 
    [gestureRecognizer release]; 

操作:

[Textfield resignfirstresponder] 
0

//此方法導航到其他視圖之前會打電話......

-(void)viewWillDisappear:(BOOL)animated 
{ 
    [super viewWillDisappear:YES]; 
    [yourTextField resignFirstResponder]; 
} 

//這將導航到其他屏幕前辭職鍵盤..

我希望這會幫助你。