zoukankan      html  css  js  c++  java
  • iOS之页面传值-----单例传值、通知传值

    单例传值

    //单例的.h文件

    @property(strong,nonatomic)NSString *name;

    +(userInfo *)shareinstence;

    //单例的.m文件

    static userInfo *instence=nil;

    //第一种写法

    //+(userInfo *)shareinstence

    //{

    //    //懒加载

    //    if (instence==nil)

    //    {

    //        instence=[[userInfo alloc] init];

    //    }

    //    return instence;

    //}

    //第二种写法

    +(userInfo *)shareinstence

    {

        @synchronized(self)

        {

            if (instence==nil)

            {

                instence=[[userInfo alloc] init];

            }

            return instence;

        }

        //懒加载

        

    }

    //重写allocWithZone方法

    +(instancetype)allocWithZone:(struct _NSZone *)zone

    {

        if (instence==nil)

        {

            instence=[super allocWithZone:zone];

        }

        return instence;

    }

    //重写copyWithZone

    -(id)copyWithZone:(NSZone *)zone

    {

        return self;

    }

    //然后新建两个类

    //第一个类.h

    #import <UIKit/UIKit.h>

    #import "userInfo.h"

    #import "secondViewController.h"

    @interface ViewController : UIViewController

    @property(strong,nonatomic)UITextField *textname;

    @end

    //第一个类.m

    - (void)viewDidLoad {

        [super viewDidLoad];

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

       

        self.textname=[[UITextField alloc] initWithFrame:CGRectMake(100, 100, 100, 50)];

        self.textname.borderStyle=1;

        [self.view addSubview:self.textname];

    }

    -(void)viewWillAppear:(BOOL)animated

    {

        [super viewWillAppear:animated];

        userInfo *info=[userInfo shareinstence];

        NSLog(@"%@",info.name);

    }

    -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event

    {

        userInfo *userinfo=[userInfo shareinstence];

        userinfo.name=self.textname.text;

        

        [self presentViewController:[secondViewController new] animated:YES completion:^{

            NSLog(@"下一页");

        }];

    }

     第二个类也是一样的道理

    通知传值

    创建两个类,继承于UIviewcontroller

    第一个类.h

    #import <UIKit/UIKit.h>

    #import "secondViewController.h"

    @interface ViewController : UIViewController

    @property(strong,nonatomic)UITextField *txt;

    @end

    第一个类.m

    - (void)viewDidLoad {

        [super viewDidLoad];

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

        self.txt=[[UITextField alloc] initWithFrame:CGRectMake(100, 100, 100, 50)];

        self.txt.borderStyle=1;

        [self.view addSubview:self.txt];

        

    }

    -(void)viewWillAppear:(BOOL)animated

    {

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

        

    }

    -(void)dealloc

    {

        [[NSNotificationCenter defaultCenter] removeObserver:self name:@"LAY" object:nil];

        

    }

    -(void)myPostValue:(NSNotification *)noti

    {

        self.txt.text=noti.object;

    }

    -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event

    {

        secondViewController *second=[[secondViewController alloc] init];

        [self presentViewController:second animated:YES completion:^{

            NSLog(@"下一页");

        }];

    }

    第二个类的.h

    #import <UIKit/UIKit.h>

     遵循UITextFieldDelegate的协议

    @interface secondViewController : UIViewController<UITextFieldDelegate>

    @property(strong,nonatomic)NSString *str;

    @property(strong,nonatomic)UITextField *txtinfo;

    @end

    第二个类的.m

    - (void)viewDidLoad {

        [super viewDidLoad];

        // Do any additional setup after loading the view.

      

        self.view.backgroundColor=[UIColor grayColor];

        NSLog(@"%@",self.str);

        self.txtinfo=[[UITextField alloc] initWithFrame:CGRectMake(100, 100, 150, 50)];

        self.txtinfo.borderStyle=1;

        self.txtinfo.delegate=self;

        self.txtinfo.text=self.str;

        

        [self.view addSubview:self.txtinfo];

        

    }

    -(BOOL)textFieldShouldReturn:(UITextField *)textField

    {

        

        //通知

        NSNotification *notification=[[NSNotification alloc] initWithName:@"LAY" object:textField.text userInfo:nil];

        [[NSNotificationCenter defaultCenter]postNotification:notification];

        

        //隐藏键盘

        if ([textField isFirstResponder])

        {

            [textField resignFirstResponder];

        }

        

        //隐藏页面

        [self dismissViewControllerAnimated:YES completion:^{

            NSLog(@"返回");

        }];

        return YES;

    }

  • 相关阅读:
    Azure 中 Linux 虚拟机的大小
    排查在 Azure 中创建、重启 Linux VM 或调整其大小时发生的分配故障
    如何在 Azure 中的 Linux 经典虚拟机上设置终结点
    针对通过 SSH 连接到 Azure Linux VM 时发生的失败、错误或被拒绝问题进行故障排除
    Linux 内核超时导致虚拟机无法正常启动
    Java并发编程(十三)同步容器类
    可以开发着玩一下的web项目
    org.tmatesoft.svn.core.SVNCancelException: svn: E200015: authentication canc
    FastDFS单机搭建以及java客户端Demo
    做前端(单纯页面和js)遇到的问题辑录(一)
  • 原文地址:https://www.cnblogs.com/layios/p/5285363.html
Copyright © 2011-2022 走看看