• 单例传值


    //创建单例

    @interface Singleton : NSObject

     @property (retain,nonatomic) NSString *info;

    +(instancetype)shareInstance;

     @end

    //单例实现

    @implementation Singleton

    static Singleton *_instance = nil;

    +(instancetype)shareInstance

    {

        if (_instance == nil) {

            _instance = [[super alloc] init];

        }

        return _instance;

    }

    @end

    //  FirstViewController

    #import "Singleton.h"

    @interface FirstViewController : UIViewController<UITextFieldDelegate>

    @property (strong,nonatomic) UITextField *myText;

    @property (strong,nonatomic) NSString *str;

    @end

    @implementation FirstViewController

    - (void)viewDidLoad {

        [super viewDidLoad];

        

        self.view.backgroundColor = [UIColor redColor];

        

        self.myText = [[UITextField alloc] initWithFrame:CGRectMake(100, 100, 100, 40)];

        self.myText.borderStyle = 2;

        self.myText.backgroundColor = [UIColor redColor];

        [self.view addSubview:self.myText];

        self.myText.delegate = self;

        self.myText.text = self.str;

        

    }

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

    {

        if ([self.myText isFirstResponder]) {

            [self.myText resignFirstResponder];

            

        }

    }

    -(BOOL)textFieldShouldReturn:(UITextField *)textField

    {

        Singleton *sin = [Singleton shareInstance];

        

         sin.info = self.myText.text;

        NSLog(@"%@,%@",self.myText.text,sin.info);

        [self dismissViewControllerAnimated:YES completion:^{

            

        }];

        return YES;

    }

    //  ViewController

    #import "FirstViewController.h"

    #import "Singleton.h"

    @interface ViewController : UIViewController<UITextFieldDelegate>

    @property (strong,nonatomic) UITextField *textF;

    @end

    @implementation ViewController

    - (void)viewDidLoad {

        [super viewDidLoad];

        self.textF = [[UITextField alloc] initWithFrame:CGRectMake(100, 100, 100, 40)];

        self.textF.borderStyle = 2;

        self.textF.backgroundColor = [UIColor redColor];

        [self.view addSubview:self.textF];

        self.textF.delegate = self;

        

        

    }

    -(void)viewWillAppear:(BOOL)animated

    {

        self.textF.text = [Singleton shareInstance].info;

        

    }

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

    {

        if ([self.textF isFirstResponder]) {

            [self.textF resignFirstResponder];

            FirstViewController *firstC = [[FirstViewController alloc] init];

            firstC.str = self.textF.text;

            

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

                

            }];

        }

    }

  • 相关阅读:
    如果 TCP 是一场恋爱:遇到心动的女孩时,如何去把握?
    再来聊一聊「动态规划」
    如何判断一个数是否在 40 亿个整数中?
    写代码之外,如何再赚一份工资?
    学好这13种数据结构,应对各种编程语言(C++版)
    GitHub 标星 3w+,很全面的算法和数据结构知识
    和程序员小吴学算法文章导航
    Broadcast组件——收发广播应用——捕获屏幕的变更事件——竖屏与横屏切换
    homebrew
    git连接gitlab远程仓库
  • 原文地址:https://www.cnblogs.com/wujie123/p/5281207.html
Copyright © 2020-2023  润新知