0

i've been advised to pass data using delegation, but it does not seem to show any log message of the string. i'm passing data from the frontViewController to the backViewController.

frontViewController.h

@protocol passIdDelegate <NSObject>

-(void)idPassed:(NSString*)idString;

@end



@interface FrontViewController : UIViewController {

    id<passIdDelegate> delegate;
}

@property(nonatomic,assign)id delegate;

@end

frontViewController.m passing data in didSelectRow

[delegate idPassed:[[homesDic objectAtIndex:indexPath.row] objectForKey:@"idString"]];

[self performSegueWithIdentifier:@"showHome" sender:self];

backViewController.h

@interface HomeProfileViewController : UIViewController <passIdDelegate>{

}

@end

backViewControlller.m

-(void)idPassed:(NSString*)idString{
    NSLog(@"%@", idString);
}
1
  • 2
    Where are you setting your delegate?
    – shabbirv
    Aug 18, 2014 at 22:05

3 Answers 3

1

In this case, where you are trying to pass data to a new view controller that is being presented, delegation isn't the correct pattern. All you need is a property on your destination view controller to receive the data.

BackViewController.h

@interface HomeProfileViewController : UIViewController{

@property (strong,nonatomic) NSString *idString;

}

@end

frontViewController.m passing data in didSelectRow

NSString *idString =  [[homesDic objectAtIndex:indexPath.row] objectForKey:@"idString"]];

[self performSegueWithIdentifier:@"showHome" sender:idString];

In frontViewController.m prepareForSegue

-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"showHome"]) {
        HomeProfileViewController *hpvc = (HomeProfileViewController *)segue.destinationViewController;
        hpvc.idString=(NSString *)sender;
    }
}
0

In your backviewController.m you need something like frontViewController.delegate = self;

4
  • i've added FrontViewController *frontVC = [[FrontViewController alloc] init]; frontVC.delegate = self;
    – user3926776
    Aug 18, 2014 at 22:35
  • This is not doing anything
    – user3926776
    Aug 18, 2014 at 22:35
  • You need to learn about the difference between classes and instances. You are creating a new instance of your front view controller, not referencing the existing one. In this situation I don't think delegation is what you need. You simply need to set a property on your destination view controller in prepareForSegue
    – Paulw11
    Aug 18, 2014 at 22:40
  • Ok, but be sure that this instance of FrontViewContrlller is showing. Just setting some FrontViewController to the delegate is not working. Aug 18, 2014 at 22:40
0

To pass data back from ViewControllerB to ViewControllerA you need to use Protocols and Delegates or Blocks, the latter can be used as a loosely coupled mechanism for callbacks.

To do this we will make ViewControllerA a delegate of ViewControllerB. This allows ViewControllerB to send a message back to ViewControllerA enabling us to send data back.

For ViewControllerA to be delegate of ViewControllerB it must conform to ViewControllerB's protocol which we have to specify. This tells ViewControllerA which methods it must implement.

In ViewControllerB.h, below the #import, but above @interface you specify the protocol.

@class ViewControllerB;

@protocol ViewControllerBDelegate <NSObject>
- (void)addItemViewController:(ViewControllerB *)controller didFinishEnteringItem:(NSString *)item;
@end

next still in the ViewControllerB.h you need to setup a delegate property and synthesize in ViewControllerB.m

@property (nonatomic, weak) id <ViewControllerBDelegate> delegate;

In ViewControllerB we call a message on the delegate when we pop the view controller.

NSString *itemToPassBack = @"Pass this value back to ViewControllerA";
[self.delegate addItemViewController:self didFinishEnteringItem:itemToPassBack];

That's it for ViewControllerB. Now in ViewControllerA.h, tell ViewControllerA to import ViewControllerB and conform to its protocol.

#import "ViewControllerB.h"

@interface ViewControllerA : UIViewController <ViewControllerBDelegate>

In ViewControllerA.m implement the following method from our protocol

- (void)addItemViewController:(ViewControllerB *)controller didFinishEnteringItem:(NSString *)item
{
    NSLog(@"This was returned from ViewControllerB %@",item);
}

The last thing we need to do is tell ViewControllerB that ViewControllerA is its delegate before we push ViewControllerB on to nav stack.

ViewControllerB *viewControllerB = [[ViewControllerB alloc] initWithNib:@"ViewControllerB" bundle:nil];
viewControllerB.delegate = self
[[self navigationController] pushViewController:viewControllerB animated:YES];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.