2016-06-24 4 views
0

私はiOS開発の新機能で、webviewを使用してハイブリッドiOSアプリケーションを構築しようとしました。私は一緒に行って、私はここや他のウェブサイトが必要なほとんどの答えを見つけたとして私はかなり学んでいるが、私は固まっているポイントに来ている。別のViewControllerのボタンでWKWebview URLを変更してください

私はメインのViewControllerで私のwebviewセットアップを持っています。ライブラリを使ってサイドメニューを作成しています。しかし、ライブラリが動作するため、メニューは別のViewControllerに組み込まれています。私はwebviewにロードされるリンクをサイドメニューに設定したい。これは可能ですか?

編集:私はおそらく、これをすべて迅速に実行しているとも言えるはずです。

EDIT2は:ここで私はサイドメニューのために使用しているフレームワークへのリンクです:https://github.com/jonkykong/SideMenu

フレームワークはかなりあなたがテーブルビューを使用して独自のビューを構築することができます。

+0

使用しているフレームワークとそのドキュメントの場所を教えてください。 デリゲートプロトコルや通知を使用することが可能です。 – FelixSFD

+0

@FelixSFDクイック返信をお寄せいただきありがとうございます。私は使用しているサイドメニューフレームワークへのリンクを含めるように質問を編集しました。 –

答えて

0

質問が正しく理解されているかどうかわかりません。あなたの質問について私が思ったのは以下の通りです:

MenuViewControllerにはメニューがあり、セルが選択されたときにMainViewControllerのwebviewがリンクURLを読み込みますが、このURLはMenuViewControllerに配置されていますか?

これはあなたの質問であれば、あなたはこれらのソリューションとして、それを解決することができますが:

方法:UPDATEを

MainViewController通知オブザーバを追加し、MenuViewControllerは

// MainViewController 
class ViewController: UIViewController { 
let webView = UIWebView() 

override func viewDidLoad() { 
    super.viewDidLoad() 
    webView.frame = self.view.bounds 
    self.view.addSubview(webView) 

    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ViewController.handleNotification(_:)), name: "OpenURL", object: nil) 
} 

func handleNotification(notification:NSNotification) { 
    if let url = notification.userInfo?["url"] { 
     webView.loadRequest(NSURLRequest(URL: url as! NSURL)) 
    } 
} 
} 

// MenuViewController 
class MenuViewController: UIViewController, UITableViewDelegate { 
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
    let userInfo = ["url" : your_url] 
    NSNotificationCenter.defaultCenter().postNotificationName("OpenURL", object: nil, userInfo: userInfo) 
} 
} 

通知を投稿UITableViewを使用する

class MenuViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 
let tableView = UITableView(frame: CGRectZero, style: .Plain) 
let urls = ["https://google.com", "https://youtube.com", "http://stackoverflow.com/"] 
let cellIdentifier = "CellIdentifier" 

override func viewDidLoad() { 
    super.viewDidLoad() 

    tableView.delegate = self 
    tableView.dataSource = self 
    tableView.frame = view.bounds 
    tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: cellIdentifier) 

    view.addSubview(tableView) 
} 

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return urls.count 
} 

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) 
    cell.textLabel?.text = urls[indexPath.row] 
    return cell 
} 


func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
    let url = NSURL(string: urls[indexPath.row])! 
    let userInfo = ["url" : url] 
    NSNotificationCenter.defaultCenter().postNotificationName("OpenURL", object: nil, userInfo: userInfo) 
} 
} 
+0

はい、それは私の質問です。私はすぐにこれを試しましたが、サイド・メニューを開くためにボタンをクリックすると、このエラーが発生します。 "libC++ abi.dylib:NSException型のキャッチされていない例外で終了します。 これは、通常、IBの店舗は接続されていないが、すべてがうまく見えることがわかっている。 –

+0

コード内のIBOutletまたはIBActionを削除したり、名前を変更したりすることはできますが、xibファイルは残っている可能性があります。 – Yahoho

+0

私は、ストーリーボード内の私のテーブルビューのクラスとしてMenuViewControllerを設定することができません。クラスにUITableViewControllerがコードにないためですか? –

0

あなたがnotifications

ファーストでこれを解決することができ、あなたは、メニュー項目が表示されているのtableViewためUITableViewControllerを作成する必要があります。 (メニュー項目をプログラムで設定する必要がない場合は、プログラムでこれを行うことをお勧めします)

\[tableView(_:didSelectRowAtIndexPath:)\]メソッドでは、選択された項目の情報を含む通知を送信する必要があります。

例:WKWebViewが含まれているあなたのビューコントローラでは、

let kMenuItemSelectedNotification = "MenuItemSelected" //this should be a global constant. It identifies the notification 

let infoDictionary = ["menuItem":"item1"] //a dictionary containing all information you need in your VC to change the WebView 

NSNotificationCenter.defaultCenter().postNotificationName(kMenuItemSelectedNotification, object:nil, userInfo:infoDictionary) 

、あなたはviewDidLoadに通知のオブザーバを追加する必要があります。

NSNotificationCenter.defaultCenter().addObserver(self, selector: "selector for your method:", name: kMenuItemSelectedNotification, object: nil) 

セレクタで呼び出すメソッドは、WebViewのコンテンツの変更を処理します。

まだ通知に精通していない場合は、thisチュートリアルをご覧ください。通知の呼び出しと受信方法、および送信された情報を処理する方法について説明しています。userInfo

+0

ありがとう。私はすでにストーリーボードにテーブルビューを作成しましたので、プログラムでそれを作成します。通知チュートリアルも便利です。 –

関連する問題