1.push 按钮
//初始化push按钮
- (void)initPushButton
{
UIButton *pushButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
pushButton.frame = CGRectMake(100, 100, 200, 40);
[pushButton addTarget:self action:@selector(pushAction) forControlEvents:UIControlEventTouchUpInside];
[pushButton setTitle:@"push" forState:UIControlStateNormal];
[self.view addSubview:pushButton];
}
- (void)pushAction
{
pushViewController *pushVC = [[pushViewController alloc] init];
**[self.navigationController pushViewController:pushVC animated:YES];**
//这是是否显示工具栏的设置 标签栏
RootViewController *rootVC = (RootViewController *)self.tabBarController;
[rootVC showTabBar:NO];
}
2.模态视图 (返回主视图 )
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor blackColor];
UIButton *pushButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
pushButton.frame = CGRectMake(100, 100, 200, 40);
[pushButton addTarget:self action:@selector(dismissAction) forControlEvents:UIControlEventTouchUpInside];
[pushButton setTitle:@"dismiss" forState:UIControlStateNormal];
[self.view addSubview:pushButton];
}
- (void)dismissAction
{
**[self dismissViewControllerAnimated:YES completion:nil];**
}
3.导航栏 模态视图设置
//自定义导航栏按钮
- (void) initNavButton{
UIButton *writeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
writeBtn.frame = CGRectMake(0, 0, writeButtonWidth, writeButtonHeight);
[writeBtn setBackgroundImage:[UIImage imageNamed:@"write"] forState:UIControlStateNormal];
[writeBtn addTarget:self action:@selector(presentAction) forControlEvents:UIControlEventTouchUpInside];
//初始化按钮
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:writeBtn];
self.navigationItem.leftBarButtonItem = item;
}
//模态视图
- (void)presentAction
{
ModelViewController *modelVC = [[ModelViewController alloc]init];
**[self presentViewController:modelVC animated:YES completion:nil];**
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
3.关闭视图
//添加数据
- (IBAction)AddAction {
//1.关闭当前视图控制器
[self.navigationController popViewControllerAnimated:NO];
---------------------------------------------------------
//代理传值
if([self.delegate respondsToSelector:@selector(addContact:didAddContact:)])
{
contactModel *ctModel = [[contactModel alloc]init];
ctModel.name = self.nameField.text;
ctModel.phone = self.phoneField.text;
[self.delegate addContact:self didAddContact:ctModel];
}
// //1.关闭当前视图控制器 这是跳转到根view
//
[self.navigationController popToRootViewControllerAnimated:YES];
----------------------------------------------------------------
// //代理传值
// if ([self.delegate respondsToSelector:@selector(addContact:didAddContact:)]) {
// JKContactModel *contactModel = [[JKContactModel alloc] init];
// contactModel.name = self.nameField.text;
// contactModel.phone = self.phoneField.text;
// [self.delegate addContact:self didAddContact:contactModel];
// }