1、新建SingleViewApplication项目,在storyboard面板把原有的控件删除,拖动一个NavigationController控件,然后删掉右边的root view controller,然后再添加一个ViewController控件,吧NavigationController与之连结,同样是用segue去连结(有点重复,主要是ViewController控件有上下边栏,并且原有的root view controller的导航栏不能删除),类似再做一遍,一个是登录一个是注册,添加响应的控件,两个导航栏之间用present modally来连接(从注册按钮拖动到注册导航视图上)注册页面的两个按钮Cancel和Save都是设置为系统默认按钮:
2、两个导航视图我们需要两个类,现在已经有一个ViewController了,现在我们在添加一个cocoa touch class,名字叫RegisterViewController,并且把两个视图控制器页面分别配置为这两个类:
3、ViewController的代码如下,其中有些需要自己定义输出口和事件:
//
// ViewController.h
// TestProject
//
// Created by 侯家奇 on 16/8/17.
// Copyright © 2016年 侯家奇. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
//
// ViewController.m
// TestProject
//
// Created by 侯家奇 on 16/8/17.
// Copyright © 2016年 侯家奇. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(registerCompletion:) name:@"RegisterCompletionNotification" object:nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)registerCompletion:(NSNotification *)notification {
NSDictionary *theData = [notification userInfo];
NSString *username = [theData objectForKey:@"username"];
NSLog(@"username = %@", username);
}
@end
4、RegisterViewController的代码如下,都是采用通知机制将参数传给登录视图的:
//
// RegisterViewController.h
// TestProject
//
// Created by 侯家奇 on 16/8/29.
// Copyright © 2016年 侯家奇. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface RegisterViewController : UIViewController
@property (weak, nonatomic) IBOutlet UITextField *txtUsername;
- (IBAction)cancel:(id)sender;
- (IBAction)save:(id)sender;
@end
//
// RegisterViewController.m
// TestProject
//
// Created by 侯家奇 on 16/8/29.
// Copyright © 2016年 侯家奇. All rights reserved.
//
#import "RegisterViewController.h"
@interface RegisterViewController ()
@end
@implementation RegisterViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
- (IBAction)cancel:(id)sender {
[self dismissViewControllerAnimated:YES completion:^{NSLog(@"点击Cancel按钮,关闭模态视图");}];
}
- (IBAction)save:(id)sender {
[self dismissViewControllerAnimated:YES completion:^{
NSLog(@"点击Save按钮,关闭模态视图");
NSDictionary *dataDict = [NSDictionary dictionaryWithObject:self.txtUsername.text forKey:@"username"];
[[NSNotificationCenter defaultCenter]
postNotificationName:@"RegisterCompletionNotification"
object:nil
userInfo:dataDict];
}];
}
@end