I am attempting to open links embedded in a custom table view cell using a UIWebView in the same scene. However, I when I tap the button the webview doesn't open in response. The button is working (it will print an NSLog), and the webview will show if I set it visible in ViewDidLoad in the "HouseItemsViewController."
The Table View contains my custom cells, and I am trying to show the highlighted View:
Imgur image of my "House Items View Controller" Scene
The HouseItemsViewController contains a method to accept a NSURL:
- (void)displayWebViewWithURL:(NSURL *)targetURL
{
NSURL *url = targetURL;
NSURLRequest *myrequest = [NSURLRequest requestWithURL:url];
[webView loadRequest:myrequest];
theURLView.hidden=NO;
}
The custom table view cell includes an action:
- (IBAction)congressUrlLink:(id)sender
{
HouseItemsViewController* billViewController = (HouseItemsViewController*)self.tapHandler;
[billViewController displayWebViewWithURL:congressUrl];
}
Where tapHandler is defined:
#property (nonatomic, copy) void (^tapHandler)(id sender);
What's the right way to reference the HouseItemsViewController from here and to execute the displayWebViewWithURL:(NSURL *)targetURL method?[
I would assign the tapHandler manually as a delegate.
#property (nonatomic, weak) HouseItemsViewController *delegate;
synthesize it, and assign it manually after initializing your table view cell:
[myCell setDelegate:self];
Hope this helps
Related
I have a navigation controller with multiple cells. I want to link each of the cells to a webpage in a web view. However, I don't want to create another view controller for each cell. Is it possible to create 1 view controller and link all cells to the same view controller but make a different webpage show up in the web view for each cell? For example, there is cell 1 and cell 2. They both link to one view controller with a web view in the view controller. When you click cell 1, it brings you to the view controller and in the web view it brings you to http://example.com. When you click cell 2, it brings you to the same view controller as cell 1 but in the web view, it brings you to http://example.com/1. How can I do that?
Just declare a property in your viewcontroller which holds the URL you want to request.
#property (strong,nonatomic) NSString *url;
When you click on a cell assign the URL of the cell accordingly to the property of your viewcontroller
Your viewcontroller should look something like this.
Header
#import <UIKit/UIKit.h>
#interface RSViewController : UIViewController
#property (weak, nonatomic) NSString *url;
#end
Implementation
#import "RSViewController.h"
#interface RSViewController ()<UIWebViewDelegate>
#property (strong,nonatomic) IBOutlet UIWebView *webview;
#end
#implementation RSViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.webview.delegate = self;
NSURLRequest *urlRequest = [[NSURLRequest alloc]initWithURL:[NSURL URLWithString:self.url]];
[self.webview loadRequest:urlRequest];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)webViewDidStartLoad:(UIWebView *)webView
{
NSLog(#"Did start loading");
}
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
NSLog(#"Did finish loading");
}
#end
Now you just need a reference to your viewcontroller in your tableviewcontrollerand set the urlproperty on cell click.
I've a problem with some label. I made a public method that takes information from a table view and shows this info in 3 label, a text view and it load a pic from the web. I setup all the variable in the table view didSelectRowAtIndexPath method, but I'm having some issue to display that info. I made so:
in the table view controller I called the other method so:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
self.detailCharacterViewController = [[DetailCharacterViewController alloc] init];
[self.detailCharacterViewController setupDetailViewControllerWithName:[self.arrayCharacters[indexPath.row] objectForKey:#"name"] andSurname:[self.arrayCharacters[indexPath.row] objectForKey:#"surname"] andKind:[self.arrayCharacters[indexPath.row] objectForKey:#"kind"] andDescription:[self.arrayCharacters[indexPath.row] objectForKey:#"description"] andImage:[self.arrayCharacters[indexPath.row] objectForKey:#"URLpic"]];
}
and I implemented the method so:
- (void)setupDetailViewControllerWithName:(NSString*)name andSurname:(NSString *)surname andKind:(NSString*) kind andDescription:(NSString*)description andImage:(NSString*)url {
NSLog(#"name = %#", name);
self.labelName.text = name;
self.labelSurname.text = surname;
self.labelKind.text = kind;
self.textDescription.text = description;
NSURL *urlPic = [NSURL URLWithString:url];
NSData *dataPic = [NSData dataWithContentsOfURL:urlPic];
[self.imagePic setImage:[UIImage imageWithData:dataPic]];
}
If I look to the log i see the right things, but if I look to the GUI on iPhone or iPhone simulator it will remain blank. What's wrong with this code?
Thank you for the suggestion.
#Alex Blundell
#import <UIKit/UIKit.h>
#interface DetailCharacterViewController : UIViewController
#property (weak, nonatomic) IBOutlet UILabel *labelName;
#property (weak, nonatomic) IBOutlet UILabel *labelSurname;
#property (weak, nonatomic) IBOutlet UILabel *labelKind;
#property (weak, nonatomic) IBOutlet UIImageView *imagePic;
#property (weak, nonatomic) IBOutlet UITextView *textDescription;
- (void)setupDetailViewControllerWithName:(NSString*)name andSurname:(NSString *)surname andKind:(NSString*) kind andDescription:(NSString*)description andImage:(NSString*)url;
#end
The problem is that your DetailCharacterViewController's view is not loaded yet when you call setupDetailViewControllerWithName:..., so none of the outlets are connected (yet). Instantiating a view controller via alloc-init does not load its view automatically.
You could force loading the view by calling [self view] at the beginning of your setup method. This works because accessing the view property will automatically load the view if it wasn't loaded before.
Alternatively, you could make the name, surname, etc. NSString properties of the view controller and set the corresponding labels in viewDidLoad.
Are you using Interface Builder/Storyboards to design your interface? Have you made sure to connect the IBOutlets for each UITextField/View?
You should identify the objects in the header file on your view controller class, and ensure they're prefixed by IBOutlet for them to appear in Interface Builders connections pane. E.g.
IBOutlet UITextField labelName;
[...]
Then you need to go to IB and go to the connections pane for the view controller to connect each text field/view.
This problem happened to me as well.
It has to do with the fact that iOS actually hooks up your outlets really really late.
If you put a breakpoint in your setupDetailViewControllerWithName: and check your IBOutlet variables, they will be nil. That's why you are seeing a blank view. (The good old nil erroring)
The work-around I will do is to declare properties in the DetailCharacterViewController class. (Such as #property (copy, nonatomic) NSString* name).
And in the place where you are calling setupDetailViewControllerWithName:, set those properties instead such as:
self.detailCharacterViewController.name = name;
Finally in your DetailCharacterViewController's viewDidLoad:, you set the values of the labels using the saved properties. (The outlets are for sure hooked up then).
(By the way, this problem exists for prepareForSegue: as well. The destination view controller's outlets are all still nil in prepareForSegue:. So you cannot set them directly there yet)
I'm trying to call Sharekit with a UIButton, but when I tap the button running on my device, nothing happens.
This should be the relevant code in my view controller's .h
#import "SHK.h"
#import "SHKFacebook.h"
#interface ViewController : UIViewController <CaptureManagerDelegate>
{
IBOutlet UIButton *upload;
}
#property (nonatomic, retain) IBOutlet UIButton *upload;
-(IBAction)buttonPressed:(id)sender;
And in my view controller's .m (using code from example here http://gazapps.com/wp/2011/07/17/basic-sharekit-customization)
#synthesize upload;
-(IBAction)buttonPressed:(id)sender {
SHKItem *item;
NSURL *url = [NSURL URLWithString:#"http://www.gazapps.com"];
item = [SHKItem URL:url title:#"Checkout this site"];
[SHKFacebook shareItem:item];
}
-(void)dealloc
{
[upload release];
}
Besides trying to use a UIButton instead of UIBarButton, I've followed Sharekit's install instructions to the letter. In my storyboard, I CTRL-dragged to the "first responder" and linked up my button to buttonPressed. I'm not getting any warnings or errors in xcode. But nothing happens when I tap the button.
You code seems fine. The problem is probably within the nib file. Check your connections again.
Are you doing anything else with your upload (UIButton) property? You don't need that Outlet at all, if you only want to trigger one IBAction.
You could put a breakpoint in your buttonPressed method, to see if the connections are setup correctly.
you should use SHKActionSheet
SHKActionSheet *actionSheet = [EDShareActionSheet actionSheetForItem:item];
[actionSheet showFromToolbar:self.navigationController.toolbar];
I have a UIWebView object declared in my view controller and I want to make it load a different page when the app recieves a notification while the app is running using application:didRecieveRemoteNotification:. I'm having trouble accessing the object from inside the appDelegate.
Any simple way to do this?
this is how I declare my web view
.h
IBOutlet UIWebView *webView;
#property (nonatomic, retain) UIWebView *webView;
.m
#synthisize webView;
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:currentPath]]];
this is not the best idea to show the web view from outside the controller. Just add method like this to controller:
-(void) loadFromPath:(NSString*) path {
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath: path]]];
}
and call it in your app delegate when you need.
I have a tabBar application. In the first tab, I have a navigation bar. In this navigation bar I have a table view. When the user clicks a cell of the table, it goes to another table view, and when the user clicks a cell in that table, it goes to a webview to open a web page.
Basically, it goes fine until opening the webview. In the webview, viewDidLoad is called and seems to work properly. However, webViewDidStartLoad is never called and the web page never shows up.
I am not using IB. I build the UITabBarController in the AppDelegate, where I also assign an instance of my UINavigationController for each tab.
I call the webview from the second UITableViewController as follows:
rssWebViewController = [[webViews objectAtIndex: indexPath.row] objectForKey:#"controller"];
[[self navigationController] pushViewController:rssWebViewController animated:YES];
I have checked that the navigationController is there and it seems just fine.
The viewDidload of my webview is as follows:
- (void)viewDidLoad {
[super viewDidLoad];
NSString *urlAddress = self.storyUrl;
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[[self rssWebView] setDelegate: self];
[[self view] addSubview:[self rssWebView]];
[rssWebView loadRequest:requestObj];
self.rssWebView.scalesPageToFit = YES;
self.rssWebView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
}
The web view controller interface is defined as follows:
#interface RSSWebViewController : UIViewController <UIWebViewDelegate>{
IBOutlet UIWebView *rssWebView;
IBOutlet NSString *storyUrl;
IBOutlet NSString *feedName;
}
#property (nonatomic, retain) IBOutlet UIWebView *rssWebView;
#property (nonatomic, retain) IBOutlet NSString *storyUrl;
#property (nonatomic, retain) IBOutlet NSString *feedName;
#end
Any help is greatly appreciated.
ok, I found out, I had to initiate my webview with a frame in viewDidLoad:
rssWebView = [[UIWebView alloc] initWithFrame:self.view.bounds];