Monday, February 3, 2014

Sample Delegate

Web Hosting


In this tutorial, I will show you how to create a simple delegate that passing a data in any class. In your first class, we just name it FirstClass.h and FirstClass.m. See the code below.


FirstClass.h File

//  Created by mm20 on 1/28/14.
//  Copyright (c) 2014 mm20. All rights reserved.
//

#import <UIKit/UIKit.h>


@class FirstClass;
@protocol passDelegate <NSObject>

-(void)didPass:(FirstClass *)controller nname:(NSString *)nname rname:(NSString *)rname amount:(NSString *)amount charge:(NSString *)charge total:(NSString *)total;

@end
@interface FirstClass : UIViewController

- (IBAction)btn_preview:(id)sender;
@property(nonatomic,weak)id<passDelegate>pdelegate;
@end

FirstClass.m File

//Apply this code were when you want to set the delegate. In my side, I set it inside the action button.

Note you need to import the SecondClass.h in this class. SecondClass.h is the class were you need to pass the data that you set from this class.

- (IBAction)btn_preview:(id)sender {
    

        SecondClass *sc = [self.storyboard instantiateViewControllerWithIdentifier:@"ss"];
        sc.hidesBottomBarWhenPushed = YES;
        sc.title = @"Preview";
        [self.navigationController pushViewController:sc animated:YES];
        
        self.pdelegate = sc;
        [self.pdelegate didPass:self nname:_senderName.text rname:_receiverName.text amount:_tf_amount.text charge:_lbl_charge.text total:_lbl_total.text];
    
}


SecondClass.h

#import <UIKit/UIKit.h>
#import "FirstClass.h"

@interface SecondClass : UIViewController<passDelegate>

@end


SecondClass.m

Note: Declare assigning variables at the top under implementation

- (void)didPass:(FirstClass *)controller nname:(NSString *)nname rname:(NSString *)rname amount:(NSString *)amount charge:(NSString *)charge total:(NSString *)total{
    
    
    get_sname = nname;
    get_rname = rname;
    get_amount = amount;
    get_charge = charge;
    get_total = total;
}


Web Hosting

That's it.

No comments:

Post a Comment