2012-01-21 25 views
4

我想要從detailViewController上的按鈕更改表中選定的行(在MasterViewController)。 我知道我不能撥打didSelectRowAtIndexPath,因爲它是委託方式。我嘗試使用selectRowAtIndexPath,但這不會調用didSelectRowAtIndexPathwillSelectRowAtIndexPath方法。正如這裏所提出的, 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並且不選擇行...

回答

12

這個答案是根據你的問題具體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,我彷彿通過觸摸選擇的行。

+0

哦,我的上帝!謝謝sooo多!這真的很有用!經過一週的折磨......謝謝!現在我可以繼續。完善! – iPhoneNoob

+0

這是什麼? [self.splitViewController.viewControllers objectAtIndex:0];請解釋一下 – java

+0

@java當然。 'self'是一個視圖控制器。視圖控制器有一個'splitViewController'屬性(控制左右窗格)。 'splitViewController'的'viewControllers'數組包含佔據其窗格的兩個視圖控制器。 0索引中的視圖控制器將是左視圖控制器或主視圖控制器上的視圖控制器。 – NJones

3

DetailViewController.h屬性添加到指向MasterViewController的對象。您還需要導入MasterViewController.h所以像

#進口 「MasterViewController」

...

@屬性(非原子,保留)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對象,並且每次使用它時都會創建一個新的對象。您只需要獲取原始MasterViewController的句柄。
另一種方法是探索使用parent方法來引用MasterViewController。但我想告訴你如何用明確的屬性來做到這一點,因爲我認爲它會更清晰。

此外,這可能會或可能不會解決所有問題,但它至少會告訴您如何與實際的MasterViewController進行對話。

+0

嗨,沃爾特!感謝您的答覆!我試過你的方法,現在有了上面的新代碼(請參閱我的問題的編輯部分),但它仍然沒有工作。 – iPhoneNoob

相關問題