
精通
英语
和
开源
,
擅长
开发
与
培训
,
胸怀四海
第一信赖
锐英源精品开源,禁止转载和任何形式的非法内容使用,违者必究
I first created a property in my SecondViewController.h file using首先在我的SecondViewController.h文件中创建了一个属性
@property (weak, nonatomic) IBOutlet UIWebView *webView;
And then synthesised it in the corresponding .m file using然后使用相应的.m文件合成它
@property (weak, nonatomic) IBOutlet UIWebView *webView;
I then created a function to create a webpage using a string which the user would cast as an argument. The function:然后,我创建了一个函数来使用字符串创建网页,用户可以将其作为参数进行转换。功能:
void createWebpage(NSString *webString) { NSURL *url = [NSURL URLWithString:webString]; NSURLRequest *requestUrl = [NSURLRequest requestWithURL:url]; [webView loadRequest:requestUrl]; }
And where it is called.调用位置。
- (void)viewDidLoad { createWebpage(@"http://www.google.com"); [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. }
However, in the last line of the function, [webView loadRequest:requestUrl];, webView produces the error "Use of undeclared identifier 'webView'. Why is this, and how can I fix it? All help appreciated.但是,在函数的最后一行[webView loadRequest:requestUrl];,webView会产生错误“使用未定义的标识符'webView'。为什么会这样,我该如何解决?所有帮助都表示赞赏。
You are declaring a property which is a available in an object. But you are declaring a simple C method:您定义属性是对象中可用的。但是你要定义了一个简单的C方法:
void createWebpage(NSString *webString) { NSURL *url = [NSURL URLWithString:webString]; NSURLRequest *requestUrl = [NSURLRequest requestWithURL:url]; [webView loadRequest:requestUrl]; }
This method will be executed in a "global context" but not on the object. So you can't access the objects properties.此方法将在“全局上下文”中执行,但不在对象上执行。因此您无法访问对象属性。
而是使用一种方法:
Instead use a method:
- (void) createWebpage:(NSString *)webString { NSURL *url = [NSURL URLWithString:webString]; NSURLRequest *requestUrl = [NSURLRequest requestWithURL:url]; [self.webView loadRequest:requestUrl]; }
And you have to use self to refer to the current object when you're accessing a property.
You can then call this method:当您访问属性时,必须使用self来引用当前对象。
然后,您可以调用此方法:
[self createWebpage:@"http://www.google.com"];
I really recommend you to read this: https://developer.apple.com/library/mac/documentation/cocoa/conceptual/ProgrammingWithObjectiveC/Introduction/Introduction.html
void createWebpage is not a instance method, so the instance variables (such as webView) won't be accessible from there.
You must declare the method as: -(void)createWebpage:(NSString *)webString, and call it as [self createWebPage:@"http://www.google.com"];void createWebpage不是实例方法,因此webView无法从那里访问实例变量(例如)。
您必须将方法声明为:-(void)createWebpage:(NSString *)webString,并将其命名为[self createWebPage:@"http://www.google.com"];