Reputation: 555
I'm creating a Google search with a UIWebView
and UISearchBar
.
Here's the code from the .m file -
#import "ThirdViewController.h"
@implementation ViewController3
-(IBAction)Googlesearch:(id)sender {
NSString *query = [googleBar.text stringByReplacingOccurrencesOfString:@" " withString:@"+"];
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.google.co.uk/search?q=%@", query]];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webview3 loadRequest:request];
[self.view addSubview:webview3];
[self.view bringSubviewToFront:webview3];
}
@end
.h file -
#import <UIKit/UIKit.h>
@interface ViewController3 : UIViewController <UISearchBarDelegate> {
IBOutlet UIWebView *webview3;
IBOutlet UISearchBar *googleBar;
}
-(IBAction)Googlesearch:(id)sender;
@end
Note: The IBactions on both files aren't connected (as I am unsure where to connect it to). But the webview/searchbar are connected to the iboutlets.
How it's suppose to work:
The webview is blank at first, input text in the searchbar, press "search", uiwebview loads Google with the search results of the query entered in the searchbar
Problem/Question:
When I run the app, the UIWebView
doesn't load the Google search when I input something into the searchbar.
How do I fix this?
Upvotes: 0
Views: 375
Reputation: 11127
Set the delegates of your UISearchBar
object then this method will triggered
-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
NSString *query = [googleBar.text stringByReplacingOccurrencesOfString:@" " withString:@"+"];
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.google.co.uk/search?q=%@", query]];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webview3 loadRequest:request];
[self.view addSubview:webview3];
[self.view bringSubviewToFront:webview3];
}
- (void)viewDidLoad {
googleBar.delegate = self;
}
Now write your code inside this method.
Upvotes: 1