在拆分视图控制器:调用详细视图控制器方法从主视图控制器

问题描述:

在我的MasterViewControlleruitabelview没有选择方法更改DetailedViewController视图。在拆分视图控制器:调用详细视图控制器方法从主视图控制器

我的问题是当我在DetailedViewController中更改视图时,如果用户未手动保存,则需要保存文本字段值。

我该如何检查MasterViewController或任何想法来检测主视图做选择方法从DetailedViewController调用。

在此先感谢。

+0

我想你应该实现[方案](https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/WorkingwithProtocols/WorkingwithProtocols的.html)。 – Mahesh

+0

你可以给一些代码。 – chimbu

+0

你想从'MasterViewController'的'didSelectRowAtIndexPath'调用一个'DetailedViewController'方法,对吧? – Mahesh

MasterViewController.h

@protocol CellSelectionDelegate <NSObject> 

    -(void)rowSelected:(NSIndexPath *)indexPath; 
@end 


@interface MasterViewController : UIViewController <UITableViewDataSource,UITableViewDelegate> 
{ 
} 
@property(nonatomic,strong)IBOutlet UITableView *menuTable; 
@property (nonatomic, weak) id<CellSelectionDelegate> cellDelegate; 

@end 
MasterViewController.m

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if([self.cellDelegate respondsToSelector:@selector(rowSelected:)]) 
    { 
     //sending selected indexPath, you can use indexPath.row in your detail view 
     [self.cellDelegate rowSelected:indexPath]; 
    } 

} 

现在

DetailedViewController.h

#import "MasterViewController.h" 
@interface DetailedViewController : UIViewController<CellSelectionDelegate> 
{ 

} 
@property(nonatomic,strong)MasterViewController *masterView; 
@end 

DetailedViewController.m

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    //Set master page as the delegate. 
    masterView.delegate = self; 

} 

现在宣布以下的委托方法

-(void)rowSelected:(NSIndexPath *)indexPath 
{ 
    NSLog(@"Your selected Row : %d",indexPath.row); 
    //do your work according to selection made in master view. use indexpath.row to identify which option was selected, you can also pass the other data with rowselected: method 
} 
+0

无法找到CellSelectionDelegate.unable的协议声明来实现协议。 – chimbu

+0

我知道这是旧的,但我只是遇到了这个问题,发现我有一个循环依赖在我的主要和详细信息导入(即他们正在导入其他人的标题)。解决这个问题使代表能够正常工作 – Chris