faisal60
faisal60

Reputation: 87

Custom Delegate Callback

I'm new to iOS programming and I'm facing a problem I'm having a problem with custom delegate. I'm trying to make a simple custom where it return data to the previous view controller and pop the current view controller.

I have 2 navigation view controller

1 - main view controller 2 - Adding

and here is the protocol that is written in the adding view controller

@protocol AddingDelegate <NSObject>

@required
-(void)setInformation:(Adding *)controller withObject:(Conference *)info;

and here is the where I called it in adding view controller

-(IBAction)addingConference
{
    NSLog(@"Adding Button Pressed");
    conferenceObject = [[Conference alloc]init];
    conferenceObject.name = [NameTX text];
    conferenceObject.city = [CityTX text];
    conferenceObject.description = [Dectription text];
    NSMutableArray *info = [[NSMutableArray alloc] init];
    [info addObject:conferenceObject];
    [self.delegate setInformation:self withObject:conferenceObject];
    NSLog(@"adding Conference method is done");
}

I wrote the delegate at the interface in the main view controller

@interface MainViewController : UITableViewController <AddingDelegate>

@end

and here where I declared the delegate method

-(void)setInformation:(Adding *)controller withArray:(NSMutableArray *)info
{
    NSLog(@"in the main view at the delegate");
    [self.navigationController popToRootViewControllerAnimated:YES];
    NSLog(@"Should be popped right now");
}

and this is the prepare for segue method

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"AddObject"]) {
        UINavigationController *navigation = segue.destinationViewController;
        Adding *addingViewController = [[navigation viewControllers]objectAtIndex:0];
        addingViewController.delegate = self;
    }
}

now the problem is when I push the adding on top of the stack and then fill the information and press done the adding view controller doesn't pop to show main view controller.

I tried to log everything and the logs from the main view controller doesn't show .

Please help me

Upvotes: 1

Views: 534

Answers (2)

bhavya kothari
bhavya kothari

Reputation: 7474

In Adding.m

  @class Adding;

@protocol AddingDelegate
- (void)flipsideViewControllerDidFinish:(FlipsideViewController *)controller;
@end

@interface Adding : UIViewController

 @property (weak, nonatomic) id <AddingDelegate> delegate; // have you forgot this one


 @end

and use

 [self dismissViewControllerAnimated:YES completion:nil];

You need dismiss if you want get back to previous screen and make sure you have added Navigation controller Your storyboard should look like this

Upvotes: 1

Gianluca Tranchedone
Gianluca Tranchedone

Reputation: 3598

What I notice here is that in the implementation of prepareForSegue:sender: the segue's destinationViewController is a navigation controller. This makes me think that your segue is not pushing the AddingController on the current navigation stack but it's presenting a new one instead. This means the new navigation controller containing the AddingController is presented modally and as such, when you try to pop the navigation stack nothing seems to happen because you're operating on the wrong navigation stack. If that is the case you have two options: 1. change [self.navigationController popToRootViewControllerAnimated:YES]; for [self dismissViewControllerAnimated:YES completion:nil]; or 2. change the segue to be a push segue instead of a modal segue and point the segue directly to the AddingController.

Upvotes: 1

Related Questions