无法从另一个 ViewController 调用方法

发布于 2024-12-02 04:37:09 字数 2352 浏览 0 评论 0原文

我正在尝试执行以下操作

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

出了什么问题,为什么我不能调用此方法,我需要一些详细信息,任何帮助表示赞赏

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

飞烟轻若梦 2024-12-09 04:37:09

好吧,这很简单,你做到了:

[readerController handleSingleTap];

实际上你应该这样做:

[readerController handleSingleTap:yourRecognizer];

或者

[readerController handleSingleTap:nil];

如果你未能传递识别器参数,那么当需要 handleSingleTap: 时,你将发送一个 handleSingleTap 信号

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

墟烟 2024-12-09 04:37:09

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

-(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];

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];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文