另一个类的目标C调用方法 [英] Objective C Calling Method on Another Class
问题描述
我有两个类, MainViewController
和'FooView'例如。
I have 2 classes, MainViewController
and 'FooView' for example.
在MainViewController中, - (void)doSearch
In MainViewController I have a method called -(void)doSearch
我试图调用 doSearch
来自Foo,在日志doSearch(NSLog)中看到,但UIWebView没有响应。
I'm trying to call doSearch
from Foo, is see in the log "doSearch" (NSLog) but the UIWebView doesn't response.
Foo / p>
Foo
MainViewController *mainVc = [[MainViewController alloc] init];
[mainVc doSearch];
MainViewController
- (void)doSearch {
NSLog(@"doSearch");
[myWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com/"]]];
}
myWebView是一个UIWebView。
myWebView is a UIWebView.
感谢,
Guy Dor
推荐答案
这里的东西是零。在Obj-C中,您可以在没有错误或异常的情况下在nil对象上发送消息(调用方法)。在你的情况下,您的MainViewController可能在FooView中为null,或者该方法中的某个可能是nil。例如,如果有一个用于该视图控制器的xib文件,则需要使用initWithNibName:bundle :.如果你不这样做,那么IBOutlets将是nil(例如可能是你的doSearch:方法中的myWebView)。
I think something is nil here. In Obj-C you can send messages (call methods) on a nil object without an error or exception. In your case, your MainViewController could be null in the FooView, or it is possible that something in that method is nil. For example, if have a xib file for that view controller, then it needs to be loaded with initWithNibName:bundle:. If you don't do that, then the IBOutlets will be nil (possibly the myWebView in your doSearch: method for example).
这篇关于另一个类的目标C调用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!