1.访问相机相册
#pragma mark 拍照
- (void)takePhoto {
if ([UIImagePickerController isSourceTypeAvailable:(UIImagePickerControllerSourceTypeCamera)]) {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES; //是否可编辑
//摄像头
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:picker animated:YES completion:nil];
AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
if (authStatus == AVAuthorizationStatusRestricted || authStatus ==AVAuthorizationStatusDenied) {
//无权限
UIAlertController *alert = [YLAlertController createOneButtonAlertViewWithTitle:nil message:@"请在iPhone的\"设置-隐私\"选项中,允许迪动访问你的摄像头" buttonTitle:@"确定" buttonAction:nil];
[picker presentViewController:alert animated:YES completion:nil];
}
}
}
#pragma mark 从相册选择
- (void)chooseFromAlbum {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
//打开相册选择照片
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:YES completion:nil];
}
}
#pragma mark 相机相册选择后的代理事件,image即为选择的图片对象
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
//得到图片
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
[self dismissViewControllerAnimated:YES completion:nil];
}
2.自己封装的网络请求的调用
NSDictionary *parameters = @{
@"uid":USER_ID,
@"token":USER_CURRENT_TOKEN,
};
[[NetRequest sharedRequest] postURL:[<#请求地址#> getFullRequestPath] parameters:parameters success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"请求结果是%@",responseObject);
if ([responseObject isRequestSuccess]) {
} else {
HUD_TEXTONLY([responseObject requestErrorInfo]);
}
} failed:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
HUD_FAILED(REQUEST_FAILED_INFO);
}];
3.注册键盘弹出和消失的通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
#pragma mark 键盘的通知
- (void)keyboardWillShow:(NSNotification *)noti {
NSDictionary *dict = noti.userInfo;
NSValue *frameValue = [dict valueForKey:UIKeyboardFrameEndUserInfoKey];
CGRect frame = [frameValue CGRectValue];
CGFloat height = frame.size.height;
self.tableView.contentInset = UIEdgeInsetsMake(0, 0, height, 0);
}
- (void)keyboardWillHide:(NSNotification *)noti {
self.tableView.contentInset = UIEdgeInsetsZero;
}
4.设置tableView
- (UITableView *)tableView {
if (!_tableView) {
_tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
_tableView.delegate = self;
_tableView.dataSource = self;
_tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
}];
_tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
}];
[self.view addSubview:_tableView];
[_tableView mas_updateConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self.view);
}];
}
return _tableView;
}
#pragma mark UITableViewDelegate,UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return <#NSInteger#>;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return <#NSInteger#>;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *reuseIdentifier = @"";
<#UITableViewCell#> *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
if (!cell) {
cell = [[<#UITableViewCell#> alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
}
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return <#expression#>;
}
5.获取当前的年月日等
NSCalendar *calendar = [NSCalendar currentCalendar];
NSUInteger unitFlags = NSCalendarUnitYear |
NSCalendarUnitMonth | NSCalendarUnitDay |
NSCalendarUnitHour | NSCalendarUnitMinute |
NSCalendarUnitSecond | NSCalendarUnitWeekday;
NSDateComponents *dateComponent = [calendar components:unitFlags fromDate:[NSDate date]];
NSInteger year = [dateComponent year];
NSInteger month = [dateComponent month];
6.UITableView嵌套UITextView时无法通过修改tableView的contentInset来让tableView自动上移,避免键盘遮挡textView,可用下面方法解决#pragma mark 键盘的通知
#pragma mark 键盘的通知
- (void)keyboardWillShow:(NSNotification *)noti {
NSDictionary *dict = noti.userInfo;
NSValue *frameValue = [dict valueForKey:UIKeyboardFrameEndUserInfoKey];
CGRect frame = [frameValue CGRectValue];
CGFloat height = frame.size.height;
self.tableView.contentInset = UIEdgeInsetsMake(0, 0, height, 0);
if ([_explainTextView isFirstResponder]) {
//将textView的frame转换为相对于tableView的frame
CGRect frame1 = [_explainTextView convertRect:_explainTextView.frame toView:self.tableView];
//height为键盘高度,(SCREEN_HEIGHT - 64 - frame1.origin.y - frame1.size.height)为textView底边距离屏幕底边的高度
self.tableView.contentOffset = CGPointMake(0, height - (SCREEN_HEIGHT - 64 - frame1.origin.y - frame1.size.height));
}
}
- (void)keyboardWillHide:(NSNotification *)noti {
self.tableView.contentInset = UIEdgeInsetsZero;
}
7.限制金额输入的方法
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:@".0123456789"] invertedSet]; NSString*filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""]; BOOL basicTest = [string isEqualToString:filtered]; if(!basicTest) { return NO; } else { if ([string isEqualToString:@"."]) { if ([textField.text isEqualToString:@""]) { textField.text = @"0."; return NO; } else if ([textField.text containsString:@"."]) { return NO; } } NSRange myRange = [textField.text rangeOfString:@"."]; if (myRange.length != 0) { if ([textField.text length]-myRange.location >= 3) { if ([string isEqualToString:@""]) { return YES; } else { return NO; } }else { return YES; } } else { return YES; } } }
8.UITextField密文输入时再次编辑内容不清空
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
//得到输入框的内容
NSString * textfieldContent = [textField.text stringByReplacingCharactersInRange:range withString:string];
if (textField == self.passwordTextField && textField.isSecureTextEntry ) {
textField.text = textfieldContent;
return NO;
}
return YES;
}