Swift: Triggering Tableviewcell to Lead to a Link in a Uiwebview in Another Viewcontroller

Swift: Triggering TableViewCell to lead to a link in a UIWebView in another ViewController

You should not be using dequeueReusableCellWithIdentifier to create your next view controller. You should use the storyboard's instantiateViewControllerWithIdentifier to create the instance: Set the "Storyboard ID" for the viewController in your storyboard

Storyboard image

If you set it to, for example, "WebViewController" you can then create the instance with:

let myWebView = self.storyboard!.instantiateViewControllerWithIdentifier("WebViewController") as! LinkViewViewController

My recommendation would be to give the LinkViewViewController its own URL property,

var url : NSURL?

and to pass the URL after creating the instance:

myWebView.url = url

and then present the new VC:

self.presentViewController(myWebView, animated: true, completion: nil)

In the viewDidLoad of LinkViewViewController, load the URL into the webView:

let URLRequest = NSURLRequest(self.url)
self.webView.loadRequest(URLRequest)

(Typed without testing; please forgive any typos/errors with optionals etc).

As a final note, this method uses the didSelectRow method to create the view controller instance and pass the URL. Another option would be to use a segue by ctrl-dragging from the prototype cell to the view controller. You then need to implement similar code in prepareForSegue method to set the url.

Handle selection of custom CallOutView swift

Have you tried using instantiateViewControllerWithIdentifier? You can trigger this when tapping a button, in the viewDidLoad or in whatever other method you want it to trigger and push a new a viewController.

Firstly, set the "Storyboard ID" for the viewController in your storyboard. Like this:

Sample Image

Example:

Here's an example of how you can push to a new viewController when tapping a cell. As mentioned you can use this method in different functions to push to another viewController :

  override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

let yourNewView = self.storyboard!.instantiateViewControllerWithIdentifier("yourStoryBoardID") as! ViewControllerYourePushingTo

self.presentViewController(yourNewView, animated: true, completion: nil)

}

In your case this is in the hitTest func.

I asked a similar question not too long ago. Here's the reference: Swift: Triggering TableViewCell to lead to a link in a UIWebView in another ViewController

Here's the link from Apple Dev: InstantiateViewControllerWithIdentifier

Swift after signup move to 2nd or any other tab of UITabBarController

Using instantiateViewControllerWithIdentifier is the right way to go. However, you need to add the actual identifier, and the UIViewController Custom class type needs to be the actual class.

I'm going to give you a rough example of what it looks like, but it should work. Either way you have to edit your instantiate method as I explain below.

Steps:

Firstly, set the "Storyboard ID" for the viewController in your storyboard. Like this:

Sample Image

Then add this code modified instantiateViewControllerWithIdentifier method:

   @IBAction func btn_login(sender: AnyObject) {
activityIndicator.progressBarDisplayer("Logging In", true, uiView: self.view)
let timezone = GLib.timezone();
let parameters = ["email": txtemail.text!,"password": txtpassword.text!,"time_zone": timezone]
GLib.fetch("www.abc.com/test/auth/login", parameters: parameters, localkey: "user"){
(isResponse) -> Void in
if(isResponse == true)
{
self.activityIndicator.strLabel.text = "Personalizing Data";

let yourNewViewController: ViewControllerClassName = self.storyboard!.instantiateViewControllerWithIdentifier("yourStoryBoardID") as! ViewControllerYourePushingTo

self.presentViewController(yourNewViewController, animated: true, completion: nil)

}else
{
self.show_alert("Attention Please!", message: self.GLib.error)
}
}

As a type, you need to actually use the viewController's name.

I asked a similar question a few weeks ago: Swift: Triggering TableViewCell to lead to a link in a UIWebView in another ViewController

Pass URL from text field to web view in second controller

Replace your second view controller with something like below

 import UIKit
import WebKit

class BrowserController: UIViewController, WKNavigationDelegate {

@IBOutlet weak var label: UILabel!
var urlString = String()
var webView : WKWebView!

override func viewDidLoad() {
super.viewDidLoad()

label.text = urlString

// Do any additional setup after loading the view.
//Load the webview
loadPage();
}

func loadPage(){
let url = NSURL(string: myBlog)
let request = NSURLRequest(URL: url!)

// init and load request in webview.
webView = WKWebView(frame: self.view.frame)
webView.navigationDelegate = self
webView.loadRequest(request)
self.view.addSubview(webView)
}

//MARK:- WKNavigationDelegate
func webView(webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: NSError) {
print(error.localizedDescription)
}

func webView(webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
print("Strat to load")
}
func webView(webView: WKWebView, didFinishNavigation navigation: WKNavigation!) {
print("finish to load")
}
}

How to intercept click on link in UITextView?

Update: From ios10,

- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange interaction:(UITextItemInteraction)interaction;

From ios7 and Later UITextView has the delegate method:

- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange *NS_DEPRECATED_IOS(7_0, 10_0, "Use textView:shouldInteractWithURL:inRange:forInteractionType: instead");*

to intercept the clicks to links. And this is the best way to do it.

For ios6 and earlier a nice way to do this is to by subclassing UIApplication and overwriting the -(BOOL)openURL:(NSURL *)url

@interface MyApplication : UIApplication {

}

@end

@implementation MyApplication

-(BOOL)openURL:(NSURL *)url{
if ([self.delegate openURL:url])
return YES;
else
return [super openURL:url];
}
@end

You will need to implement openURL: in your delegate.

Now, to have the application start with your new subclass of UIApplication, locate the file main.m in your project. In this small file that bootstraps your app, there is usually this line:

int retVal = UIApplicationMain(argc, argv, nil, nil);

The third parameter is the class name for your application. So, replacing this line for:

int retVal = UIApplicationMain(argc, argv, @"MyApplication", nil);

This did the trick for me.

Reusable UITableViewCell duplicating on scroll in Swift

You should create a custom UITableViewCell with desirable views (in your case 2 UILabels and UIWebView), load this cell from nib in cellForRowAtIndexPath and set needed values from arrays at indexPath.row appropriately. Don't create and add views in cellForRowAtIndexPath, just set values.



Related Topics



Leave a reply



Submit