在拆分视图控制器:调用详细视图控制器方法从主视图控制器
问题描述:
在我的MasterViewController
uitabelview
没有选择方法更改DetailedViewController
视图。在拆分视图控制器:调用详细视图控制器方法从主视图控制器
我的问题是当我在DetailedViewController
中更改视图时,如果用户未手动保存,则需要保存文本字段值。
我该如何检查MasterViewController
或任何想法来检测主视图做选择方法从DetailedViewController
调用。
在此先感谢。
答
在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
}
我想你应该实现[方案](https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/WorkingwithProtocols/WorkingwithProtocols的.html)。 – Mahesh
你可以给一些代码。 – chimbu
你想从'MasterViewController'的'didSelectRowAtIndexPath'调用一个'DetailedViewController'方法,对吧? – Mahesh