【问题标题】:calling didSelectRowAtIndexPath from other ViewController not responding iPad从没有响应 iPad 的其他 ViewController 调用 didSelectRowAtIndexPath
【发布时间】:2012-01-21 14:31:16
【问题描述】:

我想通过 detailViewController 上的按钮更改表中的选定行(在 MasterViewController 中)。 我知道我不能调用didSelectRowAtIndexPath,因为它是一个委托方法。我尝试使用 selectRowAtIndexPath 但这不会调用 didSelectRowAtIndexPathwillSelectRowAtIndexPath 方法。正如这里所建议的, 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 部分,也没有选择行...

【问题讨论】:

    标签: ios uitableview ipad


    【解决方案1】:

    根据您的问题,此答案是特定于 iPad 的。

    Master/Detail 模板的 iPad 版本使用 UISplitViewController 两个拆分屏幕,如您所见。这个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];
    

    当我将此代码放入详细视图控制器中的 IBAction 方法时,链接按钮会执行适当的操作,既选择行又按下 VC,就好像我通过触摸选择了行一样。

    【讨论】:

    • 天哪!太感谢你了!!!这真的很有效!经过一周的折磨...谢谢!现在我可以继续了。完美!
    • 这是什么? [self.splitViewController.viewControllers objectAtIndex:0];请解释
    • @java 当然。 self 是一个视图控制器。视图控制器具有splitViewController 的属性(它控制左右窗格)。 splitViewControllerviewControllers 数组包含占据其窗格的两个视图控制器。 0 索引中的视图控制器将是左侧的视图控制器或主视图控制器。
    【解决方案2】:

    DetailViewController.h 中为指向MasterViewController 的对象添加一个属性。您还需要导入 MasterViewController.h 之类的东西

    #import "MasterViewController"

    ...

    @property (nonatomic, 保留) MasterViewController *mVc;

    ...

    不要忘记DetailViewController.m@synthesize mVc;

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

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

    我认为您遇到的另一个问题是每次单击 nextItem 按钮时分配新版本的 mVC。 IOS 会很高兴地让您制作大量 MasterViewController 对象,并在您每次alloc 时创建一个新对象。您只想获取原始 MasterViewController 的句柄。
    另一种方法是探索使用parent 方法来引用MasterViewController。但是我想向您展示如何使用显式属性来执行此操作,因为我认为它会更清晰。

    此外,这可能会或可能不会解决您的所有问题,但希望它至少向您展示了如何与实际的 MasterViewController 对话。

    【讨论】:

    • 嗨,沃尔特!谢谢您的答复!我试过你的方法,现在有了上面的新代码(见我问题的编辑部分),但它仍然不起作用。
    猜你喜欢
    • 1970-01-01
    • 2013-10-07
    • 1970-01-01
    • 1970-01-01
    • 2017-06-01
    • 2015-06-05
    • 2013-04-03
    相关资源
    最近更新 更多