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


主要參考了這篇博客http://mobile.51cto.com/iphone-284116.htm

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

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

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

#import <Foundation/Foundation.h>

@protocol UIViewPassValueDelegate

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

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

@end

2.第一個頁面

.h文件

#import <UIKit/UIKit.h>

#import "UIViewPassValueDelegate.h"

#import "ValueInputView.h"

 @interface DelegateSampleViewController : UIViewController<UIViewPassValueDelegate>

{

    UITextField *_value;

}

@property (strong, nonatomic) IBOutletUITextField *value;

- (IBAction)buttonClick:(id)sender;

@end

.m文件

#import "DelegateSampleViewController.h"

 @implementation DelegateSampleViewController

@synthesize value = _value;

 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

    }

    returnself;

}

 - (void)viewDidLoad

{

    [superviewDidLoad];

}

 - (void)viewDidUnload

{

    [self setValue: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"

 @interface ValueInputView : 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"

 

@implementation ValueInputView

@synthesize value=_value;

@synthesize delegate;

 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

        // Custom initialization

    }

    returnself;

}

 - (void)viewDidLoad

{

    [superviewDidLoad];

}

 - (void)viewDidUnload

{

   [self setValue: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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM