Transferring data between view controllers using protocol and delegation

I am trying to press string text to view controller 1 from my view manager 2 using protocols and delegate. I'm new to this data transfer method, so forgive me if I seem kind of ignorant. The color of the string always returns null. I will post the code that I still have, and if that helps, im using the navigation controller and using the navigation button go from ViewController 2 to ViewController 1.

Viewcontroller 2

.h

@protocol PassString <NSObject> @required - (void) setSecondFavoriteColor:(NSString *)string; @end @interface ViewController2 : UIViewController{ UIButton *button; NSString *ee id <PassString> delegate; } @property (retain) id delegate; 

ViewController 2

.m

 @synthesize delegate; -(void)button{ ee = @"Blue Color"; [[self delegate] setSecondFavoriteColor:ee]; 

ViewController 1.h

 @interface ViewController1 : UIViewController <PassString>{ NSString*color; } @property (strong,nonatomic) NSString *color 

ViewController 1.m

 - (void)setSecondFavoriteColor:(NSString *)string { color = string; NSLog(@"%@",color); } 
+4
source share
1 answer

Just a couple of things that I noticed in your code, your property should contain the specified protocol:

 @property (retain) id <PassString> delegate; 

And at some point in the class that implements the delegate methods, you must assign a delegate to view controller 1. For example:

 [viewController2Instance setPassingDelegate:self]; 
+2
source

Source: https://habr.com/ru/post/1500050/


All Articles