ios - UISplitView 中的委托(delegate)未被调用

标签 ios delegates ios5 uisplitviewcontroller

我已经设置了一个委托(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);
}

当我运行应用程序时,来自 SelectionChangedNSLog 永远不会被调用,并且我没有收到任何错误。有什么想法吗?

最佳答案

好吧,我想通了...在我的 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/

相关文章:

ios - "Use of unresolved indentifier ' 自身 '"带按钮.addTarget

ios - 为应用程序商店构建时出现 XCode 错误

objective-c - 带有几个参数的 performSelector

iphone - 我如何将数据传回父 View Controller 。我正在使用 addSubView 方法来显示 subview

ios - 如何使一个标签栏按钮引用 iOS 中的两个 View / Controller

ios - 无法在 iOS7.1 应用程序上找到 EXC_BAD_ACCESS 的原因

ios - UITableView 在滚动中改变图像

iphone - 如何在派生类中分配全局 NSInteger 对象

c# - 注册定时器流逝事件

c# - 为什么编译器不能在这种重载解析情况下告诉更好的转换目标? (协方差)