我想要從detailViewController上的按鈕更改表中選定的行(在MasterViewController
)。 我知道我不能撥打didSelectRowAtIndexPath
,因爲它是委託方式。我嘗試使用selectRowAtIndexPath
,但這不會調用didSelectRowAtIndexPath
或willSelectRowAtIndexPath
方法。正如這裏所提出的, https://stackoverflow.com/a/7496926/1050722它可能是一些新的方法,然後從另一個ViewController
(detailViewController)調用該方法,但該方法應該如何?調用didSelectRowAtIndexPath從其他ViewController沒有響應iPad
下面是一些代碼:
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並且不選擇行...
哦,我的上帝!謝謝sooo多!這真的很有用!經過一週的折磨......謝謝!現在我可以繼續。完善! – iPhoneNoob
這是什麼? [self.splitViewController.viewControllers objectAtIndex:0];請解釋一下 – java
@java當然。 'self'是一個視圖控制器。視圖控制器有一個'splitViewController'屬性(控制左右窗格)。 'splitViewController'的'viewControllers'數組包含佔據其窗格的兩個視圖控制器。 0索引中的視圖控制器將是左視圖控制器或主視圖控制器上的視圖控制器。 – NJones