0

i have two view controllers, AbcViewController and XyzViewController. Both controllers behave similarly. Each has a "add" button which opens up a AddNewAbcViewController and AddNewXyzViewController respectively.

On AddNewAbcViewController, when the button "submit" is taped, it does it necessary stuff and close, bringing it back to AbcViewController. I am using delegate here where AbcViewController does the closing of AddNewAbcViewController. This works.

Now I want to do the same for XyzViewController and AddNewXyzViewController, but it is not working. When the btnSubmit is called in AddNewXyzViewController, it didn't enter into XyzViewController dimiss method. I have scanned through my codes many times but don't find anything extra not added. I even gave a different dismiss method name in XyzViewController and AddNewXyzViewController but that didn't work either. What did I miss?

here are my snippets for AbcViewController and AddAbcViewController. The codes for Xyz are identical:

class AddNewAbcViewController.h is

#import <Foundation/Foundation.h>

// protocol
@protocol AddNewAbcProtocol <NSObject>

-(void)dismiss;

@end

@interface AddNewAbcViewController : UIViewController<UITextViewDelegate>

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

@end

class AddNewAbcViewController.m is

@interface AddNewAbcViewController() <UINavigationControllerDelegate, UIImagePickerControllerDelegate>
...
@end

@implementation AddNewAbcViewController

...

- (IBAction)btnSubmit:(id)sender
{
  [self.delegate dismiss];
}
@end

class AbcViewController.h is

#import <Foundation/Foundation.h>
#import "AddNewAbcViewController.h"

@interface AbcViewController : UIViewController<AddNewAbcProtocol, UISplitViewControllerDelegate>
...
@end

class AbcViewController.m is

@implementation AbcViewController

-(void)dismiss
{
    NSLog(@"delegated to dismiss()");
    [self dismissViewControllerAnimated:YES completion:nil];
}

@end
Noor
  • 2,071
  • 19
  • 28
chrizonline
  • 4,779
  • 17
  • 62
  • 102
  • You haven't shown where you are setting the `delegate` – Flexicoder Oct 23 '14 at 09:18
  • sorry setting delegate in where? – chrizonline Oct 23 '14 at 09:19
  • Also if you only want to dismiss the view controller you can use `[self.presentingViewController dismissViewControllerAnimated:YES completion:NULL];` – Cornelius Oct 23 '14 at 09:20
  • in AddNewAbcViewController you are calling `self.delegate dismiss` in order for that to work the `delegate` actually needs to be assigned. I'm assuming from `AbcViewController`. But as @Cornelius has said if all you are trying to do is dismiss it there are better ways – Flexicoder Oct 23 '14 at 09:22
  • oh yes @Cornelius but i need to refresh the tableview in AbcViewController.m – chrizonline Oct 23 '14 at 09:23
  • You can do that in the completion block (I NULL'ed it in my example, but you can call a block there that refreshes your tableview) – Cornelius Oct 23 '14 at 09:24
  • @Cornelius so in the completion block i can call a AbcViewController's method that refresh the table? – chrizonline Oct 23 '14 at 09:28
  • @Flexicoder, do you mean in AbcViewController init(), I add self.newAbc = [[AddNewAbcViewController alloc] init]; self.newAbc.delegate = self; ????? – chrizonline Oct 23 '14 at 09:32
  • @nuttynibbles I thought so, but sadly it isn't that easy. Look at this answer to see how it can be done: http://stackoverflow.com/questions/12658147/present-and-dismiss-uiviewcontroller-with-completion-blocks-without-protocols/12662872#12662872 Maybe your delegate is the cleanest method, though .. – Cornelius Oct 23 '14 at 09:35
  • ya it does look complicated haha – chrizonline Oct 23 '14 at 09:43
  • @Flexicoder i managed to find the missing code. Tks alot mate – chrizonline Oct 23 '14 at 09:50

2 Answers2

5

As everyone explained, basically you forgot a line of code like ".delegate = self".

Here's a handy beginner's intro to delegates.

https://stackoverflow.com/a/4213005/294884

Community
  • 1
  • 1
Fattie
  • 27,874
  • 70
  • 431
  • 719
0

Use if statement to see if delegate works:

if ([self.delegate respondsToSelector:@selector(dismiss)])
{
    [self.delegate dismiss];
}

Create AddNewXyzViewController as an instance variable, but not a local variable.