Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
523 views
in Technique[技术] by (71.8m points)

iphone - Passing Values Between Master and Detail in UISplitViewController Using Storyboards

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; 
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...