2014-07-07 44 views
2

我正在使用UIPicker填充UITextField而不是鍵盤。這很好,但我現在無法關閉UIPicker。我希望能夠點擊屏幕上的任何地方並關閉UIPicker。我已經嘗試過每種觸摸方法都不會觸發。touchesBegan not called

setUserInteractionEnabled:YES。 不知道它是否有所作爲,但我使用故事板

我應該在我的AppDelegate中設置一些東西來偵聽觸摸嗎?

這裏是我的.h

#import <UIKit/UIKit.h> 

@interface RNMemberTableViewController : UITableViewController<UIPickerViewDataSource, UIPickerViewDelegate, UITextFieldDelegate> 

@property (weak, nonatomic) IBOutlet UIPickerView *behaviorPicker; 
@property (nonatomic, weak) IBOutlet UIDatePicker *dateOfRecordPicker; 
@property (nonatomic, strong) NSArray *behaviorLevels; 
@property (weak, nonatomic) IBOutlet UITextField *behaviorTextField; 

@end 

這裏是一些實施的...

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    [self.view setUserInteractionEnabled:YES]; 
    [self buildBehaviorPicker]; 
    NSLog(@"Member View"); 
} 

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    NSLog(@"test"); 
    [self.view touchesBegan:touches withEvent:event]; 
    UITouch *touch = [touches anyObject]; 
    int tCount = touch.tapCount; 
    NSLog(@"Touched %d", tCount); 
} 

- (void) buildBehaviorPicker 
{ 
    behaviorLevels = [[NSArray alloc] initWithObjects:@"Unsatisfactory", @"Needs Improvement", @"Satisfactory", @"Outstanding", nil]; 

    UIPickerView *pickerView = [[UIPickerView alloc] init]; 
    pickerView.dataSource = self; 
    pickerView.delegate = self; 
    [pickerView selectRow:2 inComponent:0 animated:NO]; 
    self.behaviorTextField.inputView = pickerView; 


} 

在此先感謝 -Bob

+2

鮑勃,如果你喜歡工作答案,相應地標記爲「回答」。 – BonanzaDriver

回答

6

你總是可以嘗試以下方法:

-(void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapReceived:)]; 
    [tapGestureRecognizer setDelegate:self]; 
    [self.view addGestureRecognizer:tapGestureRecognizer]; 
} 

-(void)tapReceived:(UITapGestureRecognizer *)tapGestureRecognizer 
{ 
    // do something, like dismiss your view controller, picker, etc., etc. 
} 

希望這有助於...

+2

這很好,但任何想法爲什麼touchesBegan不會開火?這是最佳做法嗎?我對Obj-C非常陌生 – Bob

+2

我仍然想知道爲什麼touches方法不會觸發 – Bob

+0

如果您在常規(非表視圖控制器)上設置此選項,您將看到使用touchesBegan( ) - 桌子很漂亮。但是,正確的做法仍然是使用手勢識別器。 – BonanzaDriver

0

您可以添加的透明按鈕與選取器下的控制器視圖大小相同。

設置button.hidden = NO當拾取器顯示並設置爲button.hidden = YES當拾取器被隱藏。

有辦法隱藏選取器。最簡單的方法是設置picker.hidden = YES

-5

如果您正在使用的UIScrollView然後

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 

不會被調用,爲的touchesBegan是UIView的方法而不是UIScrollView的方法。

+14

實際上,touchesBegan是UIResponder的一種方法。 UIView從UIResponder繼承,UIScrollView從UIView繼承,所以這裏推理不準確。 – davew