Ilya Chikmarev Ilya Chikmarev - 2 months ago 19
iOS Question

Delegate form UIView to UIViewController

I have UIView, what called Popup and poping from UIViewContorller(ParentVC)
On UIView I have 4 buttons. When buttons is pressed, it needs to open new Controllers from(ParentVC). I am using Delegate, were is my mistake?

//Popup.h
@protocol PopupDelegate
@required

- (IBAction)stepOfRestoration:(id)sender;
- (IBAction)clientCall:(id)sender;
- (IBAction)readyTo:(id)sender;
- (IBAction)givePhone:(id)sender;

@end

@interface Popup : PSCustomViewFromXib

@property (nonatomic, assign) id <PopupDelegate> delegate;
@property (strong, nonatomic) IBOutlet UIView *view;

- (IBAction)stepOfRestoration:(id)sender;
- (IBAction)clientCall:(id)sender;
- (IBAction)readyTo:(id)sender;
- (IBAction)givePhone:(id)sender;


In .m i have this:

@synthesize delegate;
....
- (IBAction)stepOfRestoration:(id)sender {
[self.delegate buttonPressed];
}


And this is Parent .m

...
CGRect rect = CGRectMake(0,0,200,300);
Popup *popup1 = [[Popup alloc] initWithFrame:rect];
popup1.delegate = self;
....

-(void)buttonPressed {
[self performSegueWithIdentifier:@"infoSegue" sender:nil];
}


So were is my mistake?

Answer

You don't have a method called buttonPressed in your protocol, you need to call a method in your protocol, e.g.

Popup.m
- (IBAction)buttonPressed:(id)sender {
   [self.delegate stepOfRestoration:sender];
}

Parent.m
- (IBAction)stepOfRestoration:(id)sender {
   // some code
}

Link to long winded but hopefully helpful tutorial, good luck.