Accessing the object of another class with - ios

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];
}

Related

How to show multiple web view links on Xcode under one UIViewController

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

How to open url from table cells in webview iOS

I have just figured out how to make a tabbed navigation controller open in two table views. I would like to be able to click on one of the table cells (6 on a page), and for this to open the url in a webview.
This is my code:
#import "FlashTopicsEViewController.h"
#import "ADVTheme.h"
#implementation FlashTopicsEViewController
#synthesize webView;
NSArray *flashtopicsEth;
NSArray *urlLinks;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.;
self.view.backgroundColor = [ADVTheme viewBackgroundColor];
self.view.tintColor = [UIColor whiteColor];
flashtopicsEth = [NSArray arrayWithObjects:#"Topic1",#"Topic2", nil];
urlLinks = [[NSArray alloc] initwithObjects:#"http://url1.com", #"http://url2.com",nil];
webView.delegate = self;
NSURL *URL = [NSURL URLWithString: urlLinks[indexPath.row]];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:URL];
[webView loadRequest:requestObj];
}
and this is the code below it:
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"FlashTopicECell";
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:simpleTableIdentifier];
}
UIImage *image1 = [UIImage imageNamed:#"flashImage.png"];
cell.imageView.image = image1;
cell.textLabel.text = [flashtopicsEth objectAtIndex:indexPath.row];
return cell;
}
and this is what I have in my .h file
#import <UIKit/UIKit.h>
#interface FlashTopicsEViewController : UIViewController
#property NSArray *urlLinks;
#property NSArray *flashtopicsEth;
#property (nonatomic,strong) IBOutlet UIWebView *webView;
#end
#matt is right, this is beyond trivial to do lol :) But anyway, what you need to do is to make use of one of UITableView's delegate methods called "didSelectRowAtIndexPath". This will detect when a cell is tapped:
Your question is very basic and not detailed, so for the purposes of this basic answer, I am going to declare a UIWebView in the header file and then use that. You will need to add it to your ViewController and link it up. I also do NOT know how you are storing your URLs, I presume you are using an array? Well for the purposes of this answer, I will too.
IBOutlet UIWebView *websiteView;
NSArray *urlLinks;
Now on to the implementation code:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Get the cell number that was tapped and then
// use that to access your array of URLs.
// Then pass that URL to a UIWebView.
NSURL *URL = [NSURL URLWithString:urlLinks[indexPath.row]];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:URL];
[websiteView loadRequest:requestObj];
}
Also in the viewDidLoad, set the webview delegate like so:
websiteView.delegate = self;
Update - In answer to your comments
Ok so in my answer I just assumed you are using an array for the list of URLs. But there are other ways too. You don't have to use an array if you don't want to.
As to how you populate the array? Well that depends on quite a few things:
1) Are you downloading a list of URLs from a server?
2) Are you setting the list of URLs in your code?
If you are doing option 1, then you are probably parsing something like a JSON file which you are downloading from a server. In this case you may want to use something a long the lines of:
urlLinks = [[feed objectForKey:#"items"] valueForKey:#"url"];
The above code will put all the url links into your array. (This is just a very tiny snippet - not the whole code - just to give you an idea).
If you are going with option 2, so you already know the list of URLs and you want to define them in your app, then just do this:
NSArray *urlLinks = [[NSArray alloc] initwithObjects:#"http://url1.com", #"http://url2.com", ...etc.... , nil];
Right so to then access the URLs, you need to basically get the string from the correct array object and then turn that into an NSURL and then pass that to your webview like so:
NSURL *URL = [NSURL URLWithString:urlLinks[indexPath.row]];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:URL];
[websiteView loadRequest:requestObj];
Lastly, how you present your UIWebView is up to you. It doesn't have to be in a seperate ViewController if you dont want it to. Thats up to you. But if you do, then yes, the next bit is to present the next view controller on screen. Just remember you will have to pass your URL to it, if you want the UIWebView in that view controller to present your website.
Update 2 - in answer to your question/code
Ok there are a few things wrong with your code:
Get rid of the "etc...." bit in your array declaration.... Lol I only added that to help you understand how to populate the array. So it should really be:
flashtopicsEth = [NSArray arrayWithObjects:#"Topic1",#"Topic2", nil];
You have just randomly inserted your array code in your implementation (.m) file.... that's not where you put it. You can insert it in methods such as the viewDidLoad or other methods that you make yourself. You can't just randomly add it wherever you feel like. So for the purposes of keeping things simple, lets start with just assigning the arrays in the viewDidLoad like so:
-(void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
flashtopicsEth = [NSArray arrayWithObjects:#"Topic1", #"Topic2", nil];
urlLinks = [[NSArray alloc] initwithObjects:#"http://url1.com",#"http://url2.com", nil];
webView.delegate = self;
NSURL *URL = [NSURL URLWithString: urlLinks[indexPath.row]];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:URL];
[webView loadRequest:requestObj];
}
Declare your NSArray's in your header file and assign them in the implementation (.m) file. Don't declare them in the viewDidLoad because they will then be local to the viewDidLoad and the other UITableView methods will not be able to access them unless your pass the array to them. For the purposes of keeping things simple (as you are a beginner), just declare them like this in your header (.h) file:
#import <UIKit/UIKit.h>
#interface FlashTopicsEViewController : UIViewController {
NSArray *flashtopicsEth;
NSArray *urlLinks;
}
#property (nonatomic,strong) IBOutlet UIWebView *webView;
#end

UIWebView doesn't appear in DetailViewController when called from a button inside a Popover

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!

Add a custom UIWebView using Phonegap

I have an iOS app that needs OAuth authentication of a Facebook account, but I have to use my own server for the API calls.
So I want to open a FBLogin screen (UIWebView), with a login URL, when a certain button is clicked.
I have the following code:
FBWebViewController.h
#import <UIKit/UIKit.h>
#interface FBWebViewController : UIViewController <UIWebViewDelegate>
- (void)initFBWebView;
#end
FBWebViewController.m
- (void)initFBWebView {
NSLog(#"DetailViewController->initUIWebView {");
UIWebView *aWebView;
// init and create the UIWebView
aWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 400)];
aWebView.autoresizesSubviews = YES;
aWebView.autoresizingMask=(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth);
//set the web view delegates for the web view to be itself
[aWebView setDelegate:self];
//Set the URL to go to for your UIWebView
NSString *urlAddress = #"http://www.google.com";
//Create a URL object.
NSURL *url = [NSURL URLWithString:urlAddress];
//URL Requst Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
//load the URL into the web view.
[aWebView loadRequest:requestObj];
//add the web view to the content view
[self.view addSubview:aWebView];
}
and in my Authorize.m file, which handles all the API calls etc.
- (void)fbLogin:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options {
FBWebViewController *webview = [[FBWebViewController alloc] init];
[webview view];
[webview initFBWebView];
}
Nothing happens. No WebView is opened. I put some debug statements in the initWithFbWebView method, and the method is called and run until the end, but no screen is showing up.
What am I doing wrong?

iPad SplitView detailView default load

I have an iPad split-view app. The detailview is a webview. In which method, can I get a default page loaded when the app launch before the user clicks on an item in the table view?
IN detailviewcotroller.h , create outlet as follows,
IBOutlet UIWebView *WEBVIEW;
connect outlet in xib by drag n droping uiwebview control.
In your detailviewcontroller.m's viewdidload method u can write as follows-
NSString *str=#"yr default webpage link here";
NSURL *url=[NSURL URLWithString:str];
NSURLRequest *req=[NSURLRequest requestWithURL:url];
[WEBVIEW loadRequest:req];
Just a good advice, by creating Outlets, when you synthesize do:
#synthesize webView = _webView;
and use :
[_webView loadRequest:req];
for loading your request.

Resources