zoukankan      html  css  js  c++  java
  • ioS 页面与页面之间传参数的方法 代理传值

    代理传值 是用于反向传值

    1.创建协议 及协议方法 在反向传值的页面(SecondVC)中

    2.创建协议类型 的属性 在(SecondVC)中创建属性 id<XXXPostValueDelegate> delegate

    3.调用属性 即delegate 在(SecondVC)页面中的 对象传值的方法调用  [self.delegate  postValue:self.MyText.text];

    4.在第一页 即显示修改信息的页面 遵循协议 实现协议方法 指定代理对象 (即 在页面传递参数的方法中为代理赋值 second.delegate=self;)

    #import <UIKit/UIKit.h>

    #import "FirstViewController.h"

    @interface AppDelegate : UIResponder <UIApplicationDelegate>

    @property (strong, nonatomic) UIWindow *window;

    @end

    #import "AppDelegate.h"

    @interface AppDelegate ()

    @end

    @implementation AppDelegate

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

    {

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

        self.window.rootViewController=first;

        return YES;

    }

    #import <UIKit/UIKit.h>

    #import "SecondViewController.h"

    @interface FirstViewController : UIViewController<UITextFieldDelegate,PostValueDeledate>

    @property(strong,nonatomic) UITextField *MyText;

    @property(strong,nonatomic) UIButton *MyButton;

    @end

    #import "FirstViewController.h"

    @interface FirstViewController ()

    @end

    @implementation FirstViewController

    - (void)viewDidLoad

    {

        [super viewDidLoad];

        self.view.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"106.jpg"]];

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

        self.MyText.delegate=self;

        self.MyText.borderStyle=1;

        [self.view addSubview:self.MyText];

        

        //初始化按钮

        self.MyButton=[[UIButton alloc] initWithFrame:CGRectMake(150, 200, 100, 50)];

        //给按钮添加背景色

        self.MyButton.backgroundColor=[UIColor redColor];

        [self.MyButton setTitle:@"next" forState:UIControlStateNormal];

        [self.MyButton addTarget:self action:@selector(NextPage) forControlEvents:UIControlEventTouchUpInside];

        [self.view addSubview:self.MyButton];

      }

    -(void)NextPage

    {

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

        //指定代理

        second.delegate=self;

        second.MyStr=self.MyText.text;

        [self presentViewController:second animated:YES completion:

         ^{

             NSLog(@"下一页");

        }];

        

    }

    -(BOOL)textFieldShouldReturn:(UITextField *)textField

    {

        //隐藏键盘

        if ([self isFirstResponder])

        {

            [self resignFirstResponder];

        }

        return YES;

    }

    //4.在第一页 即显示修改信息的页面 遵循协议 实现协议方法 指定代理对象 (即 在页面传递参数的方法中为代理赋值 second.delegate=self;)

    -(void)PostValue:(NSString *)Infor

    {

        self.MyText.text=Infor;

     }

    #import <UIKit/UIKit.h>

    //1.创建协议 及协议方法 在反向传值的页面(SecondVC)中

    @protocol PostValueDeledate <NSObject>

    -(void)PostValue:(NSString *)Infor;

    @end

    @interface SecondViewController : UIViewController<UITextFieldDelegate>

    //2.创建协议类型 的属性 在(SecondVC)中创建属性 id<XXXPostValueDelegate> delegate

    @property(strong,nonatomic) id<PostValueDeledate> delegate;

    @property(strong,nonatomic) NSString *MyStr;

    @property(strong,nonatomic) UITextField *MyText;

    @end

    #import "SecondViewController.h"

    @interface SecondViewController ()

    @end

    @implementation SecondViewController

    - (void)viewDidLoad

    {

        [super viewDidLoad];

        self.view.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"8efce6cd7b899e51feb2371b40a7d933c8950d83.jpg"]];

        

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

        self.MyText.borderStyle=1;

        self.MyText.delegate=self;

        self.MyText.text=self.MyStr;

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

        [self.view addSubview:self.MyText];

    }

    -(BOOL)textFieldShouldReturn:(UITextField *)textField

    {

        //隐藏键盘

        if ([self isFirstResponder])

        {

            [self resignFirstResponder];

        }

        //3.调用属性 即delegate 在(SecondVC)页面中的 对象传值的方法调用  [self.delegate  postValue:self.MyText.text];

        if (self.delegate)

        {

            [self.delegate PostValue:self.MyText.text];

        }

        //让本页面消失

        [self dismissViewControllerAnimated:YES completion:

        ^{

            NSLog(@"OK");

        }];

        

        return YES;

    }

  • 相关阅读:
    【Pandas最好用的函数】
    【自动下单外挂】
    【爬取新浪股票实时数据--tushare】
    linux入门:设置代理服务器
    nginx入门(3):静态服务搭建
    nginx入门(2):配置文档结构
    nginx入门(1):开启,关闭,重新加载配置文件
    SELinux的开启和关闭
    centos7 播放网页flash视频
    索引最左前缀匹配原则
  • 原文地址:https://www.cnblogs.com/tmf-4838/p/5281507.html
Copyright © 2011-2022 走看看