2012-01-07 78 views
0

我已经设置了一个委托方法来从我的masterViewController到我的detailViewController进行通信,但委托方法没有被调用。UISplitView中的代表没有被调用

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); 
} 

当我运行应用程序,从SelectionChangedNSLog,不会被调用我没有得到任何错误。有任何想法吗?

+0

你确定'self.delegate'不是零吗? – Saphrosit 2012-01-07 02:04:01

+0

我刚测试过它,果然,self.delegate是(null)。我想我应该明白为什么这是... – 2012-01-07 02:23:19

+1

似乎无法弄清楚我的委托是空....呃。 – 2012-01-07 03:56:07

回答

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; 
} 

基本问题我没有在任何地方分配代表....呃。