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; 
    }
azamsharp
  • 19,710
  • 36
  • 144
  • 222

2 Answers2

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__];
}
CodaFi
  • 43,043
  • 8
  • 107
  • 153
  • But then how do I invoke the didSelectCustomer delegate from within the didSelectRowAtIndexPath method. – azamsharp Apr 04 '12 at 21:39
  • You're doing that correctly. You just never assigned a delegate, so it would never have worked. – CodaFi Apr 04 '12 at 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 Apr 04 '12 at 21:44
  • You need an instance of MasterViewController. – CodaFi Apr 04 '12 at 21:45
  • I already tried that and it did not work. I am using UISplitViewController. See UPDATE 1 – azamsharp Apr 04 '12 at 21:49
  • I pasted the complete code so you can look at it more. http://codepaste.net/c6as3u – azamsharp Apr 04 '12 at 21:59
  • Why have you added the delegate property inside the Customer class? I have the id inside the MasterViewController. – azamsharp Apr 04 '12 at 22:00
  • Wow, you have some serious misunderstandings as to the format of delegation in ObjC. See [here](http://iphonedevelopertips.com/objective-c/the-basics-of-protocols-and-delegates.html) for a tutorial and [here](http://stackoverflow.com/questions/1045803/how-does-a-delegate-work-in-objective-c) for a related question. – CodaFi Apr 04 '12 at 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 Apr 04 '12 at 22:08
  • I used the confirmsToProtocol and it shows that the DetailViewController DOES conform to the CustomerDelegate. – azamsharp Apr 04 '12 at 22:18
  • Man you were sending me on wild goose chases. Take a look at the edit to find the solution. – azamsharp Apr 04 '12 at 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.

rbbtsn0w
  • 75
  • 11