Krunal Krunal - 3 months ago 12
iOS Question

Method doesn't refresh data after DismissViewcontroller

I am developing iPhone app, where i got stuck at one point.

The error i am facing is, i have one tableview in

FirstViewController
on click of cell i am presenting
SecondViewController
on
SecondViewController
i have one button, on click of that button i am dismissing my presentViewController and again comes back to
FirstViewController
on
FirstViewController
i am calling one method that method gets called successfully but it doesn't perform inner task of that method, (means i want to reload my tableview it doesn't reloads my tableview, i want to change title text of that page but it doesn't change)

Here is my code snippet:

FirstViewController:


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
SecondViewController *Obj= [[SecondViewController alloc] init];
[self.navigationController presentViewController:Obj animated:YES completion:nil];
}


Code on SecondViewController:


- (IBAction)ButtonClick:(id)sender {

FirstViewController *Obj=[[FirstViewController alloc] init];

[Obj MethodCall];

[self dismissViewControllerAnimated:YES completion:nil];

}


after dismissing SecondViewController, code on FirstViewController


-(void) MethodCall
{
NSLog(@" -- MethodCall Success --");
self.title=@"From back";
[tblView reloadData];
}


My log shows -- MethodCall Success -- but it neither Reloads my tableview nor changes the title of FirstViewController.

Where i am doing mistake ?

Please help and thanks for reading.

NEW EDIT:

code on FirstViewController:


-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tableReload) name:@"tableReload" object:nil];
}

-(void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
[[NSNotificationCenter defaultCenter]removeObserver:self name:@"tableReload" object:nil];
}

-(void)tableReload{
NSLog(@"-- FromSeeMore PushAndShowMerchants -- = %@",FromSeeMore);

[tblList reloadData];

}


code on SecondViewController :


- (IBAction)SeemoreClick:(id)sender {

[[NSNotificationCenter defaultCenter] postNotificationName:@"tableReload" object:nil];

[self dismissViewControllerAnimated:YES completion:nil];

}

Answer

First of all you should follow object creation and methods declaration in Objective-C

SecondViewController *Obj it should be as SecondViewController * obj

-(void) MethodCall it should be as -(void) methodCall

- (IBAction)ButtonClick:(id)sender is should be - (IBAction)ButtonClick:(id)sender

And, Where did you call your method MethodCall in FirstViewController?

Did you try to use Delegate option to send the data to previous view controller?

Example :

In your FirstViewController.h

@protocol FirstControllerDelegate

-(void)dataUpdated:(NSArray *)array;

@end

@interface FirstViewController : UIViewController <FirstControllerDelegate>

@end

In your FirstViewController.m

-(void)dataUpdated:(NSArray *)array
{
    NSLog(@" -- MethodCall Success -- :%@", array);

    self.title=@"From back";

    [tblView reloadData];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    SecondViewController *Obj= [[SecondViewController alloc] init];

    Obj.delegate = self;

    [self.navigationController presentViewController:Obj animated:YES completion:nil];
}

In SecondViewController.h

@property (nonatomic, assign) id <FirstControllerDelegate> delegate;

In SecondViewController.m

- (IBAction)ButtonClick:(id)sender 
{
    FirstViewController *Obj=[[FirstViewController alloc] init];

    [_delegate dataUpdated:[NSArray arrayWithObject:@"Object Passed"]];

    [self dismissViewControllerAnimated:YES completion:nil];

} 
Comments