Ios 从其他ViewController调用DidSelectRowatingIndexPath没有响应
我想从detailViewController上的按钮更改表中的选定行(在Ios 从其他ViewController调用DidSelectRowatingIndexPath没有响应,ios,uitableview,ipad,Ios,Uitableview,Ipad,我想从detailViewController上的按钮更改表中的选定行(在MasterViewController中)。 我知道我不能调用didSelectRowAtIndexPath,因为它是一个委托方法。我尝试使用selectRowAtIndexPath,但这不会调用didSelectRowAtIndexPath或willSelectRowAtIndexPath方法。正如这里所说, 可以使用一些新方法,然后从另一个ViewController(detailViewController)调用该
MasterViewController
中)。
我知道我不能调用didSelectRowAtIndexPath
,因为它是一个委托方法。我尝试使用selectRowAtIndexPath
,但这不会调用didSelectRowAtIndexPath
或willSelectRowAtIndexPath
方法。正如这里所说,
可以使用一些新方法,然后从另一个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]];
}
- (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);
}
-(IBAction)test{
//MasterViewController *mVc = [[MasterViewController alloc] init];
[self.mVc pushDetailViewController:[NSIndexPath indexPathForRow:0 inSection:0]];
}
有人能给我举个例子来解决这个问题吗?谢谢
编辑:这是新代码
MasterViewController.m
-(IBAction)nextItem
{
MasterViewController *mVc = [[MasterViewController alloc] init];
[mVc pushDetailViewController:[NSIndexPath indexPathForRow:0 inSection:0]];
}
- (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);
}
-(IBAction)test{
//MasterViewController *mVc = [[MasterViewController alloc] init];
[self.mVc pushDetailViewController:[NSIndexPath indexPathForRow:0 inSection:0]];
}
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;
它不调用方法的NSLog部分,也不选择
DetailViewController.h中的行…,向指向MasterViewController的对象添加属性。您还需要导入MasterViewController.h,例如
#导入“MasterViewController”
@属性(非原子,保留)MasterViewController*mVc
不要忘记在DetailViewController.m
中合成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进行对话。根据您的问题,此答案是特定于iPad的
iPad版的主/细节模板使用了一个UISplitViewController
2,可以在屏幕上看到时分割屏幕。此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];
当我将此代码放入详细视图控制器中的iAction
方法时,链接按钮执行适当的操作,既选择行,又按下VC,就像我通过触摸选择行一样。嗨,Walter!谢谢你的回复!我尝试了你的方法,现在有了上面的新代码(请参阅我问题的编辑部分),但它仍然不起作用。哦,我的上帝!非常感谢你!!!这真的很有效!经过一周的折磨。。。非常感谢。现在我可以继续了。完美的这是什么?[self.splitViewController.viewControllers objectAtIndex:0];请explain@java当然self
是一个视图控制器。视图控制器具有拆分视图控制器
的属性(该属性控制左窗格和右窗格)。splitViewController
的viewControllers
数组包含占用其窗格的两个视图控制器。0索引中的视图控制器将是左侧的视图控制器或主视图控制器。