从其他ViewController调用didSelectRowAtIndexPath不响应iPad

iPh*_*oob 4 uitableview ipad ios

我想MasterViewController从detailViewController上的按钮更改表(in )中的选定行.我知道我不能打电话,didSelectRowAtIndexPath因为它是委托方法.我尝试过使用selectRowAtIndexPath但不会调用didSelectRowAtIndexPath也不会调用willSelectRowAtIndexPath方法.正如这里提到的,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);
}
Run Code Online (Sandbox Code Playgroud)

DetailViewController.m

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

有人能举个例子来解决这个问题吗?谢谢.

编辑:这是新代码

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);
}
Run Code Online (Sandbox Code Playgroud)

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;
Run Code Online (Sandbox Code Playgroud)

DetailViewCOntroller.m

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

它不会调用方法的NSLog部分,也不会选择行...

NJo*_*nes 12

根据您的问题,这个答案是iPad特定的.

UISplitViewController屏幕/详细信息模板的iPad版本使用两个分割屏幕,如您所见.此UISplitViewController实例保留对两个导航控制器的引用.其中保留了对最顶层视图控制器的引用.这是示例代码,除了测试之外不要逐字使用,因为它不包含错误检查.

// 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];
Run Code Online (Sandbox Code Playgroud)

当我将此代码放入IBAction详细视图控制器中的方法时,链接按钮执行适当的操作,两者都选择行并推送VC,就像我通过触摸选择了行一样.