无法从另一个ViewController调用方法

时间:2011-08-29 12:24:40

标签: ios objective-c uiviewcontroller

我正在尝试执行以下操作

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

其中handleSingleTap是ReaderController中的一个方法但是我无法调用它..方法是下一个

 - (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
    }
}

出了什么问题,为什么我不能称这种方法,我需要一些细节任何帮助赞赏

2 个答案:

答案 0 :(得分:2)

这很简单,你做了:

[readerController handleSingleTap];

实际上你应该这样做:

[readerController handleSingleTap:yourRecognizer];

[readerController handleSingleTap:nil];

如果您未能通过识别器参数,则在预计handleSingleTap时会发送handleSingleTap:信号

答案 1 :(得分:1)

您错过了方法和选择器的工作原理。方法定义中的冒号是选择器名称的一部分。例如,这三种方法:

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

有这些完整的选择器名称:

updateUserInterface
handleSingleTap:
updateItemAtIndex:animated:

名称必须始终匹配。包括我必须再次强调的冒号是名称的一部分。

你打电话给:

[readerController handleSingleTap];

名称中不包含冒号,因此代码假设存在具有此定义的方法,并尝试调用它:

-(id)handleSingleTap;

由于它不存在,您会收到异常并且您的应用程序崩溃。

如果您不想发送参数,则必须仍具有全名。但是可以传递一个可以忽略的参数,例如nil,如下所示:

[readerController handleSingleTap:nil];