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

时间:2015-10-06 06:01:59

标签: ios objective-c uisplitviewcontroller

在我的MasterViewController uitabelview中,选择方法会更改DetailedViewController视图。

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

我如何从MasterViewController检查此内容,或者检测主视图的任何想法都选择了从DetailedViewController调用的方法。

提前感谢。

1 个答案:

答案 0 :(得分:1)

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
}