Thursday 19 December 2013

Protocol simple example

ViewController.h


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

@interface ip20ViewController : UIViewController <MyVCDelegate>
{
    MyVC *myvc;
}


@end

ViewController.m


- (void)clickPressed:(NSString *)message
{
    NSLog(@"%@", message);

    [myvc dismissViewControllerAnimated:YES completion:nil];
}
- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    myvc = [[MyVC alloc] init];
    myvc.delegate = self;
    [self presentViewController:myvc animated:YES completion:nil];
}

MyVC.h

 

#import <UIKit/UIKit.h>

@protocol MyVCDelegate <NSObject>

@optional

- (void)clickPressed:(NSString *)message;

@end

@interface MyVC : UIViewController

@property (nonatomic, strong) id<MyVCDelegate> delegate;

- (IBAction)click:(id)sender;

@end

MyVC.m


@synthesize delegate;
- (IBAction)click:(id)sender
{
    [delegate clickPressed:@"Just Clicked!"];
}

No comments:

Post a Comment

Comment