开发者

Want to dismiss UIPickerView in UIView while touching outside

I'm trying to dismiss by UIPickerView by just touching outside the view, onto a UIView. I've seen other posts like this one that explains how to use the NotificationCenter to dismiss the UIKeyboard when pressing outside the keyboard. I'd like to know if there's a similar way of doing the same thing with a UIPickerView. Right now I'm using a done button inside of a UIActionSheet, but I like the idea of giving the user the option to just click outside the view. Also, I'm aware of the idea to use an "invisible button", which works about the same way, I was just looking to see if there's a开发者_如何学Go more elegant solution.

Thanks in advance.


I have done this in the past by creating a custom UIView and then overriding the pointInside method of the UIView class. That is the method that will fire every time a UIView gets a touch notification. When that happens you can then find out whether or not the touch was inside or outside of your views bounds.

For example your custom UIView could look like this:

CustomTouchUIView.h

#import 

@protocol CustomTouchUIViewDelegate

- (void) uiViewTouched:(BOOL)wasInside;

@end

@interface CustomTouchUIView : UIView 

// Properties
@property (nonatomic, assign) id delegate;

@end

CustomTouchUIView.m

#import "TOTouchUIView.h"

@implementation CustomTouchUIView

#pragma mark - Synthesize
@synthesize delegate;

#pragma mark - Touches
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
    if( point.x > 0 && point.x < self.frame.size.width && point.y > 0 && point.y < self.frame.size.height )
    {
        [delegate uiViewTouched:YES ];
        return YES;
    }

    [delegate uiViewTouched:NO ];
    return NO;
}
@end

I have a downloadable example / tutorial online here

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜