zoukankan      html  css  js  c++  java
  • ios页面间传递参数四种方式

    1、使用SharedApplication,定义一个变量来传递.

    2、使用文件,或者NSUserdefault来传递

    3、通过一个单例的class来传递

    4、通过Delegate来传递。

    IOS开发使用委托delegate在不同窗口之间传递数据是本文要介绍的内容,主要是来讲解如何使用委托delegate在不同窗口之间传递数据,具体内容来看详细内容。在IOS开发里两个UIView窗口之间传递参数方法有很多,比如

    前面3种方法,暂且不说,这次主要学习如何使用通过Delegate的方法来在不同的UIView里传递数据

    比如: 在窗口1中打开窗口2,然后在窗口2中填入一个数字,这个数字又回传给窗口1。

    窗口1

    IOS开发使用委托delegate在不同窗口之间传递数据

    窗口2

    IOS开发使用委托delegate在不同窗口之间传递数据

    窗口2的结果传递给窗口1

    IOS开发使用委托delegate在不同窗口之间传递数据

    1、首先定义个一委托UIViewPassValueDelegate用来传递值

    @protocol UIViewPassValueDelegate  
    - (void)passValue:(NSString *)value;  
    @end 

    这个protocol 就是用来传递值

    2、在窗口1的头文件里,声明delegate

    复制代码
    复制代码
    #import <UIKit/UIKit.h> 
    #import "UIViewPassValueDelegate.h"  
    @interface DelegateSampleViewController : UIViewController <UIViewPassValueDelegate> 
    {  
        UITextField *_value;  
    }  
    @property(nonatomic, retain) IBOutlet UITextField *value;  
    - (IBAction)buttonClick:(id)sender;  
    @end 
    复制代码
    复制代码

    并实现这个委托

    - (void)passValue:(NSString *)value  
    {  
      self.value.text = value;  
        NSLog(@"the get value is %@", value);  
    } 

    button的Click方法,打开窗口2,并将窗口2的delegate实现方法指向窗口1。

    复制代码
    复制代码
    - (IBAction)buttonClick:(id)sender  
    {  
        ValueInputView *valueView = [[ValueInputView alloc] initWithNibName:@"ValueInputView" bundle:[NSBundle mainBundle]];  
        valueView.delegate = self;  
        [self setModalTransitionStyle:UIModalTransitionStyleCoverVertical];  
        [self presentModalViewController:valueView animated:YES];  
    } 
    复制代码
    复制代码

    第二个窗口的实现

    .h 头文件

    复制代码
    复制代码
    #import <UIKit/UIKit.h> 
    #import "UIViewPassValueDelegate.h"  
     
    @interface ValueInputView : UIViewController {  
     
        NSObject<UIViewPassValueDelegate> * delegate;  
        UITextField *_value;  
    }  
    @property(nonatomic, retain)IBOutlet UITextField *value;  
    @property(nonatomic, retain) NSObject<UIViewPassValueDelegate> * delegate;  
    - (IBAction)buttonClick:(id)sender;  
    @end 
    复制代码
    复制代码

    .m实现文件

    复制代码
    复制代码
    #import "ValueInputView.h"  
    @implementation ValueInputView  
    @synthesize delegate;  
    @synthesize value = _value;  
    - (void)dealloc {  
        [self.value release];  
        [super dealloc];  
    }  
     
    - (IBAction)buttonClick:(id)sender  
    {  
        [delegate passValue:self.value.text];  
        NSLog(@"self.value.text is%@", self.value.text);  
        [self dismissModalViewControllerAnimated:YES];      
         
    }  
    - (void)didReceiveMemoryWarning {  
        // Releases the view if it doesn't have a superview.  
        [super didReceiveMemoryWarning];  
          
        // Release any cached data, images, etc. that aren't in use.  
    }  
     
    - (void)viewDidUnload {  
        [super viewDidUnload];  
        // Release any retained subviews of the main view.  
        // e.g. self.myOutlet = nil;  
    }  
     
     
    @end 
    复制代码
  • 相关阅读:
    python_day10 socket serverr
    python_day10 协程 GEVENT
    python_day10 协程 Greenlet
    python_day10 协程
    python_day10 paramiko模块
    python-day10 线程 queue
    python_day10 event
    python_day10 信号量
    python_day10 锁
    CSS命名规范(规则)
  • 原文地址:https://www.cnblogs.com/Peak-Banish/p/4004980.html
Copyright © 2011-2022 走看看