【发布时间】:2015-11-11 02:26:36
【问题描述】:
我想将数据从详细视图控制器传递到 iPad 的 UISplitViewController 的主视图控制器。在情节提要中的 splitView 之前,我有一个登录视图控制器作为根视图。我尝试通过创建自定义委托来传递数据,但它不起作用。 数据将从 DetailViewController 的 didSelectRowAtIndexPath 方法传递。请让我知道我在哪里做错了。下面是我的代码-
在 MasterViewController.m -
#import "MasterViewController.h"
#import "DetailViewController.h"
@interface MasterViewController ()
@property (strong, nonatomic) DelegateToPassDataInMaster *delegateController;
@property (strong, nonatomic) NSMutableArray *objects;
@end
@implementation MasterViewController
- (void)awakeFromNib {
[super awakeFromNib];
self.clearsSelectionOnViewWillAppear = NO;
self.preferredContentSize = CGSizeMake(320.0, 600.0);
}
- (void) getRowID:(NSString *)_id {
NSLog(@"Row ID : %@", _id);
}
- (void)viewDidLoad {
[super viewDidLoad];
self.delegateController = [[DelegateToPassDataInMaster alloc] init];
self.delegateController.delegate = self;
self.objects = [[NSMutableArray alloc] initWithObjects:@"One",@"Two",@"Three", nil];
// Do any additional setup after loading the view, typically from a nib.
self.detailViewController = (DetailViewController *)[[self.splitViewController.viewControllers lastObject] topViewController];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table View
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.objects.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
NSString *str = self.objects[indexPath.row];
cell.textLabel.text = str;
return cell;
}
@end
在 DetailViewController.m -
#import "DetailViewController.h"
#import "DelegateToPassDataInMaster.h"
@interface DetailViewController ()
@property (strong, nonatomic) DelegateToPassDataInMaster *delegateController;
@property (strong, nonatomic) NSMutableArray *objects;
@end
@implementation DetailViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.delegateController = [DelegateToPassDataInMaster new];
self.objects = [[NSMutableArray alloc] initWithObjects:@"1",@"2",@"3", nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.objects.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
NSString *str = self.objects[indexPath.row];
cell.textLabel.text = str;
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *str = self.objects[indexPath.row];
[self.delegateController getDataFromDelegateFunc:str];
}
@end
在 DelegateToPassDataInMaster.h -
#import <Foundation/Foundation.h>
@protocol DelegateForPassDataToMasterView;
@interface DelegateToPassDataInMaster : NSObject
@property (nonatomic, weak) IBOutlet id <DelegateForPassDataToMasterView> delegate;
-(void) getDataFromDelegateFunc:(NSString *)_id;
@end
#pragma mark -
@protocol DelegateForPassDataToMasterView <NSObject>
@optional
- (void)getRowID:(NSString *)_id;
@end
在 DelegateToPassDataInMaster.h -
#import "DelegateToPassDataInMaster.h"
@implementation DelegateToPassDataInMaster
-(void) getDataFromDelegateFunc:(NSString *)_id {
[self.delegate getRowID:_id];
}
@end
【问题讨论】:
-
嘿 Nuibb,我认为这是因为您没有对主控制器进行真正的引用,而是在创建一个新对象。要我扩展吗?
-
感谢您的回复。你能详细解释一下吗?我真的不明白。我该怎么办?
-
在我的 AppDelegate.m 中,我删除了所有与 spliteView 相关的行。如- - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { return YES; }
-
是的,我可以解释一下,给我几分钟时间
-
查看我的答案以获得更简单的解决方案:stackoverflow.com/questions/24813442/…
标签: ios ipad delegates uisplitviewcontroller master-detail