Talha Ch Talha Ch - 2 months ago 13
Objective-C Question

.delegate self unrecognized selector sent to instance

I've a

TableViewController
in which I'm saving the selected cells in an
NSMUtableArray
. After selecting these cells user clicks on a confirm button and in this button action I'm trying to pass that
NSMUtableArray
so that I can display it in another
viewController
tableView



#import <UIKit/UIKit.h>

@protocol SelectedDXDelegate;

@interface AddDXTableViewController : UITableViewController

@property (nonatomic, strong) NSMutableArray *favDXArray;
@property (strong, nonatomic) UISearchController *searchController;
@property (nonatomic, strong) DX *AddEditDX;

@property (weak) id<SelectedDXDelegate> delegate;

- (IBAction)confirmPressed:(id)sender;

@end

@protocol SelectedDXDelegate <NSObject>
@required
-(void)getSelectedDX:(NSMutableArray *)DXselected;
@end


So when confirm button is pressed

- (IBAction)confirmPressed:(id)sender {

[self.delegate getSelectedDX:selectedDX];
[self.navigationController popViewControllerAnimated:YES];
}


it gets me to the

-(void)getSelectedDX:(NSMutableArray *)DXselected
{
myDXSelected = DXselected;
}


But it crashes the app here at reloadData in

- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.DXTableView reloadData];
}

Answer

You seem to have mixed up which view controller should be the delegate and which is the delegator. Also, you are just allocating a new instance of the AddDXTableViewController and assigning this as the delegate. This won't work; you need to have the existing instance of your view controller set as the delegate.

From what I can tell from your question, it is actually an instance of DXViewController that is to be the delegate of AddDXTableViewController

Presumably in DXViewController you have some code something like:

AddDXViewController *newViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"AddDXViewController"];
[self.navigationController pushViewController:newViewController animated:YES];

What you need to do is set your delegate at this point:

AddDXViewController *newViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"AddDXViewController"];
newViewController.delegate = self;
[self.navigationController pushViewController:newViewController animated:YES];

Having said all of that, since you are using a storyboard, a delegate is probably an unnecessarily complicated way of achieving your requirement; You can use a segue to move between the first and second view controller and an unwind segue to return back to the first. You can then implement prepareForSegue in the second view controller and use that to provide the array back to the first view controller