我已经设置了一个委托(delegate)方法来从我的 masterViewController
到我的 detailViewController
进行通信,但委托(delegate)方法没有被调用。
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 中合成委托(delegate):
@synthesize delegate;
最后,我从 didSelectRowAtIndexPath 方法中调用委托(delegate)方法,如下所示:
- (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;
}
基本上问题是我没有将委托(delegate)分配到任何地方......呃。
关于ios - UISplitView 中的委托(delegate)未被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8766708/