0

I have a ViewController in which I create a Modal ViewController. I have a SEL value in my modal that I set when it is being instantiated from the parent.

setDateViewController.selectorName = @selector(myMethod:);

In my modal I am trying to call this SEL like:

[[self parentViewController] performSelector:self.selectorName withObject:selectedDate afterDelay:.5]; 

{selectedDate} is obviously a value from my modal.

I don't get any errors or stack, however, this SEL (method) on my parent is never being called. For some reason I think this should work, but something tells me I'm way off track.

Thanks.

El Guapo
  • 5,581
  • 7
  • 54
  • 82

2 Answers2

1

Perhaps you would consider added a delegate protocol to your modal that will allow it to call the method on the parent.

Quick (untested) example:

// MyController.h
@protocol MyControllerDelegate;
@interface MyController : UIViewController
{
    id<MyControllerDelegate> delegate;
}
@end

@protocol MyControllerDelegate <NSObject>
 - (void)methodToCall:(id)sender;
@end


// MyControler.m
@implementation MyController

- (void) loadView
{
    [super loadView];
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(20.0f, 20.0f, 50.0f, 30.0f);
    [btn setTitle:@"blah" forState:UIControlStateNormal];
    [btn addTarget:self.delegate 
         action:@selector(methodToCall:) 
         forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:btn];
}

@end

// ParentController.h
@interface ParentController : UIViewController<MyControllerDelegate>
{
}
@end

// ParentController.m
@implementation ParentController

- (void)methodToCall:(id)sender
{
    NSLog(@"HERE");
}

@end

Just make sure when you are creating your modal controller you set it's delegate to self on the parent:

MyController *controller = [[MyController alloc] init];
controller.delegate = self;
[self presentModalViewController:controller animated:YES];
Wess Cope
  • 26
  • 2
  • yeah... i've seen this before... for some reason it doesn't sit well with me... was hoping to make it as generic as possible... but, from what I can see this seems to be the de-facto standard... thanks! – El Guapo Sep 04 '11 at 15:53
  • 1
    You can also use a block callback. – Fabian Kreiser Sep 04 '11 at 17:13
  • @Fabian -- I've already accepted an answer, but I'm curious about your suggestion (always looking for new ways to do things); care to elaborate in Obj-c? – El Guapo Sep 06 '11 at 12:37
  • 1
    The answer to [this question](http://stackoverflow.com/questions/4824038/how-to-simplify-callback-logic-with-a-block) shows you how to implement a block callback. The SomeObjectBlockDelegate class has two blocks as iVars. That's what you need to do. – Fabian Kreiser Sep 06 '11 at 16:31
1

I guess [self parentviewcontroller] is not returning anything. Try UiviewController* v = [self parentviewcontroller]; and check if is nil. Most probably it should be nil. Else if its was pointing to another object of different class then it would have crashed. Please do one thing. YOu should set bot the object and the methd you need to call. IT will solve any issues if it has any.

setDateViewController.selectorDelegate = self;
setDateViewController.selectorName = @selector(myMethod:);

call like this from parent class. So you can dynamically specify the method and the object you want to call gives more flexibility.

and use, [selectorDelegate performSelector:self.selectorName withObject:selectedDate afterDelay:.5];

this should solve any issues.

J S Rodrigues
  • 471
  • 4
  • 8