你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> ios 頁面跳轉之間傳遞數據----通過delegate

ios 頁面跳轉之間傳遞數據----通過delegate

編輯:IOS開發綜合

 

主要用到了,兩個類,一個delegate

a類,調用b類,當b類執行之後,需要把一個數據傳遞給a類,a類把這個數據顯示出來。

1.delegate,就這一個頭文件就足夠了。在類中去實現這個代理方法

#import

@protocolUIViewPassValueDelegate

- (void)passValue:(NSString*)value;

一旦某個類,實現了這個回調函數,這個類就會獲取當前的value數據。因此,接收數據的類一定實現一個回調函數。對於當前項目就是passValue

@end

2.第一個頁面

.h文件

#import

#import UIViewPassValueDelegate.h

#import ValueInputView.h

@interfaceDelegateSampleViewController : UIViewController

{

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

#import UIViewPassValueDelegate.h

@interfaceValueInputView : UIViewController

{

NSObject* delegate;

UITextField* _value;

}

@property(retain, nonatomic) IBOutletUITextField*value;

@property(nonatomic,retain) NSObject* 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

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved