【问题标题】:Passing Values Between Master and Detail in UISplitViewController Using Storyboards使用 Storyboard 在 UISplitViewController 中的 Master 和 Detail 之间传递值
【发布时间】:2012-04-04 21:20:21
【问题描述】:

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

@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 方法,如下所示:

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

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

@interface DetailViewController : UIViewController<UISplitViewControllerDelegate,CustomerDelegate>
{

}

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

从不调用 didSelectCustomer 方法。我想我需要设置 masterViewController.delegate = self 但我不确定在哪里设置这个东西。

更新 1:

我在 DetailViewController 中添加了 MasterViewController 的实例,但它不起作用:

- (void)viewDidLoad
{
    [super viewDidLoad];    

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

解决方案:

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

【问题讨论】:

  • 您解决了这个问题吗?这会吃掉我的大脑。如果您找到了解决方法,请告诉我?

标签: iphone objective-c


【解决方案1】:

您永远不会明确地将自己声明为 Consumer 类的委托。仅仅符合它不会削减它。通过创建 Consumer 的实例在 -viewDidLoad 中声明它,可能像这样:

-(void)viewDidLoad {
    Consumer *consumer = [[Consumer alloc]init];
    [consumer setDelegate:self];
}

您也没有在 Consumer 中为您的委托对象声明属性,因此它永远无法真正被访问。首先这样做:

@class Customer; 
@protocol CustomerDelegate <NSObject>

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

@end

@interface Customer : NSObject
{

}

@property (nonatomic,copy) NSString *name; 
@property (nonatomic,copy) NSString *occupation; 
@property (weak) id <CustomerDelegate> delegate; //use assign or __unsafe_unretained if targeting <5.0.

@end

你可以像这样检查你的类是否符合你的协议:

if (![delegate conformsToProtocol:@protocol(CustomerDelegate)]) {
    [NSException raise:@"Delegate Exception"
                format:@"Parameter does not conform to CustomerDelegate protocol at line %d", (int)__LINE__];
}

【讨论】:

  • 但是我如何从 didSelectRowAtIndexPath 方法中调用 didSelectCustomer 委托。
  • 你做得对。您只是从未分配过代表,因此它永远不会起作用。
  • 我的委托在 MasterViewController 文件中。所以从 DetailViewController 应该是 _masterViewController.delegate = self;但它不起作用。
  • 你需要一个 MasterViewController 的实例。
  • 我已经尝试过了,但没有成功。我正在使用 UISplitViewController。见更新 1
【解决方案2】:

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

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

你知道,那么你可以自己做。

【讨论】:

    猜你喜欢
    • 2011-12-23
    • 1970-01-01
    • 1970-01-01
    • 2016-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-25
    相关资源
    最近更新 更多