string manipulation and methods in obj-c
I am very new to obj-c (about 1 day) and i have read the documentation on how to call methods and how to modify strings and i have used similar code in another program and it worked fine. I'm programming a simple web browser for the iphone to teach myself about WebViewController library. When i compile this it gives me the warning "'WebViewController' may not respond to '-parseURl:" at line 17 in the .m file and when i run it i throws the error "NSInvalidArgumentException" in the console.
Code for this in WebViewController.h:
#import <UIKit/UIKit.h>
@interface WebViewController : UIViewController {
IBOutlet UIWebView *webView;
IBOutlet UITextField *textField;
}
NSString *urlAddress;
NSURL *url;
NSURLRequest *requestObj;
- (IBAction)gotoAddress:(id)sender;
- (NSString*) parseURL:(NSString*)str;
@property (nonatomic, retain) UIWebView *webView;
@end
Code for this in WebViewController.m:
#import "WebViewController.h"
@implementation WebViewController
@synthesize webView;
开发者_Go百科
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
// Initialization code
}
return self;
}
- (IBAction)gotoAddress:(id)sender {
urlAddress = textField.text;
urlAddress = [self parseURl:urlAddress];
url = [NSURL URLWithString:urlAddress];
requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
NSLog(@"urlAddress= %s", [urlAddress cStringUsingEncoding:1]);
}
- (NSString*) parseURL:(NSString*)str {
NSLog(@"made it");
NSString *httpPart = @"http://";
if ([str rangeOfString:httpPart].location == NSNotFound) {
NSString *correctURL = [NSString stringWithFormat:@"%@%@", httpPart, str];
return correctURL;
}
else {
return str;
}
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
// Release anything that's not essential, such as cached data
}
- (void)dealloc {
[webView release];
[super dealloc];
}
@end
Thanks for the help
Objective-C (and most other languages) is case-sensitive. "URL" and "URl" are different.
urlAddress = [self parseURl:urlAddress];
should be
urlAddress = [self parseURL:urlAddress];
精彩评论