Call to didSelectRowAtIndexPath from another ViewController does not answer iPad

I want to change the selected row in a table (in MasterViewController ) using a button on detailViewController. I know that I cannot call didSelectRowAtIndexPath because it is a delegate method. I tried using selectRowAtIndexPath , but that does not call the didSelectRowAtIndexPath or willSelectRowAtIndexPath . As suggested here, https://stackoverflow.com/a/166268/ can use a new method and then call this method from another ViewController (detailViewController), but what should this method look like?

Here is the code:

MasterViewController.m

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [self pushDetailViewController:indexPath]; } -(void)pushDetailViewController:(NSIndexPath *)indexPath { if (!self.detailViewController) { self.detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil]; } [self.navigationController pushViewController:self.detailViewController animated:YES]; NSLog(@"pushDetailviewC and index %@", indexPath); } 

DetailViewController.m

 -(IBAction)nextItem { MasterViewController *mVc = [[MasterViewController alloc] init]; [mVc pushDetailViewController:[NSIndexPath indexPathForRow:0 inSection:0]]; } 

Can someone give me an example to fix this? Thanks.

EDIT : here is the new code

MasterViewController.m

  - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [self pushDetailViewController:indexPath]; } -(void)pushDetailViewController:(NSIndexPath *)indexPath{ if (!self.detailViewController) { self.detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil]; } self.detailViewController.mVc = self; [self.navigationController pushViewController:self.detailViewController animated:YES]; NSLog(@"pushDetailviewC and index %@", indexPath); } 

DetailViewController.h

 #import <UIKit/UIKit.h> #import "MasterViewController.h" @interface DetailViewController : UIViewController <UISplitViewControllerDelegate, UITableViewDelegate> @property (strong, nonatomic) id detailItem; @property (strong, nonatomic) IBOutlet UILabel *detailDescriptionLabel; @property (nonatomic, retain) MasterViewController *mVc; 

DetailViewCOntroller.m

 -(IBAction)test{ //MasterViewController *mVc = [[MasterViewController alloc] init]; [self.mVc pushDetailViewController:[NSIndexPath indexPathForRow:0 inSection:0]]; } 

It does not call part of the NSLog method and does not select a string ...

+4
source share
2 answers

This answer is iPad specific for your question.

The IP version of the Master / Detail template uses the two UISplitViewController that you see on the screen. This instance of UISplitViewController stores links to both of your navigation controllers. Which contains a link to your top controllers. Here is a sample code, do not use it verbatim, except for verification, since it does not contain error checking .

 // Picking some arbitrary row NSUInteger desiredRow = 3;//??? // The Master navigation controller is at index zero in the template, you can check in your app delegate didFinishLaunching: method UINavigationController *masterController = [self.splitViewController.viewControllers objectAtIndex:0]; // Next we want a reference to the topmost viewController again you shouldn't assume it a UITableViewController UITableViewController *tableController = (UITableViewController *)masterController.visibleViewController; // Next we get the reference to the tableview in-question UITableView *tableView = tableController.tableView; // We translate our desired row to an index path NSIndexPath *path = [NSIndexPath indexPathForRow:desiredRow inSection:0]; // We select the row , again this will not call didSelectRowAtIndexPath: [tableView selectRowAtIndexPath:path animated:YES scrollPosition:UITableViewScrollPositionNone]; // We call didSelectRowAtIndexPath: on the tableview delegate [tableView.delegate tableView:tableView didSelectRowAtIndexPath:path]; 

When I put this code in the IBAction method in the detail view controller, the associated button performs the appropriate actions, both select the row and push VC, as if I select the row by pressing.

+12
source

In DetailViewController.h add a property to the object pointing to MasterViewController. You will also need to import MasterViewController.h, something like

#import "MasterViewController"

...

@property (non-atomic, save) MasterViewController * mVc;

...

Remember in DetailViewController.m to @synthesize mVc;

Now in pushDetailViewController you add a link to the union of two objects

 if (!self.detailViewController) { self.detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil]; } [self.detailViewController.mVc = self]; // New line [self.navigationController pushViewController:self.detailViewController animated:YES]; 

Then in the DetailViewController you reference the object

 -(IBAction)nextItem{ [self.mVc pushDetailViewController:[NSIndexPath indexPathForRow:0 inSection:0]]; } 

I think that another problem you encountered was distributing a new version of mVC every time you click the nextItem button. IOS will happily allow you to create many MasterViewController objects and create a new one each time you alloc it. You just wanted to get the handle of your original MasterViewController.
Another approach would be to study the parent methods to reference the MasterViewController. But I wanted to show you how to do this with an explicit property, as I thought it would be clearer.

Also, this may or may not fix all your problems, but hopefully at least will show you how to access the actual MasterViewController.

+3
source

Source: https://habr.com/ru/post/1392208/


All Articles