Implement delegation methods for transmitting modal view controller data

I have a simple project to present a modal view controller and pass back the string based on which a button was pressed in modal VC. I based all this on viewing the Stanford class on iTunes U. It seems that I have everything right, but I get a couple of compiler warnings.

First, I get one called passing argument 1 of 'setDelegate:' from incompatible pointer type in TransferViewController.m

Second, I get four warnings called Invalid receiver type 'id <MyModalViewControllerDelegate>*' , but they do not appear in the assembly results area, but next to offensive lines in MyModalViewController.m , both lines in each of the button actions.

Here is the code ...

 // TransferViewController.h #import <UIKit/UIKit.h> #import "MyModalViewController.h"; @interface TransferViewController : UIViewController <MyModalViewControllerDelegate> { UILabel *label; UIButton *button; } @property (nonatomic, retain) IBOutlet UILabel *label; @property (nonatomic, retain) UIButton *button; - (IBAction)updateText; @end 

 // TransferViewController.m #import "TransferViewController.h" @implementation TransferViewController @synthesize label; @synthesize button; - (IBAction)updateText { MyModalViewController *myModalViewController = [[MyModalViewController alloc] init]; myModalViewController.delegate = self; // I get the warning here. [self presentModalViewController:myModalViewController animated:YES]; [myModalViewController release]; } - (void)myModalViewController:(MyModalViewController *)controller didFinishSelecting:(NSString *)selectedDog { label.text = selectedDog; [self dismissModalViewControllerAnimated:YES]; } @end 

 // MyModalViewController.h #import <UIKit/UIKit.h> @protocol MyModalViewControllerDelegate; @interface MyModalViewController : UIViewController { UIButton *abby; UIButton *zion; id <MyModalViewControllerDelegate> delegate; } @property (assign) id <MyModalViewControllerDelegate> delegate; - (IBAction)selectedAbby; - (IBAction)selectedZion; @end @protocol MyModalViewControllerDelegate <NSObject> @optional - (void)myModalViewController:(MyModalViewController *)controller didFinishSelecting:(NSString *)selectedDog; @end 

 // MyModalViewController.m #import "MyModalViewController.h" @implementation MyModalViewController @synthesize delegate; - (IBAction)selectedAbby { if ([self.delegate respondsToSelector:@selector (myModalViewController:didFinishSelecting:)]) { [self.delegate myModalViewController:self didFinishSelecting:@"Abby"]; } } - (IBAction)selectedZion { if ([self.delegate respondsToSelector:@selector (myModalViewController:didFinishSelecting:)]) { [self.delegate myModalViewController:self didFinishSelecting:@"Zion"]; } } 
+4
source share
1 answer

Get rid of these * after id <something> and before delegate .

So do it

 id <MyModalViewControllerDelegate> *delegate; 

this is

 id <MyModalViewControllerDelegate> delegate; 
+4
source

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


All Articles