13 Feb 2012

iOS通过UIActionSheetDelegate来实现警告的方法

有时候需要用户确认一些操作,比如退出,删除数据等等,用UIActionSheetDelegate实现比较友好,实现也很简单,controller实现UIActionSheetDelegate协议就可以了,示例如下:

01 #pragma mark-
02 #pragma mark UIActionSheetDelegate with logOff
03
04 - (void) onLogOffButtonClick:(id)sender{
05     UIActionSheet actionSheet = [[UIActionSheet alloc] initWithTitle:@“确认要退出登录吗?” delegate:self cancelButtonTitle:@“不退出” destructiveButtonTitle:@“退出” otherButtonTitles:nil, nil];
06    
07     [actionSheet showFromTabBar:self.navigationController.tabBarController.tabBar];
08     XX_RELEASE_SAFELY(actionSheet);
09 }
10 - (void)logOff{
11
12     //do logoff
13    
14     [self.navigationController popViewControllerAnimated:YES];
15 }
16
17 - (void)actionSheet:(UIActionSheet
)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex; {
18     switch (buttonIndex) {
19         case 0:
20             [self logOff];
21             break;
22         case 1:
23             [self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:YES];
24             break;
25         default:
26             break;
27     }
28 }

09 Dec 2011

How to,在iOS中点击文本框以外区域,隐藏键盘

当点击文本框以外部分时,隐藏键盘,实现很简单,加个Category就可以了,代码如下:

#import <UIKit/UIKit.h>
@interface UITextField (HideKeyBoard)
-(void)hideKeyBoard:(UIView *)view;
@end

#import "UITextField+HideKeyBoard.h"

@implementation UITextField (HideKeyBoard)
- (void) hideKeyBoard:(UIView*)view{
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
                                   initWithTarget:self
                                   action:@selector(doHideKeyBoard)];
   
    tap.numberOfTapsRequired = 1;
    [view  addGestureRecognizer: tap];
    [tap setCancelsTouchesInView:NO];
    [tap release];
}

- (void)doHideKeyBoard{
    [self resignFirstResponder];
}
@end

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self.txtInput hideKeyBoard:self.view];
}

see also: https://gist.github.com/1450404