How to put viewController and then click viewController through delegation

I have a UITableViewController which, when a cell is clicked, I want the controller to pop up itself, and then the controller that it pops up, insert another view controller on the stack.

I call this method because popped-to viewController is a delegate of tableViewController

Currently, I call this method with a delay on it, because otherwise everything becomes littered, waiting for the animation to finish. The implementation of this method seems a bit hacked and it seems to me that it will fail if one of the devices does not pop up at the expected wait time that I gave it.

Here are some of the code:

//**** code in my tableViewController ***// [self.navigationController popViewControllerAnimated:YES]; [self.delegate cellPressedInTableViewControllerWithCalculationsModel:(id)anArgmentMyDelegateMethodTakes]; // **** Code in the viewController being popped to ****// //CalculationsViewController is a subclass of UIViewController CalculationsViewController *calcViewController = [[CalculationsViewController alloc] init]; //some customization code would go her [self.navigationController performSelector:@selector(pushViewController:animated:) withObject:calcViewController afterDelay:0.75]; //this seems like the arbitrary part, the 0.75 second delay. [calcViewController release]; 

There seems to be a better way to pop / skip delegation, which will be performed after the animation finishes. It seems to me that waiting time seems to be the cause of unexpected problems.

I also tried using:

  performSelectorOnMainThread:withObject:waitUntilDone 

But the code just executes immediately, and the view hierarchy is screwed up.

I also looked at this question: The issue of delegation and it got me so far, but I'm curious to find out if there is a better way to accomplish such a task, Thank you.

edit: I also tried to wrap the method in an instance of NSInvocation, and I could not get it to coordinate the method call until the animation ended without arbitrarily setting a delay

+8
ios objective-c iphone
source share
5 answers

You must use the flag to overcome this situation. You set this flag in viewWillDisappear mode of the view controller that is exposed. When this flag is set, then you can push another view controller on the stack. Hope this is understandable.

+4
source share

The cleanest way to do more than a click or pop of a view controller is to set up an array of UINavigationController controllers.

For example, for pop, and then click on a view controller:

 MyTableViewController *vc = [[MyTableViewController alloc] init]; NSMutableArray *controllers = [NSMutableArray arrayWithArray:self.navigationController.viewControllers]; [controllers removeLastObject]; [controllers addObject:vc]; [self.navigationController setViewControllers:controllers animated:YES]; 
+37
source share

How about when you reject your UIViewController containing the table you send NSNotifcation to your viewDidDisappear method like this:

 - (void)viewDidDisappear:(BOOL)animated { [[NSNotificationCenter defaultCenter] postNotificationName:@"loadOtherVC" object:nil]; } 

And in the parent view controller, which will push the new view controller, you add an observer for this notification like this:

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

You must have a method matching the selector.

 - (void) LoadOtherVC:(NSNotification *) notification { // load your other view controller you want here } 
+3
source share

Do not remove it from the top-level controller. Call the delegate method, which will display the view from the navigation controller, and then click new.

0
source share

I liked the Alexandre solution, but if I were a delegate, I would not want to use a notification. Therefore, in this case, we can simply use the delegate in the viewDidDisappear method.

So, in the didSelectRowAtIndexPath method didSelectRowAtIndexPath you can set the controller -

 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ [self.navigationController popViewControllerAnimated:YES]; } 

and in the same view controller, the ViewController that you are calling, call the delegate method in the viewDidDisappear method, for example -

 -(void)viewDidDisappear:(BOOL)animated{ [self.delegate cellPressedInTableViewControllerWithCalculationsModel:(id)anArgmentMyDelegateMethodTakes]; } 

Then, in the managed controller, you can implement the delegation method and inside this delegation method, you can do whatever you want.

0
source share

All Articles