2016-06-24 2 views
0

저는 iOS 개발에 익숙하며 webview를 사용하여 하이브리드 iOS 응용 프로그램을 만들려고합니다. 나는 나가 나가 여기에서 필요로 한 대부분의 응답을 또는 다른 웹 사이트 찾아 냈다 그러나 나가 붙어 있던 점에왔다.다른 ViewController의 버튼으로 WKWebview URL 변경

메인 ViewController에서 모든 웹 페이지를로드 할 수 있습니다. 라이브러리를 사용하여 훌륭한 메뉴를 만듭니다. 그러나 라이브러리가 작동하는 방식 때문에 메뉴는 다른 ViewController에 내장되어 있습니다. webview에로드 될 링크가있는 사이드 메뉴를 채우고 싶습니다. 이것이 가능한가?

편집 : 내가이 모든 것을 신속하게 처리하고 있음을 언급해야합니다.

EDIT2 : https://github.com/jonkykong/SideMenu

프레임 워크가 꽤 많이 당신이있는 tableview를 사용하여 자신의 뷰를 구축 할 수 있습니다 : 여기 사이드 메뉴에 대한 사용하고 프레임 워크에 대한 링크입니다.

+0

의 정보를 처리하는 방법에 대해 설명합니다. 사용중인 프레임 워크와 설명서의 위치는 무엇입니까? Delegate-Protocols 또는 알림을 사용하여 가능해야합니다. – FelixSFD

+0

@FelixSFD 빠른 답장을 보내 주셔서 감사합니다. 내가 사용하고있는 사이드 메뉴 프레임 워크에 대한 링크를 포함하도록 질문을 편집했습니다. –

답변

0

질문을 올바르게 이해했는지 모르겠다. 다음은 귀하의 질문에 대해 생각한 것입니다 :

MenuViewController는 메뉴 목록 인 셀의 셀 목록을 선택하면 MainViewController의 webview가 링크 URL을로드하지만이 URL은 MenuViewController에 배치됩니다. 이 질문 인 경우

, 당신은이 솔루션으로 해결할 수 있습니다

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) 
} 
} 

UPDATE 게시 방법 :

을 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

스토리 보드의 내 tableview 클래스로 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

감사합니다. 스토리 보드에 이미 tableview를 만들었으므로 프로그래밍 방식으로 작성하려고합니다. 알림 자습서 또한 유용합니다. –

관련 문제