ios入门之界面跳转和数据传递

界面跳转有

(1)模态视图:

登录的时候,没有注册,跳到了注册界面,这种可以是模态视图

 界面跳转和关闭界面:

//#模态视图,比如说要登录没有注册的话,使用这个来注册吧

- (IBAction)regonclick:(id)sender {

    //初始化故事版,故事版叫做 Main

    UIStoryboard *mainStoryBoard = [UIStoryboardstoryboardWithName:@"Main"bundle:nil];

    //UIStoryboard来获取UIViewController

    UIViewController *registerViewController = [mainStoryBoardinstantiateViewControllerWithIdentifier:@"registerViewController"];

    // 从下往上动画

    // registerViewController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;

    // 翻转动画

    // registerViewController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;

    // 淡入淡出

    registerViewController.modalTransitionStyle =UIModalTransitionStyleCrossDissolve;

    // 电子书效果

    // registerViewController.modalTransitionStyle = UIModalTransitionStylePartialCurl;

    

    //数据传递方式有委托设计模式和广播通知机制?

    

    //呈现模态视图

    [selfpresentViewController:registerViewControlleranimated:YEScompletion:

     //这个类似于java匿名内部类,回调函数之类的啦

     ^{NSLog(@"跳转了哦");}];

}


关闭界面:在事件中这样写 

[selfdismissViewControllerAnimated:YEScompletion:^{

        NSLog(@"结束啦);}];


消息传递,用KVO

传数据界面的事件方法:

/*

 NSNotificationCenter消息通信机制介绍(KVO)

 作用:NSNotificationCenter是专门供程序中不同类间的消息通信而设置的.

 注册通知:即要在什么地方接受消息

 [[NSNotificationCenter defaultCenter]  addObserver:self selector:@selector(mytest:) name:@" mytest" object:nil];

 参数介绍:

 addObserver: 观察者,即在什么地方接收通知;

   selector: 收到通知后调用何种方法;

       name: 通知的名字,也是通知的唯一标示,编译器就通过这个找到通知的。

 发送通知:调用观察者处的方法。

 [[NSNotificationCenter defaultCenter] postNotificationName:@"mytest" object:searchFriendArray];

 参数:

 postNotificationName:通知的名字,也是通知的唯一标示,编译器就通过这个找到通知的。

               object:传递的参数

 注册方法的写法:

 - (void) mytest:(NSNotification*) notification

 {

 id obj = [notification object];//获取到传递的对象

 }

 */

- (IBAction)finishSelf:(id)sender {

    //关闭场景

    [selfdismissViewControllerAnimated:YEScompletion:^{

        NSLog(@"结束啦");

        NSDictionary *dataDict = [NSDictionarydictionaryWithObject:self.textfiel.textforKey:@"username"];

        [[NSNotificationCenterdefaultCenter]

         postNotificationName:@"RegisterCompletionNotification"object:nil

         userInfo:dataDict];

    }];

}


接收数据的界面代码:

- (void)viewDidLoad {

    [superviewDidLoad];

    [[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(registerCompletion:)name:@"RegisterCompletionNotification"object:nil];

}


@selector(registerCompletion:) 注意一下,这里是之后要调用的方法在本类里面为registerCompletion


registerCompletion方法如下:

//通知中心

-(void)registerCompletion:(NSNotification *)notification{

    //字典相当于javamap

    NSDictionary *theData = [notification userInfo];

    //根据键获得值

    NSString *username = [theData objectForKey:@"username"];

    NSLog(@"username = %@",username);

    //对当前页面的label设置文字

    self.label.text = username;

}


整体的代码如下:



#import "RegisterViewController.h"


@interface RegisterViewController ()

- (IBAction)finishSelf:(id)sender;

@property (weak, nonatomic) IBOutletUITextField *textfiel;


@end


@implementation RegisterViewController


- (void)viewDidLoad {

    [superviewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

}


- (void)didReceiveMemoryWarning {

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

/*

 NSNotificationCenter消息通信机制介绍(KVO)

 作用:NSNotificationCenter是专门供程序中不同类间的消息通信而设置的.

 注册通知:即要在什么地方接受消息

 [[NSNotificationCenter defaultCenter]  addObserver:self selector:@selector(mytest:) name:@" mytest" object:nil];

 参数介绍:

 addObserver: 观察者,即在什么地方接收通知;

   selector: 收到通知后调用何种方法;

       name: 通知的名字,也是通知的唯一标示,编译器就通过这个找到通知的。

 发送通知:调用观察者处的方法。

 [[NSNotificationCenter defaultCenter] postNotificationName:@"mytest" object:searchFriendArray];

 参数:

 postNotificationName:通知的名字,也是通知的唯一标示,编译器就通过这个找到通知的。

               object:传递的参数

 注册方法的写法:

 - (void) mytest:(NSNotification*) notification

 {

 id obj = [notification object];//获取到传递的对象

 }

 */

- (IBAction)finishSelf:(id)sender {

    //关闭场景

    [selfdismissViewControllerAnimated:YEScompletion:^{

        NSLog(@"结束啦");

        NSDictionary *dataDict = [NSDictionarydictionaryWithObject:self.textfiel.textforKey:@"username"];

        [[NSNotificationCenterdefaultCenter]

         postNotificationName:@"RegisterCompletionNotification"object:nil

         userInfo:dataDict];

    }];

}

@end





#import "ViewController.h"


@interface ViewController ()

@property (weak, nonatomic) IBOutletUILabel *label;


@end


@implementation ViewController


- (void)viewDidLoad {

    [superviewDidLoad];

    [[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(registerCompletion:)name:@"RegisterCompletionNotification"object:nil];

}


- (void)didReceiveMemoryWarning {

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


//#模态视图,比如说要登录没有注册的话,使用这个来注册吧

- (IBAction)regonclick:(id)sender {

    //初始化故事版,故事版叫做 Main

    UIStoryboard *mainStoryBoard = [UIStoryboardstoryboardWithName:@"Main"bundle:nil];

    //UIStoryboard来获取UIViewController

    UIViewController *registerViewController = [mainStoryBoardinstantiateViewControllerWithIdentifier:@"registerViewController"];

    // 从下往上动画

    // registerViewController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;

    // 翻转动画

    // registerViewController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;

    // 淡入淡出

    registerViewController.modalTransitionStyle =UIModalTransitionStyleCrossDissolve;

    // 电子书效果

    // registerViewController.modalTransitionStyle = UIModalTransitionStylePartialCurl;

    

    //数据传递方式有委托设计模式和广播通知机制?

    

    //呈现模态视图

    [selfpresentViewController:registerViewControlleranimated:YEScompletion:

     //这个类似于java匿名内部类,回调函数之类的啦

     ^{NSLog(@"跳转了哦");}];

}


//通知中心

-(void)registerCompletion:(NSNotification *)notification{

    //字典相当于javamap

    NSDictionary *theData = [notification userInfo];

    //根据键获得值

    NSString *username = [theData objectForKey:@"username"];

    NSLog(@"username = %@",username);

    //对当前页面的label设置文字

    self.label.text = username;

}

@end

模态视图控制器的呈现和关闭、通知中心

第一个Controller

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
- (IBAction)registerAction:(id)sender;
@property (weak, nonatomic) IBOutlet UITextField *txtName;

@end
#import "ViewController.h"

@interface ViewController ()

@property (strong, nonatomic) NSArray * events;
@end

@implementation ViewController
#pragma mark - 加载视图
- (void)viewDidLoad
{
    [super viewDidLoad];
    //注册通知中心
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(registerCompletion:) name:@"RegisterCompletionNotification" object:nil];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    //移除这个通知中心观察者
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (IBAction)registerAction:(id)sender {
    
    UIStoryboard *mainBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    UIViewController *controller = [mainBoard instantiateViewControllerWithIdentifier:@"registerViewController"];
    //加载其他controller效果 
    controller.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    [self presentViewController:controller
                       animated:YES
                     completion:^{NSLog(@"展现controller");}];
}
-(void)registerCompletion:(NSNotification *)notification
{
    NSDictionary *date = [notification userInfo];
    NSString *username = [date objectForKey:@"username"];
    self.txtName.text = username;
}

@end

第二个Controller
#import <UIKit/UIKit.h>

@interface RegisterViewController : UIViewController
@property (weak, nonatomic) IBOutlet UITextField *txt;
- (IBAction)doneAction:(id)sender;

@end

#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 - 关闭自己
- (IBAction)doneAction:(id)sender {
    [self dismissViewControllerAnimated:YES completion:^{
        NSLog(@"调用dismissViewControllerAnimated方法");
        NSDictionary *dateDict = [NSDictionary dictionaryWithObject:self.txt.text
                                                             forKey:@"username"];
        //发送通知
        [[NSNotificationCenter defaultCenter] postNotificationName:@"RegisterCompletionNotification"
                                                            object:nil userInfo:dateDict];
    }];
}
@end

呈现模态:presentViewController:animated:completion

关闭模态:dismissViewControllerAnimated:completion


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值