从其他ViewController调用didSelectRowAtIndexPath不响应iPad [英] calling didSelectRowAtIndexPath from other ViewController not responding iPad

查看:90
本文介绍了从其他ViewController调用didSelectRowAtIndexPath不响应iPad的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从detailViewController上的按钮更改表中的选定行(在 MasterViewController 中)。
我知道我不能调用 didSelectRowAtIndexPath 因为它是委托方法。我尝试使用 selectRowAtIndexPath ,但这不会调用 didSelectRowAtIndexPath ,也不会调用 willSelectRowAtIndexPath 方法。正如在这里建议的那样,对某些人来说,可能会有一些信息。新方法,然后从其他 ViewController (detailViewController)调用该方法,但该方法应该如何?

I want to change the selected row in the table (in MasterViewController) from a button on a detailViewController. I know that I cannot call didSelectRowAtIndexPath because it is a delegate method. I tried using selectRowAtIndexPath but that does not invoke the didSelectRowAtIndexPath nor the willSelectRowAtIndexPath method. As suggsted here, https://stackoverflow.com/a/7496926/1050722 it is possible to some new method and then call that method from the other ViewController (detailViewController), but how should that method look like?

以下是一些代码:

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.

编辑:这是新代码

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部分,也不会选择行...

It does not call the NSLog part of the method and it is not selecting the row...

推荐答案

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

This answer is iPad specific as per your question.

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

The iPad version of the Master/Detail template uses a UISplitViewController two split the screen as you see it. This UISplitViewController instance keeps references to both of your navigations controllers. Which keep reference to your topmost view controllers. Here is sample code, do not use it verbatim except to test since it contains no 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'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 时详细视图控制器中的方法链接按钮执行适当的操作,两者都选择行并按下VC,就像我通过触摸选择了行一样。

When I put this code in an IBAction method in the detail view controller the linked button performs the appropriate actions, both selects the row and pushes a VC, as if I had selected the row by touch.

这篇关于从其他ViewController调用didSelectRowAtIndexPath不响应iPad的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆