0

I have a UIViewController Class Aplha that calls another UIViewController Class Beta in a modalView.
In Alpha.h, I declared a function:

-(void)selectMyImage:(NSString *)myImage;

In Aplha.m:

#import "Beta.h"

-(IBAction)clickMe:(id)sender
{
    Beta *b=[[Beta alloc]initWithNibName:@"Beta" bundle:nil];
    b.delegate=self;
    [self presentModalViewController:b animated:YES];
}

The above code shows the ViewCOntroller in modal view. I have an id delegate; declared in my Beta.h. Now when i try to call the function of Alpha from Beta class, it gives me error.
Here is my code in Beta.m:

 -(void)callAlpha
 {
     [delegate selectMyImage:imagename]; //imagename is declared in Beta.h too.
 }

The Xcode shows the red error mark saying "No known instance method for selector 'mySelectedImage:'"

In iOS 4.x SDK it runs fine. and Here I have enabled the ARC also.

Kjuly
  • 34,476
  • 22
  • 104
  • 118
Ajeet Pratap Maurya
  • 4,244
  • 3
  • 28
  • 46

1 Answers1

1

This is because the type id has no method named -mySelectedImage:. It's customary to create a protocol for your delegates and declare the delegate as following that protocol:

// Declared somewhere that both Alpha and Beta can see
@protocol BetaDelegate
- (void)selectMyImage:(NSString*)imageName;
@end

// Alpha.h
@interface Alpha : UIViewController <BetaDelegate>
 // rest of alpha decls
@end

// Beta.h
@interface Beta : UIViewController
@property(nonatomic,weak) id<BetaDelegate> delegate;
  // rest of beta decls
@end

If this seems like too much work for something only you're working on or whatever, you can also just specifically declare the delegate as being of class Alpha, then the compiler will be able to see that Alpha actually declares that method.

Jason Coco
  • 77,985
  • 20
  • 184
  • 180