使用未声明的标识符 – Xcode
我首先在我的SecondViewController.h文件中使用创build一个属性
@property (weak, nonatomic) IBOutlet UIWebView *webView;
然后用相应的.m文件合成它
@property (weak, nonatomic) IBOutlet UIWebView *webView;
然后我创build了一个函数来创build一个网页,使用一个string,用户将作为参数。 function:
void createWebpage(NSString *webString) { NSURL *url = [NSURL URLWithString:webString]; NSURLRequest *requestUrl = [NSURLRequest requestWithURL:url]; [webView loadRequest:requestUrl]; }
和它被称为的地方。
- (void)viewDidLoad { createWebpage(@"http://www.google.com"); [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. }
但是,在函数的最后一行, [webView loadRequest:requestUrl];
,webView产生错误“使用未声明的标识符'webView'。为什么是这样的,我该如何解决?所有的帮助表示赞赏。
您正在声明一个对象中可用的属性。 但是你正在声明一个简单的C方法:
void createWebpage(NSString *webString) { NSURL *url = [NSURL URLWithString:webString]; NSURLRequest *requestUrl = [NSURLRequest requestWithURL:url]; [webView loadRequest:requestUrl]; }
此方法将在“全局上下文”中执行,但不在对象上执行。 所以你不能访问对象的属性。
而是使用一种方法:
- (void) createWebpage:(NSString *)webString { NSURL *url = [NSURL URLWithString:webString]; NSURLRequest *requestUrl = [NSURLRequest requestWithURL:url]; [self.webView loadRequest:requestUrl]; }
当你访问一个属性时,你必须使用self
来引用当前对象。
然后你可以调用这个方法:
[self createWebpage:@"http://www.google.com"];
我真的build议你阅读: https : //developer.apple.com/library/mac/documentation/cocoa/conceptual/ProgrammingWithObjectiveC/Introduction/Introduction.html
void createWebpage
不是一个实例方法,所以实例variables(如webView
)将无法从那里访问。
你必须声明方法为: -(void)createWebpage:(NSString *)webString
,并将其称为[self createWebPage:@"http://www.google.com"];