调用其他ViewController没有响应iPad的didSelectRowAtIndexPath

我想从detailViewController上的一个button中更改表( MasterViewController )的选定行。 我知道我不能调用didSelectRowAtIndexPath因为它是一个委托方法。 我试过使用selectRowAtIndexPath但是不会调用didSelectRowAtIndexPathwillSelectRowAtIndexPath方法。 正如在这里build议的, https: //stackoverflow.com/a/7496926/1050722它是可能的一些新的方法,然后从另一个ViewController (detailViewController)调用该方法,但该方法应该如何?

这里是一些代码:

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

有人能给我一个例子来解决这个问题吗? 谢谢。

编辑 :这是新的代码

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

它不调用方法的NSLog部分,它不select行…

这个答案是根据你的问题具体的iPad。

Master / Detail模板的iPad版本使用一个UISplitViewController ,当你看到它的时候,会分裂两个屏幕。 这个UISplitViewController实例保持对你的两个导航控制器的引用。 其中保持对最高层视图控制器的引用。 这里是示例代码, 不要逐字使用,除非testing,因为它不包含错误检查

 // 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's didFinishLaunching: method UINavigationController *masterController = [self.splitViewController.viewControllers objectAtIndex:0]; // Next we want a reference to the topmost viewController again you shouldn't assume it's 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's delegate [tableView.delegate tableView:tableView didSelectRowAtIndexPath:path]; 

当我把这段代码放在详细视图控制器的IBAction方法中时,链接的button执行相应的操作,都select该行并按下一个VC,就好像我已经通过触摸select了该行。

DetailViewController.h添加一个属性到指向MasterViewController的对象。 您还需要导入MasterViewController.h,如此

#import“MasterViewController”

@property(nonatomic,retain)MasterViewController * mVc;

不要忘记在DetailViewController.m@synthesize mVc;

现在在你的pushDetailViewController添加一个引用来join这两个对象

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

然后在DetailViewController中引用该对象

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

我想你遇到的另一个问题是每次单击nextItembutton时分配一个新版本的mVC。 IOS会很高兴地让你做大量的MasterViewController对象,并在每次分配时创build一个新对象。 您只需要获取原始MasterViewController的句柄。
另一种方法是探索使用parent方法来引用MasterViewController。 但我想告诉你如何做到这一点明确的财产,因为我认为它会更清楚。

此外,这可能会或可能不会解决所有问题,但希望至less可以告诉您如何与实际的MasterViewController进行对话。