使用情节提要板在UISplitViewController中的主对象和明细之间传递值 [英] Passing Values Between Master and Detail in UISplitViewController Using Storyboards

查看:74
本文介绍了使用情节提要板在UISplitViewController中的主对象和明细之间传递值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在Customer.h文件中定义了协议,如下所示:

I have defined the protocol in Customer.h file which is shown below:

@class Customer; 
@protocol CustomerDelegate <NSObject>

-(void) didSelectCustomer:(Customer *) customer; 

@end

@interface Customer : NSObject
{

}

@property (nonatomic,copy) NSString *name; 
@property (nonatomic,copy) NSString *occupation; 

@end

MasterViewController(左侧)调用didSelectCustomer方法,如下所示:

The MasterViewController (left side) invokes the didSelectCustomer method as shown below:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    Customer *selectedCustomer = [customers objectAtIndex:[indexPath row]];
    [self.delegate didSelectCustomer:selectedCustomer]; 
}

现在,我需要告诉DetailViewController(右侧)做某事. DetailViewController符合CustomerDelegate协议.

Now, I need to tell the DetailViewController (right side) to do something. The DetailViewController complies with the CustomerDelegate protocol.

@interface DetailViewController : UIViewController<UISplitViewControllerDelegate,CustomerDelegate>
{

}

-(void) didSelectCustomer:(Customer *)customer
{
    NSLog(@"sssdasdasdasd");
}

didSelectCustomer方法从不调用.我想我需要设置masterViewController.delegate = self,但是我不确定在哪里设置.

The didSelectCustomer method is never invoked. I think I need to set the masterViewController.delegate = self but I am not sure where to set this thing up.

更新1:

我将MasterViewController的实例添加到DetailViewController内,但是它不起作用:

I added the instance of MasterViewController inside the DetailViewController but it did not work:

- (void)viewDidLoad
{
    [super viewDidLoad];    

    MasterViewController *master = [[MasterViewController alloc] init];
    master.delegate = self; 
}

解决方案:

在AppDelegate中:

In AppDelegate:

  else 
    {
        UISplitViewController *splitViewController = (UISplitViewController *) self.window.rootViewController; 
        splitViewController.delegate = [splitViewController.viewControllers lastObject];



        UINavigationController *navigationController = [splitViewController.viewControllers lastObject];
       // splitViewController.delegate = (id)navigationController.topViewController;




        DetailViewController *detail =(DetailViewController *) [splitViewController.viewControllers lastObject];

        UINavigationController *masterNavigationController = [splitViewController.viewControllers objectAtIndex:0];

        MasterViewController *master = (MasterViewController *)masterNavigationController.topViewController;

        master.delegate = detail; 
    }

推荐答案

拆分视图控制器的最后一个对象.

the split view controller's last object.

此对象返回一个UI导航控制器.

this object is return a UI navigation controller.

您知道,那么您就可以自己做.

you know, then you can do yourself.

这篇关于使用情节提要板在UISplitViewController中的主对象和明细之间传递值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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