UISplitView中的代理未被调用 [英] Delegate in UISplitView not being called

查看:179
本文介绍了UISplitView中的代理未被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经设置了一个委托元数据,从我的 masterViewController 与我的 detailViewController 通信,但是委托方法是不要打电话。

I've set up a delegate methos to communicate from my masterViewController to my detailViewController but the delegate method isn't getting called.

MasterViewController.h

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中,我正在合成代表:

Then in my MasterViewController.m I'm synthesizing the delegate:

@synthesize delegate;

最后我从我的didSelectRowAtIndexPath方法调用委托方法,如下所示:

And finally I'm calling the delegate method from my didSelectRowAtIndexPath method like so:

- (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中我有:

Then in my DetailViewController.h I have:

@interface DetailViewController : UIViewController <UISplitViewControllerDelegate, MasterViewControllerDelegate>

而在DetailViewController.m中:

And in DetailViewController.m:

- (void)SelectionChanged:(NSString *)url {
    NSLog(@"URL is %@", url);
}

当我运行应用程序时, NSLog SelectionChanged 从来没有被调用,我没有收到错误。任何想法?

When I run the app, the NSLog from SelectionChanged is never called and I get no errors. Any ideas?

推荐答案

好吧,我想出了...在我的AppDelegate.m文件中,我添加了以下内容到FinishLaunchingWithOptions



Okay I figured it out... In my AppDelegate.m file I added the following to didFinishLaunchingWithOptions

DetailViewController *detail = (DetailViewController *)navigationController.topViewController;
UINavigationController *masterNavigationController = [splitViewController.viewControllers objectAtIndex:0];
MasterViewController *master = (MasterViewController *)masterNavigationController.topViewController;

NSLog(@"%@",masterNavigationController.topViewController);
master.delegate = detail;

所以整个方法看起来像这样:

So the whole method looks like this:

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

基本上问题是我没有在任何地方分配委托。duh。

Basically the problem is that I wasn't assigning the delegate anywhere.... duh.

这篇关于UISplitView中的代理未被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆