如果您在阅读我的文章时有疑问 , 请点击这里
- 首先有两个ViewController , 实现两个界面
- 在第二个界面的 .h 里面 写协议方法 和定义协议
//协议方法
@class AddViewController;
@protocol AddViewControllerDelegate <NSObject>
@optional
-(void)addViewController:(AddViewController *)addViewController withTeacher :(Teacher *)teacher;
@end
@interface AddViewController : UIViewController
//定义协议
@property (nonatomic,strong)id<AddViewControllerDelegate> delegate;
@end
-(void)addclick{
[self.navigationController popViewControllerAnimated:YES];
//判断代理方法是否响应
if ([self.delegate respondsToSelector:@selector(addViewController:withTeacher:)]) {
//执行代理方法
Teacher *t=[Teacher new];
t.name=self.nameField.text;
t.number=self.numberField.text;
[self.delegate addViewController:self withTeacher:t];
}
}
@interface ViewController ()<AddViewControllerDelegate,EditViewControllerDelegate>
//代理方法
-(void)addViewController:(AddViewController *)addViewController withTeacher:(Teacher *)teacher{
[self.array addObject:teacher];
NSLog(@"%@",teacher.name);
[table reloadData];
//归档
[NSKeyedArchiver archiveRootObject:_array toFile:Kfilepath];
}
网友评论