質問

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];

}
役に立ちましたか?

解決 2

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];

} 

他のヒント

Try this:-

In 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{
self.tableView.delegate=self;
self.tableView.dataSource=self;
[self.tableView reloadData];
self.title=@"From back";
}

On SecondViewController:

- (IBAction)ButtonClick:(id)sender {
[[NSNotificationCenter defaultCenter] postNotificationName:@"tableReload" object:nil];
[self dismissViewControllerAnimated:YES completion:nil];
} 

@Natarajan - It's just a convention. Like all conventions they only serve to make code easier to read and maintain, for others people who will work with your code,Also .NET adopted ToString() as a convention. So it doesn't really matters how you name your methods...A lot of conventions say you capitalise the first letter of types (classes, structs, enums, etc.), and use lowercase otherwise (functions, members, etc.).But this wasn't the question asked .

The question for @Krunal will be where do you call MethodCall, and are you sure you change data in your table before you reload them? you can check using breakpoints if the table is reloaded if you say that NSLog is called that means the [tblView reloadData]; is called but it has nothing to reload

I had exactly the same idea as @Ricky and I have the same problem from a Modal window the notification where never called (so I searched the web for a solution) so you have to wait for the animation to complete so I have reach a solution and I'm going to add the solution to his code.

As he mentioned you have to add the observer in the viewDidAppear and remove it in the viewWillDisappear and in your second viewcontroller you have to add this code:

- (IBAction)ButtonClick:(id)sender {
    [self dismissViewControllerAnimated:YES completion:^(void){
        [[NSNotificationCenter defaultCenter] postNotificationName:@"tableReload" object:nil];
    }];
}

Doing this, it will call viewDidAppear in the first viewcontroller and then postNotificationName so the method tableReload will get called.

Regards and happy coding.

  1. You can use NSNotificationCenter for your problem.

Define a NSNotification in your MasterViewController viewDidLoad like below

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(closeModal:) name:@"CloseModal" object:nil];

And then define the method as below

(void)closeModal:(NSNotification *)notification{
    UIViewController *controller=(UIViewController *)notification.object;

    [controller dismissViewControllerAnimated:YES completion:^ {
         [self getPostData];                 // Reload Data
         [self.tableView reloadData];        // Reload Data
    }];

}

And at last from your other controller from where you are actually trying to dismiss your controller use code below

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top