Suppose I have an app with 3 buttons each opening 3 different UIWebView view controllers. Instead of having 3 separate view controllers for each button, I want to have 1 UIWebView view controllers and depending on the button that is pressed, that is what will show on the UIWebView.
This is just an example of what I am talking about
firstViewController
Button 1 opens Yahoo
Button 2 opens Google
Button 3 opens Bing
secondViewController
if button1 is pressed, show Yahoo on the UIWebView
if button2 is pressed, show google on the UIWebView
if button3 is pressed, show bing o the UIWebView
How do I come up with this?
in iOS each object contains own tags if you are interest use tags else other options.
assume that your button1.tag=10, button2.tag=20 and button3.tag=30
set the global string for in .h file
NSString *activecheck;
// assign the single method for all buttons in touchup inside method
- (IBAction)button_GetDeals:(UIButton*)sender {
switch (sender.tag)
{
case 10:
activecheck=#"1";
break;
case 20:
activecheck=#"2";
break;
case 30:
activecheck=#"3";
break;
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"secondviewcontrolleridentidiername"]) {
secondViewController *destViewController = segue.destinationViewController;
destViewController. buttontype = activecheck;
}
// this is your second view controller
#interface secondViewController : UIViewController
#property (nonatomic, strong) NSString *buttontype;
#property (strong, nonatomic) IBOutlet UIWebView *webview;
#end
in your .m file viewdidload
- (void)viewDidLoad
{
[super viewDidLoad];
if ([buttontype isEqualtoString:#"1"])
NSString *strURL = #"http://www.google.com";
NSURL *url = [NSURL URLWithString:strURL];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[self.webiew loadRequest:urlRequest];
}
just like follow the another two condition for bing and yahoo ....
Button 1 opens Yahoo
In its action
write:
[webView loadRequest:YOUR REQUEST_YAHOO];
// reload your view
Button 2 opens Google
In its action
write:
[webView loadRequest:YOUR REQUEST_GOOGLE];
// reload your view
Button 3 opens Bing
In its action
write:
[webView loadRequest:YOUR REQUEST_Bing];
// reload your view
For reload your view you can use:
[self.view setNeedsDisplay]; //such methods
Related
it's been almost a week of reading and trying all of these different solutions from this website and others .. but unfortunately nothing work ..
I'm trying to develop a News app (basically, main page and Details page) .. I want to use two different uiwebviews in two view controllers .. For example: The FirstView includes the First uiwebview .. and this uiwebview shows the page (index.htm ) which includes many different news (links).. when the user click on any link ( e.g. details.htm?Id=..) it shows that link in the Second uiwebview in the SecondView controller ...
what I need exactly is, when user click on a link on uiwebview1, it directly open the view controller2 (SecondView) and open that link on the webview2 ... I know it's by using shouldStartLoadWithRequest as ive seen many ( if not all ) examples of how to use it .. but it never worked with me .. the second view controller can never be shown ..as I also set the delegate = self ..
I really tried to do it, but as i'm new ios developer .. I really need some help to get this done ... and i would really appreciate more if someone gives me a link to download the example, not because of anything, but I sometimes find it difficult to put the code on the right place ...and this is might me my problem ...
Many thanks ...
=========
I found this solution on Within a (BOOL)webView: How to open a URL clicked in UIWebView that opens a Modal UIWebView
and after much work .. all errors disappeared .. but still cant move the second view controller .. it never shown .. I used this code :
this is in the viewcontroller.h
#property (nonatomic, strong) IBOutlet UIWebView *webView;
//- (IBAction)prepareForSegue;
#property (strong, nonatomic) NSURL *url;
#property (strong, nonatomic) NSURL *targetUrl;
and this is on the viewcontroller.m
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
//Gets the link.
if (navigationType == UIWebViewNavigationTypeLinkClicked) {
NSURL *URL = [request URL];
NSLog(#"url:%#",request); //Get's the url itself
// [self.navigationController pushViewController:SecondView animated:YES];
// [secondView release];
self.webView.delegate = self;
// ViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:#"SecondView"];
// [self.navigationController pushViewController:controller animated:YES];
//
if ([[URL scheme] isEqualToString:#"http"] ||
[[URL scheme] isEqualToString: #"https" ]) {
targetUrl = url;
[self performSegueWithIdentifier:#"SecondView" sender:self];
return NO;
}
return YES;
}
return YES;
}
and before that
#synthesize webView;
#synthesize targetUrl;
#synthesize url;
and finally :
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
NSLog(#"Source Controller = %#", [segue sourceViewController]);
NSLog(#"Destination Controller = %#", [segue destinationViewController]);
NSLog(#"Segue Identifier = %#", [segue identifier]);
if ([segue.identifier isEqualToString:#"SecondView"]) {
ViewController *wVC = [segue destinationViewController];
wVC.url = targetUrl ; // replace article.url with your url.
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)viewDidLoad {
NSURL *urla= [NSURL URLWithString:#"Index.htm"]; NSURLRequest *requestURL = [NSURLRequest requestWithURL:urla]; [webView loadRequest:requestURL];
//
I exactly copied the code above in my project .. and I believe there is a very tiny mistake ... it doesn't show my SecondView at all when I click on links from index.htm... what should I change on that code ...
Many thanks ...
Your main problem is that you don't have any segue defined in your storyboard. If you connect your two view controllers with a segue, name it "SecondView", and uncomment the following line...
self.webView.delegate = self;
...you'll get past your current problem.
(When the second controller displays, it doesn't seem to load the new URL...but that's some different problem.)
I have an iOS project with the following files inside it:
**PopOverContentViewController.h,
PopOverContentViewController.m,
MasterViewController.h,
MasterViewController.m,
DetailViewController.h,
DetailViewController.m.**
I created some buttons in PopOverViewController which have this method as their action:
- (void) buttonPressed
{
NSLog(#"The button was pressed");
UIWebView *myWebView = [[UIWebView alloc]
initWithFrame:self.detailViewController.view.bounds];
NSURL *myUrl = [NSURL URLWithString:#"http://www.lau.edu.lb"];
NSURLRequest *myRequest = [NSURLRequest requestWithURL:myUrl];
[myWebView loadRequest:myRequest];
[self.detailViewController.view addSubview:myWebView];
if ([self isInPopover])
{
[self.myPopOver dismissPopoverAnimated:YES];
}
}
The problem is that I am not seeing the webpage opening, the DetailviewController doesn't change.
Keep in mind I have the following lines written in PopOverContentViewController.h:
#class DetailViewController;
#property DetailViewController *detailViewController;
And that I have imported DetailViewController.h to the implementation file of PopOverViewController.
I tried loading a page from information in the masterviewcontroller and it appeared in the detailviewcontroller, but I'm clueless as to why it isn't working from the popover.
Thank you!
I've been searching a lot but it seems like I just can't figure it out. I'm pretty new to iOS development and I apologize if I am doing things completely wrong.
Here is the situation.
I am trying to switch the content view of my DetailViewController via button that should send you to the next text view or the same view but with different text.
This is my header file.
#import <UIKit/UIKit.h>
#interface DetailViewController : UIViewController
#property (strong, nonatomic) IBOutlet UITextView *rowContent;
#property int rowNumber;
#property (strong, nonatomic) NSString * rowName;
#property (strong, nonatomic) IBOutlet UIBarButtonItem *rightArrow;
#end
My tableViewController redirects me to my DetailViewContoller depending on which cell I've selected. Now in my DetailViewController I have another button that I want to switch to next text instead of me going back to the tableViewController and selecting a different cell.
//DetailViewController
//set the title of the view
self.title = rowName;
//set the UITextView basec on rowNumber
switch (rowNumber) {
case 0:
rowContent.text = [NSString stringWithContentsOfFile:[[NSBundle mainBundle]pathForResource:#"file" ofType:#"txt"] encoding:NSUTF8StringEncoding error:nil];
break;
case 1:
rowContent.text = [NSString stringWithContentsOfFile:[[NSBundle mainBundle]pathForResource:#"file1" ofType:#"txt"] encoding:NSUTF8StringEncoding error:nil];
break;
case 2:
rowContent.text = [NSString stringWithContentsOfFile:[[NSBundle mainBundle]pathForResource:#"file2" ofType:#"txt"] encoding:NSUTF8StringEncoding error:nil];
break;
case 3:
rowContent.text = [NSString stringWithContentsOfFile:[[NSBundle mainBundle]pathForResource:#"file3" ofType:#"txt"] encoding:NSUTF8StringEncoding error:nil];
break;
default:
break;
//TableViewController
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
//create and instance of our DetailViewController
DetailViewController * DVC = [[DetailViewController alloc]init];
//set the DVC to the destinationViewContoller
DVC = [segue destinationViewController];
//get the indexPath
NSIndexPath * path = [self.tableView indexPathForSelectedRow];
NSString * theList = [rowList objectAtIndex:path.row];
DVC.rowNumber = path.row;
DVC.rowName = theList;
}
Your question is a bit confusing. If you want to switch to the next case, from a button, just write rowNumber ++; in your button method, and then call the method where your switch statement is.
If I'm understanding the question correctly, it sounds like you're trying to do something similar to what I recently did in an app. That is, you have a table view in a navigation controller that goes to a detail view when you select a cell, and you want to then be able to navigate between different detail views without going back to the table view to select another cell.
I did this using a UIPageViewController. It is a bit complicated to implement but is a nice clean method to use. The way it works is when you tap on a cell, it goes to a view that contains a UIPageViewController which then loads the appropriate 'page' depending on which cell was tapped. The UIPageViewController handles moving back and forth between pages. You provide a model controller that acts as the UIPageViewController's data source. Its role is to return a detail view controller for a given index (page number). To get the UIPageViewController to load the appropriate data for each page, you feed it the same data you are using to populate your table view.
Say you have three cells in your table. You tap the middle one, and it loads the view with the UIPageViewController. When the cell is tapped and you're transitioning to the detail view, you tell it which cell was tapped. The UIPageViewController then uses this info to load the correct detail view. When the user wants to navigate between pages, the UIPageViewController simply loads the data for the next or previous page with the data that corresponds to that cell in the table view.
Note that UIPageViewController doesn't know or care about the table view, it is simply using the same source data (such as an array) that the table view uses to load its data.
Documentation here.
I have implemented a browser in my application by using UIWebView, by default I'm loading google page in my browser.
When I search something in the google page ,the UIWebViewDelegate's webView:shouldStartLoadWithRequest:navigationType: method is called.
The problem is when I tap on the back button from this search page no delegates are getting called, so I am having a problem disabling my back button.
This problem happens only in an iPad application not in an iPhone application.
This code may help u...
A UIWebView is a UIView that can load a web page while remaining in the user's application.
Navigation to other webpages is allowed through the use of imbedded links in a web page itself. Forward and backward navigation through history can be set up with instance methods goForward and goBack, but the programmer must supply the buttons.
The following example uses a UIWebView, and
1) adds forward and backward buttons. The buttons are enabled and highlighted using UIWebViewDelegate optional methods webViewDidStartLoad: and webViewDidFinishLoad:
2) adds a UIActivityIndicatorView which displays while the web page is loading
In the .h file for the WebViewController :
Declare the UIWebView, Optionally : add buttons to control moving forward and backward through browsing history and IBActions for pressing the buttons, Optionally again : add a UIActivityIndicatorView.
#interface WebViewController : UIViewController <UIWebViewDelegate>
{
UIWebView *webView;
UIButton *back;
UIButton *forward;
UIActivityIndicatorView *activityIndicator;
}
#property(nonatomic,retain)IBOutlet UIWebView *webView;
#property(nonatomic,retain)IBOutlet UIButton *back;
#property(nonatomic,retain)IBOutlet UIButton *forward;
#property(nonatomic,retain)IBOutlet UIActivityIndicatorView *activityIndicator;
-(IBAction)backButtonPressed: (id)sender;
-(IBAction)forwardButtonPressed: (id)sender;
#end
//In the .m file for the WebViewController
#implementation WebViewController
#synthesize webView;
#synthesize back;
#synthesize forward;
#synthesize activityIndicator;
//method for going backwards in the webpage history
-(IBAction)backButtonPressed:(id)sender {
[webView goBack];
}
//method for going forward in the webpage history
-(IBAction)forwardButtonPressed:(id)sender
{
[webView goForward];
}
//programmer defined method to load the webpage
-(void)startWebViewLoad
{
//NSString *urlAddress = #"http://www.google.com";
NSString *urlAddress = #"http://cagt.bu.edu/page/IPhone-summer2010-wiki_problemsandsolutions";
//Create a URL object.
NSURL *url = [NSURL URLWithString:urlAddress];
//URL Requst Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
//Load the request in the UIWebView.
[webView loadRequest:requestObj];
}
// acivityIndicator is set up here
- (void)viewDidLoad
{
//start an animator symbol for the webpage loading to follow
UIActivityIndicatorView *progressWheel = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
//makes activity indicator disappear when it is stopped
progressWheel.hidesWhenStopped = YES;
//used to locate position of activity indicator
progressWheel.center = CGPointMake(160, 160);
self.activityIndicator = progressWheel;
[self.view addSubview: self.activityIndicator];
[self.activityIndicator startAnimating];
[progressWheel release];
[super viewDidLoad];
//call another method to do the webpage loading
[self performSelector:#selector(startWebViewLoad) withObject:nil afterDelay:0];
}
- (void)dealloc
{
[webView release];
[back release];
[forward release];
[activityIndicator release];
[super dealloc];
}
#pragma mark UIWebViewDelegate methods
//only used here to enable or disable the back and forward buttons
- (void)webViewDidStartLoad:(UIWebView *)thisWebView
{
back.enabled = NO;
forward.enabled = NO;
}
- (void)webViewDidFinishLoad:(UIWebView *)thisWebView
{
//stop the activity indicator when done loading
[self.activityIndicator stopAnimating];
//canGoBack and canGoForward are properties which indicate if there is
//any forward or backward history
if(thisWebView.canGoBack == YES)
{
back.enabled = YES;
back.highlighted = YES;
}
if(thisWebView.canGoForward == YES)
{
forward.enabled = YES;
forward.highlighted = YES;
}
}
#end
/*****************************/
//In viewDidLoad for the class which adds the WebViewController:
WebViewController *ourWebVC = [[WebViewController alloc] initWithNibName:#"WebViewController" bundle:nil];
ourWebVC.title = #"WebView";
[self.view addSubview:ourWebVC];
//release ourWebVC somewhere else
In your case ,You have to ignore/avoid "caching data". Following lines of code may help.
NSURLRequest *requestObj = [NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.google.com"] cachePolicy: NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:10.0];
[webView loadRequest:requestObj];
In my project iPad, I have a TableView that opens in a popoverController, tableView have this particular company in each cell, which was clicking to the site clicked COMPANY,
but found means to launch the User descomodo out of going to the safari app.
As I already had one in my WebView viewController which is a site with background, I decided to make the site selected in the table open in the background of the app.
How are separate classes I need a way to access the webViewHome or simply call the function that loads the site with another parameter.
I tried to access in tableView.m as follows:
viewController *varViewController = [[viewController alloc]init];
.....
- (void)mostraSite:(NSString *)endereco{
NSURL *url = [NSURL URLWithString:endereco];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[varViewController.webViewHome loadRequest:requestObj];
}
and each index of TableView call the function with a different parameter, but does not work.
if they have something to suggest I am grateful.
explaining more clearly
In my ViewController I have a webview.
eg
# import <UIKit/UIKit.h>
# interface iPhoneStreamingPlayerViewController: UIViewController
{
...
IBOutlet UIWebView * webViewHome;
...
}
# property (nonatomic, retain) UIWebView * IBOutlet webViewHome;
...
- (Void) mostraSite: (NSString *) address;
...
# end
the ... where I is Outlets and other functions. Not posted because it's just unnecessary.
I have a TabBar Controllers other that opens in a popover.
one of the items of the TabBar is a TableView that each cell contains a link, and when an item is selected, I want to open the site in the WebView I have in the ViewController.
in TableView,
imported the viewController
didSelectRowAtIndexPath and call the function passing the parameters as index
eg
viewController varViewController * = [[viewController alloc] init];
switch (indexPath.row) {
case 0:
[self mostraSite: # "http://www.radiobento.com.br"];
break;
calling the function
- (void) mostraSite: (NSString *) address {
NSURL * url = [NSURL URLWithString: address];
NSURLRequest requestObj * = [NSURLRequest requestWithURL: url];
[varViewController.webViewHome loadRequest: requestObj];
}