Mohamed Emad Hegab
Mohamed Emad Hegab

Reputation: 2675

Can't call method from another ViewController

I'm trying to do the following

 ReaderController *readerController =  [[[ReaderController alloc] init] autorelease];;
readerController.fileName = fileName;
[readerController handleSingleTap];

where handleSingleTap is a method in the ReaderController but i can't call it .. the method is next

 - (void)handleSingleTap:(UITapGestureRecognizer *)recognizer
{
#ifdef DEBUGX
    NSLog(@"%s", __FUNCTION__);
#endif

    ReaderDocument *document = [ReaderDocument unarchiveFromFileName:SAMPLE_DOCUMENT];

    if (document == nil) // Create a brand new ReaderDocument object the first time we run
    {
        NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
        NSString *documentsDirectoryPath = [searchPaths objectAtIndex:0]; 
        NSString *path = [documentsDirectoryPath stringByAppendingPathComponent:[[NSString stringWithFormat:@"%@",fileName] stringByAppendingPathExtension:@"pdf"]];



        [self checkAndCreatePList];
        NSMutableDictionary* plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:pListPath];

        [plistDict setValue: [NSString stringWithFormat:@"%@",fileName]  forKey:@"fileName"];
        [plistDict writeToFile:pListPath atomically: YES];



//      NSString *filePath = [[NSBundle mainBundle] pathForResource:path ofType:nil];

        document = [[[ReaderDocument alloc] initWithFilePath:path password:nil] autorelease];
    }

    if (document != nil) // Must have a valid ReaderDocument object in order to proceed
    {
        ReaderViewController *readerViewController = [[ReaderViewController alloc] initWithReaderDocument:document];

        readerViewController.delegate = self; // Set the ReaderViewController delegate to self

#if (DEMO_VIEW_CONTROLLER_PUSH == TRUE)

        [self.navigationController pushViewController:readerViewController animated:YES];

#else // present in a modal view controller

        readerViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
        readerViewController.modalPresentationStyle = UIModalPresentationFullScreen;

        [self presentModalViewController:readerViewController animated:YES];

#endif // DEMO_VIEW_CONTROLLER_PUSH

        [readerViewController release]; // Release the ReaderViewController
    }
}

what is wrong and why can't i call this method please i need some details any help appreciated

Upvotes: 0

Views: 406

Answers (2)

PeyloW
PeyloW

Reputation: 36752

What you have missed is how methods and selectors works. The colons in method definition is part of the selector name. For example these three methods:

-(void)updateInterface;
-(void)handleSingleTap:(id)sender;
-(void)updateItemAtIndex:(NSInteger)integer animated:(BOOL)animated;

Has these full selector names:

updateUserInterface
handleSingleTap:
updateItemAtIndex:animated:

Names must always match in full. Including the colons that, I must stressed again, are part of the names.

You call to this:

[readerController handleSingleTap];

Does not include a colon in the name, and thus the code makes the assumption that a method with this definition exists, and tries to call it:

-(id)handleSingleTap;

Since it does not exist, you get an exception and your app crashes.

If you do not want to send the argument, you must still have the full name. But can pass an argument that can be ignored, for example nil, like this:

[readerController handleSingleTap:nil];

Upvotes: 1

apouche
apouche

Reputation: 9983

well this quite simple, you did:

[readerController handleSingleTap];

when actually you should do:

[readerController handleSingleTap:yourRecognizer];

or

[readerController handleSingleTap:nil];

if you fail to pass the recognizer argument you will send a handleSingleTap signal when a handleSingleTap: is expected

Upvotes: 2

Related Questions