[英]Delegate in UISplitView not being called
我已經設置了一個委托方法,以便從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);
}
當我運行應用程序時,從未調用SelectionChanged
的NSLog
,也沒有出現任何錯誤。 有任何想法嗎?
好吧,我知道了...在我的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;
}
基本上,問題是我沒有在任何地方分配代表。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.