无法从另一个 ViewController 调用方法
我正在尝试执行以下操作
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,这很简单,你做到了:
实际上你应该这样做:
或者
如果你未能传递识别器参数,那么当需要
handleSingleTap:
时,你将发送一个handleSingleTap
信号well this quite simple, you did:
when actually you should do:
or
if you fail to pass the recognizer argument you will send a
handleSingleTap
signal when ahandleSingleTap:
is expected您错过的是方法和选择器的工作原理。方法定义中的冒号是选择器名称的一部分。例如,这三种方法:
具有这些完整的选择器名称:
名称必须始终完全匹配。我必须再次强调,包括冒号,它是名称的一部分。
您调用此方法:
名称中不包含冒号,因此代码假设存在具有此定义的方法,并尝试调用它:
由于它不存在,您会收到异常并且应用程序崩溃。
如果您不想发送参数,您仍必须保留全名。但可以传递一个可以忽略的参数,例如
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:
Has these full selector names:
Names must always match in full. Including the colons that, I must stressed again, are part of the names.
You call to this:
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:
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: