UIAlertView 简单使用
// 点击tableViewCell时弹出UIAlertView
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
LFHero *hero=self.heros[indexPath.row];
// 新建 UIAlertView类 , init时,设置,alertView代理为self,需实现UIAlertViewDelegate协议
UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:@"修改数据" message:hero.intro delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
alertView.alertViewStyle=UIAlertViewStylePlainTextInput; // 设置alertView样式。
alertView.tag=indexPath.row; // 设置alertView tag属性
UITextField *textField = [alertView textFieldAtIndex:0]; //提取alertView中的textField
textField.text=hero.name;
[alertView show];
NSLog(@"%@", [[alertView textFieldAtIndex:0] text]);
}
UIAlertViewDelegate需要实现,
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;方法(当点击alertView中的按钮时,会触发)
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if(buttonIndex==1){
// 点击确定
int row=alertView.tag;
LFHero *hero1 = self.heros[row];
UITextField *textFile=[alertView textFieldAtIndex:0];
[hero1 setName:textFile.text];
NSIndexPath *path = [NSIndexPath indexPathForRow:row inSection:0];
// 刷新指定行
[self.tableView reloadRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationRight];
// 刷新整个tableView
[self.tableView reloadData];
}else{
// 点击取消
}
}
NSNotification键盘监听
// 注册键盘尺寸监听的通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChange:) name:UIKeyboardWillChangeFrameNotification object:nil];
监听方法为keyboardWillChange:
#pragma 键盘监听的通知执行方法
-(void)keyboardWillChange:(NSNotification *)notification{
NSDictionary *dict = notification.userInfo;
NSLog(@"dict"); // 其中为键盘的各种信息
// 根据notification.userInfo中的NSDictionary 来选择需要这行的动作
}