@protocol UITextFieldDelegate <NSObject>
@optional
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField; // return NO to disallow editing. - (void)textFieldDidBeginEditing:(UITextField *)textField; // became first responder - (BOOL)textFieldShouldEndEditing:(UITextField *)textField; // return YES to allow editing to stop and to resign first responder status. NO to disallow the editing session to end - (void)textFieldDidEndEditing:(UITextField *)textField; // may be called if forced even if shouldEndEditing returns NO (e.g. view removed from window) or endEditing:YES called
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string; // return NO to not change text
- (BOOL)textFieldShouldClear:(UITextField *)textField; // called when clear button pressed. return NO to ignore (no notifications) - (BOOL)textFieldShouldReturn:(UITextField *)textField; // called when 'return' key pressed. return NO to ignore.
@end
UITextField.h头文件描述
@property(nonatomic,retain) UIView *leftView; // e.g. magnifying glass
@property(nonatomic) UITextFieldViewMode leftViewMode; // sets when the left view shows up. default is UITextFieldViewModeNever
@property(nonatomic,retain) UIView *rightView; // e.g. bookmarks button
@property(nonatomic) UITextFieldViewMode rightViewMode; // sets when the right view shows up. default is UITextFieldViewModeNever
设置方法
// 给文本输入框添加左边的视图
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 0)];
self.inputTextField.leftView = view;
// 设置左边视图的显示模式
self.inputTextField.leftViewMode = UITextFieldViewModeAlways;
// 显示模式
typedef NS_ENUM(NSInteger, UITextFieldViewMode) {
UITextFieldViewModeNever,
UITextFieldViewModeWhileEditing,
UITextFieldViewModeUnlessEditing,
UITextFieldViewModeAlways
};
定制呼出的输入法只需要将一个UIView添加到textView的inputView上即可
此外还可以添加输入法的辅助视图(比如,ToolBar),使用inputAccessoryView属性即可
UIDatePicker *datePicker = [[UIDatePicker alloc] init];
datePicker.datePickerMode = UIDatePickerModeDate;
datePicker.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];
self.inputTextField.inputView = datePicker; // 将一个date picker作为输入框呼出的输入法
UIToolbar *toolbar = [[UIToolbar alloc] init];
toolbar.barTintColor = [UIColor purpleColor];
toolbar.frame = CGRectMake(0, 0, 320, 44);
self.inputTextField.inputAccessoryView= toolbar; // 设置文本输入框键盘的辅助视图
+ (UIImage *)resizableImageWithName:(NSString *)imageName
{
// 加载原有图片
UIImage *norImage = [UIImage imageNamed:imageName];
// 获取原有图片的宽高的一半
CGFloat w = norImage.size.width * 0.5;
CGFloat h = norImage.size.height * 0.5;
// 生成可以拉伸指定位置的图片
UIImage *newImage = [norImage resizableImageWithCapInsets:UIEdgeInsetsMake(h, w, h, w) resizingMode:UIImageResizingModeStretch]
return newImage;
}
// 重新变化大小的模式
/* UIImage will implement the resizing mode the fastest way possible while retaining the desired visual appearance. Note that if an image's resizable area is one point then UIImageResizingModeTile is visually indistinguishable from UIImageResizingModeStretch. */
typedef NS_ENUM(NSInteger, UIImageResizingMode) {
UIImageResizingModeTile,
UIImageResizingModeStretch,
};
// NO 超出范围的图片不要剪切
// YES 超出范围的图片要剪切(默认)
btn.imageView.clipsToBounds = NO;
NSDate *date = [NSDate date]; // 创建时间对象
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"HH:mm";
NSString *time = [formatter stringFromDate:date];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
NSDate *date = [formatter dateFromString:@"2015-11-15 18:08:08"];
// 数据源(用来告诉UIPickerView有多少列多少行)
@property(nonatomic,assign) id<UIPickerViewDataSource> dataSource;
// 代理(用来告诉UIPickerView每1列的每1行显示什么内容,监听UIPickerView的选择)
@property(nonatomic,assign) id<UIPickerViewDelegate> delegate;
// 是否要显示选中的指示器
@property(nonatomic) BOOL showsSelectionIndicator;
// 一共有多少列
@property(nonatomic,readonly) NSInteger numberOfComponents;
// 重新刷新所有列
- (void)reloadAllComponents;
-
// 重新刷新第component列
- (void)reloadComponent:(NSInteger)component;
// 主动选中第component列的第row行
- (void)selectRow:(NSInteger)row inComponent:(NSInteger)component animated:(BOOL)animated;
// 获得第component列的当前选中的行号
- (NSInteger)selectedRowInComponent:(NSInteger)component;
// 一共有多少列
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView; // 第component列一共有多少行 - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;
// 第component列的宽度是多少
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component; - // 第component列的行高是多少 - (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component; // 第component列第row行显示什么文字 - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component; // 第component列第row行显示怎样的view(内容) - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view; // 选中了pickerView的第component列第row行 - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component;
// datePicker的显示模式
@property (nonatomic) UIDatePickerMode datePickerMode;
// UIDatePickerMode的所有取值
typedef enum {
UIDatePickerModeTime,
UIDatePickerModeDate,
UIDatePickerModeDateAndTime,
UIDatePickerModeCountDownTimer
} UIDatePickerMode;
// 显示的区域语言
@property (nonatomic, retain) NSLocale *locale;
UIToolbar的高度是固定的44
toolbar的items属性是一个可以包含若干个UIBarButtonItem的数组,显示时按下表顺序依次显示
UIBarButtonItem init的时候可以选择的系统样式:
typedef enum { // 每种变量都与上面图标的顺序一一对应且顺序相同
UIBarButtonSystemItemDone,
UIBarButtonSystemItemCancel,
UIBarButtonSystemItemEdit,
UIBarButtonSystemItemSave,
UIBarButtonSystemItemAdd,
UIBarButtonSystemItemCompose,
UIBarButtonSystemItemReply,
UIBarButtonSystemItemAction,
UIBarButtonSystemItemOrganize,
UIBarButtonSystemItemBookmarks,
UIBarButtonSystemItemSearch,
UIBarButtonSystemItemRefresh,
UIBarButtonSystemItemStop,
UIBarButtonSystemItemCamera,
UIBarButtonSystemItemTrash,
UIBarButtonSystemItemPlay,
UIBarButtonSystemItemPause,
UIBarButtonSystemItemRewind,
UIBarButtonSystemItemFastForward,
UIBarButtonSystemItemUndo , // iOS 3.0 and later
UIBarButtonSystemItemRedo , // iOS 3.0 and later
UIBarButtonSystemItemPageCurl // iOS 4.0 and later
// 以下两种样式没有对应的图标
UIBarButtonSystemItemFlexibleSpace, // 可变长度
UIBarButtonSystemItemFixedSpace, // 固定长度
} UIBarButtonSystemItem;
// 创建工具条
UIToolbar *toolbar = [[UIToolbar alloc] init];
toolbar.barTintColor = [UIColor purpleColor];
toolbar.frame = CGRectMake(0, 0, 320, 44);
// 给工具条添加按钮
UIBarButtonItem *item0 = [[UIBarButtonItem alloc] initWithTitle:@"上一个" style:UIBarButtonItemStylePlain target:self action:@selector(previousBtnClick)];
UIBarButtonItem *item3 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
toolbar.items = @[item0, item3];
每个应用程序只有唯一的一个自己的单例UIApplication
UIApplication只能在程序中通过sharedApplication方法获得,new一个会报错
2015-06-11 12:16:22.240 06-UIApplication[1215:200037] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'There can only be one UIApplication instance.'
UIApplication *app1 = [UIApplication sharedApplication]; // bingo
//UIApplication *app2 = [[UIApplication alloc] init]; // error
UIApplication *app = [UIApplication sharedApplication];
// 设置应用程序图标上的数字
app.applicationIconBadgeNumber = 998;
UIApplication *app = [UIApplication sharedApplication];
app.networkActivityIndicatorVisible = YES; // YES显示, NO不显示
UIApplication *app = [UIApplication sharedApplication];
// app.statusBarStyle = UIStatusBarStyleLightContent; // 设置为白色样式但没有过渡动画
[app setStatusBarStyle:UIStatusBarStyleLightContent animated:YES]; // 设置为白色样式且有过渡动画
// app.statusBarHidden = YES; // 设置状态栏隐藏但没有动画
[app setStatusBarHidden:YES withAnimation:UIStatusBarAnimationFade]; // 设置状态栏隐藏但有动画
// -----------------------UIStatusBarAnimation的取值--------------------
typedef NS_ENUM(NSInteger, UIStatusBarAnimation) {
UIStatusBarAnimationNone,
#if __IPHONE_3_2 <= __IPHONE_OS_VERSION_MAX_ALLOWED
UIStatusBarAnimationFade,
UIStatusBarAnimationSlide,
#endif
};
- (BOOL)openURL:(NSURL*)url; // 通过这个方法实现
// -----------------------------------------------------
// 打电话
UIApplication *app = [UIApplication sharedApplication];
[app openURL:[NSURL URLWithString:@"tel://10086"]];
// 发短信
[app openURL:[NSURL URLWithString:@"sms://10086"]];
// 发邮件
[app openURL:[NSURL URLWithString:@"mailto://[email protected]"]];
// 打开一个网页资源
[app openURL:[NSURL URLWithString:@"http://ios.itcast.cn"]];
UIWindow是一种特殊的UIView,通常在一个app中只会有一个UIWindow
iOS程序启动完毕后,创建的第一个视图控件就是UIWindow,接着创建控制器的view,最后将控制器的view添加到UIWindow上,于是控制器的view就显示在屏幕上了
一个iOS程序之所以能显示到屏幕上,完全是因为它有UIWindow
也就说,没有UIWindow,就看不见任何UI界面
// 添加UIView到UIWindow中两种常见方式:
- (void)addSubview:(UIView *)view;
// 直接将view添加到UIWindow中,但并不会理会view对应的UIViewController
@property(nonatomic,retain) UIViewController *rootViewController;
// 自动将rootViewController的view添加到UIWindow中,负责管理rootViewController的生命周期
// 常用方法
- (void)makeKeyWindow;
// 让当前UIWindow变成keyWindow(主窗口)
- (void)makeKeyAndVisible;
// 让当前UIWindow变成keyWindow,并显示出来
[UIApplication sharedApplication].windows
// 在本应用中打开的UIWindow列表,这样就可以接触应用中的任何一个UIView对象
// (平时输入文字弹出的键盘,就处在一个新的UIWindow中)
[UIApplication sharedApplication].keyWindow
// 用来接收键盘以及非触摸类的消息事件的UIWindow,而且程序中每个时刻只能有一个UIWindow是keyWindow。如果某个UIWindow内部的文本框不能输入文字,可能是因为这个UIWindow不是keyWindow
view.window
// 获得某个UIView所在的UIWindow
//先加载storyboard文件(Test是storyboard的文件名)
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Test" bundle:nil];
//接着初始化storyboard中的控制器
// 初始化“初始控制器”(箭头所指的控制器)
NJViewController *nj = [storyboard instantiateInitialViewController];
// 或者通过一个标识初始化对应的控制器
NJViewController *nj = [storyboard instantiateViewControllerWithIdentifier:@”nj"];
2. 直接创建
NJViewController *nj = [[NJViewController alloc] init];
3. 指定xib文件创建
NJViewController *nj = [[NJViewController alloc] initWithNibName:@”NJViewController" bundle:nil];
创建View时的优先级如下