Xcode_7 iOS_9 模态视图 Objective-C (15)

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


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值