Passing Values Between Master and Detail in UISplitViewController Using Storyboards

Go To StackoverFlow.com

4

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

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

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");
}

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.

UPDATE 1:

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

SOLUTION:

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; 
    }
2012-04-04 21:20
by azamsharp
Have you fixed the issue? This eats my brain. Please let me know if you've found the fix - GenieWanted 2013-09-16 13:24


0

You never explicitly declare yourself as the delegate to the Consumer class. Merely conforming to it won't cut it. Declare it in -viewDidLoad by creating an instance of Consumer, possibly like this:

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

You also don't declare a property for your delegate object in Consumer, so it can never actually be accessed. Do this first:

@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

You can check if your class conforms to your protocol like so:

if (![delegate conformsToProtocol:@protocol(CustomerDelegate)]) {
    [NSException raise:@"Delegate Exception"
                format:@"Parameter does not conform to CustomerDelegate protocol at line %d", (int)__LINE__];
}
2012-04-04 21:32
by CodaFi
But then how do I invoke the didSelectCustomer delegate from within the didSelectRowAtIndexPath method - azamsharp 2012-04-04 21:39
You're doing that correctly. You just never assigned a delegate, so it would never have worked - CodaFi 2012-04-04 21:42
My delegate is in the MasterViewController file. So from DetailViewController it should be something _masterViewController.delegate = self; But it is not working - azamsharp 2012-04-04 21:44
You need an instance of MasterViewController - CodaFi 2012-04-04 21:45
I already tried that and it did not work. I am using UISplitViewController. See UPDATE - azamsharp 2012-04-04 21:49
see my edit, as well - CodaFi 2012-04-04 21:55
I pasted the complete code so you can look at it more. http://codepaste.net/c6as3 - azamsharp 2012-04-04 21:59
Why have you added the delegate property inside the Customer class? I have the id inside the MasterViewController - azamsharp 2012-04-04 22:00
Wow, you have some serious misunderstandings as to the format of delegation in ObjC. See here for a tutorial and here for a related question - CodaFi 2012-04-04 22:03
I have used protocols and delegates before successfully! The only difference is that the Customer class simply have the protocol declaration and then the MasterViewController uses the id and invokes the didSelectCustomer method - azamsharp 2012-04-04 22:08
I used the confirmsToProtocol and it shows that the DetailViewController DOES conform to the CustomerDelegate - azamsharp 2012-04-04 22:18
Man you were sending me on wild goose chases. Take a look at the edit to find the solution - azamsharp 2012-04-04 22:37


0

the split view controller's last object.

this object is return a UI navigation controller.

you know, then you can do yourself.

2014-01-20 08:42
by rbbtsn0w
Ads