I am implementing one simple delegation between the Master and Detail views (Landscape orientation so both views are visible) in a SplitViewController. When I press a button, the table view in Detail should update.

Here is the declaration in MasterViewController.h

@protocol DetailVCDelegate <NSObject>
- (void)requestTableUpdate;
@end
@interface MasterViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
- (IBAction)updateTable:(id)sender;
@property (nonatomic, weak) id <DetailVCDelegate> delegate;

in MVC.m i Do the following for the IBaction updateTable:

[self.delegate requestTableUpdate];

Then in the Detail.h

#import "MasterViewController.h"
@interface DetailViewController : UITableViewController <DetailVCDelegate>

And in the Detail.m

- (void)requestTableUpdate
{
    NSLog(@"called");
    [self.tableView reloadData];
}

The log is never called, nor the update for the tableView. I would appreciate any suggestions of how to fix this, i guess I am missing some delegation rule that is specific for SplitViews

有帮助吗?

解决方案

When you send a message to something and nothing happens, your first thought should be: hmm, maybe I'm sending a message to nil.

So, you say:

[self.delegate requestTableUpdate];

But nothing happens. So you should immediately check to see whether self.delegate is nil.

You have a declared a delegate property:

@property (nonatomic, weak) id <DetailVCDelegate> delegate;

But a property has no value until you give it one. It is up to you to set the delegate property of your MasterViewController instance to some other instance (of something that adopts the DetailVCDelegate protocol, obviously). Delegates are not born: they are made, deliberately.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top