ios 页面跳转之间传递数据----通过delegate
主要参考了这篇博客http://mobile.51cto.com/iphone-284116.htm
主要用到了,两个类,一个delegate
a类,调用b类,当b类执行之后,需要把一个数据传递给a类,a类把这个数据显示出来。
1.delegate,就这一个头文件就足够了。在类中去实现这个代理方法
#import <Foundation/Foundation.h>
@protocolUIViewPassValueDelegate
- (void)passValue:(NSString*)value;
一旦某个类,实现了这个回调函数,这个类就会获取当前的value数据。因此,接收数据的类一定实现一个回调函数。对于当前项目就是passValue
@end
2.第一个页面
.h文件
#import <UIKit/UIKit.h>
#import "UIViewPassValueDelegate.h"
#import "ValueInputView.h"
@interfaceDelegateSampleViewController : UIViewController<UIViewPassValueDelegate>
{
UITextField*_value;
}
@property(strong, nonatomic) IBOutletUITextField*value;
- (IBAction)buttonClick:(id)sender;
@end
.m文件
#import "DelegateSampleViewController.h"
@implementationDelegateSampleViewController
@synthesizevalue = _value;
- (id)initWithNibName:(NSString*)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil
{
self= [superinitWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if(self) {
}
returnself;
}
- (void)viewDidLoad
{
[superviewDidLoad];
}
- (void)viewDidUnload
{
[selfsetValue:nil];
[superviewDidUnload];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return(interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (IBAction)buttonClick:(id)sender
{
ValueInputView* valueView = [[ValueInputViewalloc] init];
valueView.delegate= self;
[selfsetModalTransitionStyle:UIModalTransitionStyleCoverVertical];
[selfpresentModalViewController:valueView animated:YES];
}
-(void)passValue:(NSString*)value
{
self.value.text= value;
NSLog(@"the get value is %@",value);
}
@end
3.第二个类:
.h文件
#import <UIKit/UIKit.h>
#import "UIViewPassValueDelegate.h"
@interfaceValueInputView : UIViewController
{
NSObject<UIViewPassValueDelegate>* delegate;
UITextField* _value;
}
@property(retain, nonatomic) IBOutletUITextField*value;
@property(nonatomic,retain) NSObject<UIViewPassValueDelegate>* delegate;
- (IBAction)buttonClick:(id)sender;
@end
.m文件
#import "ValueInputView.h"
@implementationValueInputView
@synthesizevalue=_value;
@synthesizedelegate;
- (id)initWithNibName:(NSString*)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil
{
self= [superinitWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if(self) {
// Custom initialization
}
returnself;
}
- (void)viewDidLoad
{
[superviewDidLoad];
}
- (void)viewDidUnload
{
[selfsetValue:nil];
[superviewDidUnload];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return(interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (IBAction)buttonClick:(id)sender {
[delegatepassValue:self.value.text];
NSLog(@"self.value.text is %@",self.value.text);
[selfdismissModalViewControllerAnimated:YES];
}
@end
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。