iOS 不一样类之间的传值

  iOS是面向对象开发的,有不少不一样的类,不少时候会遇到类与类之间的"交流"需求,好比通知、传递数值等等,(通知能够用nsnotificationcenter来作, 之后总结)下面主要总结一下类之间的传值方式。post

一、属性传值学习

  前向后传值atom


二、协议传值
spa


三、Block传值
代理

代替协议代理传值,主要时间点问题。code


四、单例传值
数据共享。
server


五、通知传值
对象

通知中心
NSNotificationCenter提供了一种更加解耦的方式。最典型的应用就是任何对象对能够发送通知到中心,同时任何对象能够监听中心的通知。
发送通知的代码以下:
blog

[[NSNotificationCenter defaultCenter]      postNotificationName:@”myNotificationName”     object:broadcasterObject];开发


注册接收通知的代码以下:

[[NSNotificationCenter defaultCenter]      addObserver:listenerObject      selector:@selector(receivingMethodOnListener:)      name:@”myNotificationName”     object:nil];
注册通知的时候能够指定一个具体的广播者对象,但这不是必须的。你可能注意到了defaultCenter 。实际上这是你在应用中会使用到的惟一的中心。通知会向整个应用开放,所以只有一个中心。
同时还有一个NSDistributedNotificationCenter。这是用来应用间通讯的。在整个计算机上只有一个该类型的中心。
优势: 通知的发送者和接受者都不须要知道对方。能够指定接收通知的具体方法。通知名能够是任何字符串。
缺点: 较键值观察须要多点代码。在删掉前必须移除监听者。不能传大量数值,只能让谁去作什么事。

 

 

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

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

三、经过一个单例的class来传递

四、经过Delegate来传递。(经常使用)

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

 

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

 

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

 

窗口1

 

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

 

窗口2

 

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

 

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

 

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

 

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

 

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

 

这个protocol 就是用来传递值

 

二、在窗口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 
相关文章
相关标签/搜索