2012-01-07 60 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; 
} 

基本問題我沒有在任何地方分配代表....呃。