0

I have two view controllers. I want to make a thing like that when i press "Back" button in SecondViewController, it go back to "FirstViewController" and also update the text of label of "FirstViewController". I have tried below code, its working fine that when i press back button it goes back to "FirstViewController"but the issue is that its not updating the text of the label of "FirstViewController". When i debug my code, control goes to "startSampleProcess" method and update the label's text but when we go back to "FirstViewController" by "processCompleted" method,the old text is displaying there. (I'm doing this work using delegates) Any help would be appreciated.

FirstViewController.h

#import <UIKit/UIKit.h>
//#import "SampleProtocol.h"
@protocol SampleProtocolDelegate <NSObject>
@required
- (void) processCompleted;
@end
// Protocol Definition ends here
@interface FirstViewController : UIViewController{

    id <SampleProtocolDelegate> _delegate;
    IBOutlet UILabel *myLabel;
}

@property (nonatomic,strong) id delegate;

-(void)startSampleProcess; // Instance method

@end

FirstViewController.m

#import "FirstViewController.h"

@implementation FirstViewController
@synthesize delegate;

-(void)startSampleProcess{
    myLabel.text = @"we are back!!!";
    [NSTimer scheduledTimerWithTimeInterval:3.0 target:self.delegate
                                   selector:@selector(processCompleted) userInfo:nil repeats:NO];
}

- (void)viewDidLoad{
    [super viewDidLoad];
}

@end

SecondViewController.h

#import <UIKit/UIKit.h>
#import "FirstViewController.h"
@interface SecondViewController : UIViewController<SampleProtocolDelegate>{
    FirstViewController *sampleProtocol;
}
-(IBAction)CallBack:(id)sender;
@end

SecondViewController.m

#import "SecondViewController.h"

@implementation SecondViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad{
    [super viewDidLoad]; 
}

-(IBAction)CallBack:(id)sender{

    sampleProtocol = [[FirstViewController alloc]init];
    sampleProtocol.delegate = self;
    [sampleProtocol startSampleProcess];
}

#pragma mark - Sample protocol delegate

-(void)processCompleted{
    [self.navigationController popViewControllerAnimated:TRUE];
}

@end
  • There are many ways how to establish this "communication" between the two controllers. Are you sure you want to use delegation in this case? Why? You need to have a reason,otherwise there are simpler ways and you might instead pursue one of them. – Earl Grey Feb 27 '15 at 19:13
  • see this - http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers?answertab=votes#tab-top – Vineet Choudhary Feb 28 '15 at 04:28
  • I only know delegates so thats why i used it but if any other simpler way that can do my task than please guide me that how i can do my below task. I only want that when i press a button in "SecondViewController" it simply go back to "FirstViewController" and also update the label's text which is in "FirstViewController" . If there is any other simple method than please guide me that how i can do it.a – Mohammad Waqas Feb 28 '15 at 18:59

2 Answers2

0

It looks like the main problem is in your CallBack: method. It is creating a new instance of FirstViewController rather than referencing the existing view controller.

But even then, this feels like the wrong way to do this, as Earl mentioned in the comments.

Normally, you shouldn't need one view controller to reference another. That will result in tight coupling (bad). An easy way would be to use NSNotificationCenter, but there are lots of options here.

picciano
  • 22,341
  • 9
  • 69
  • 82
0

There are many ways that you can update the state of a view controller when a different view controller does something. See my answer on a similar question this week: What is the best way to keep the state of a modal UIViewController?

Community
  • 1
  • 1
Roderic Campbell
  • 719
  • 5
  • 14