iOS   发布时间:2022-03-30  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios – 从其他ViewController调用didSelectRowAtIndexPath而不响应iPad大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我想从detailViewController上的按钮更改表中的选定行(在MasterViewController中).
我知道我不能调用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);
}

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特定的.

主版/明细模板的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,就像我通过触摸选择了行一样.

大佬总结

以上是大佬教程为你收集整理的ios – 从其他ViewController调用didSelectRowAtIndexPath而不响应iPad全部内容,希望文章能够帮你解决ios – 从其他ViewController调用didSelectRowAtIndexPath而不响应iPad所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: