gpt4 book ai didi

ios - 从不响应 iPad 的其他 ViewController 调用 didSelectRowAtIndexPath

转载 作者:可可西里 更新时间:2023-11-01 05:02:33 25 4
gpt4 key购买 nike

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

最佳答案

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

Master/Detail 模板的 iPad 版本使用 UISplitViewController 将屏幕拆分为您所看到的两个部分。此 UISplitViewController 实例保留对两个导航 Controller 的引用。它保留对最顶层 View Controller 的引用。这是示例代码,不要逐字使用它,除非用于测试,因为它不包含错误检查

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

当我将此代码放入详细 View Controller 的 IBAction 方法中时,链接按钮会执行适当的操作,同时选择该行并按下 VC,就好像我通过触摸选择了该行一样.

关于ios - 从不响应 iPad 的其他 ViewController 调用 didSelectRowAtIndexPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8953749/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com