我已经设置了一个委托方法,可以从我的masterViewController
到我的detailViewController
进行通信,但委托方法没有被调用。
MasterViewController.h
#import <UIKit/UIKit.h>
@class DetailViewController;
@class MasterViewController;
@protocol MasterViewControllerDelegate
- (void)SelectionChanged:(NSString *)url;
@end
@interface MasterViewController : UITableViewController
@property (nonatomic, weak) id<MasterViewControllerDelegate> delegate;
@property (strong, nonatomic) DetailViewController *detailViewController;
@end
然后在我的MasterViewController.m中我正在合成委托:
@synthesize delegate;
最后我从didSelectRowAtIndexPath方法调用委托方法,如下所示:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSArray *links = [NSArray arrayWithObjects:
@"http://www.link1.com",
@"http://www.link2.com",
@"http://www.link3.com",
nil];
[self.delegate SelectionChanged:[links objectAtIndex: indexPath.row]];
}
然后在我的DetailViewController.h中我有:
@interface DetailViewController : UIViewController <UISplitViewControllerDelegate, MasterViewControllerDelegate>
在DetailViewController.m中:
- (void)SelectionChanged:(NSString *)url {
NSLog(@"URL is %@", url);
}
当我运行应用时,来自NSLog
的{{1}}永远不会被调用,我没有错误。有什么想法吗?
答案 0 :(得分:1)
好的,我想通了......在我的AppDelegate.m文件中,我将以下内容添加到didFinishLaunchingWithOptions
DetailViewController *detail = (DetailViewController *)navigationController.topViewController;
UINavigationController *masterNavigationController = [splitViewController.viewControllers objectAtIndex:0];
MasterViewController *master = (MasterViewController *)masterNavigationController.topViewController;
NSLog(@"%@",masterNavigationController.topViewController);
master.delegate = detail;
所以整个方法看起来像这样:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
UISplitViewController *splitViewController = (UISplitViewController *)self.window.rootViewController;
UINavigationController *navigationController = [splitViewController.viewControllers lastObject];
splitViewController.delegate = (id)navigationController.topViewController;
DetailViewController *detail = (DetailViewController *)navigationController.topViewController;
UINavigationController *masterNavigationController = [splitViewController.viewControllers objectAtIndex:0];
MasterViewController *master = (MasterViewController *)masterNavigationController.topViewController;
NSLog(@"%@",masterNavigationController.topViewController);
master.delegate = detail;
return YES;
}
基本上问题是我没有在任何地方分配代表......呃。