2017-09-29 15 views
4

을 대체, 내 Main.storyboard 년에는 스위프트 4 분할보기 컨트롤러 세부 난 그냥 응용 프로그램을 구축하기 시작하고 지금 나는 2 분할 뷰 컨트롤러를 추가하고 마스터

enter image description here

내가 추가 이미지처럼 보인다 내 마스터 코드를 다음

import UIKit 

class ContactsDetail: UIViewController { 

    @IBOutlet weak var detailDescriptionLabel: UILabel! 


    func configureView() { 
     // Update the user interface for the detail item. 
     if let detail = detailItem { 
      if let label = detailDescriptionLabel { 
       label.text = detail.description 
      } 
     } 
    } 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 
     configureView() 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

    var detailItem: NSDate? { 
     didSet { 
      // Update the view. 
      configureView() 
     } 
    } 


} 
,536,913 : 여기

import UIKit 

class ContactsMaster: UITableViewController { 

    var ContactsDetailController: ContactsDetail? = nil 
    var objects = [Any]() 


    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 
     navigationItem.leftBarButtonItem = editButtonItem 

     let addButton = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(insertNewObject(_:))) 
     navigationItem.rightBarButtonItem = addButton 
     if let split = splitViewController { 
      let controllers = split.viewControllers 
      ContactsDetailController = (controllers[controllers.count-1] as! UINavigationController).topViewController as? ContactsDetail 
     } 
    } 

    override func viewWillAppear(_ animated: Bool) { 
     clearsSelectionOnViewWillAppear = splitViewController!.isCollapsed 
     super.viewWillAppear(animated) 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

    @objc 
    func insertNewObject(_ sender: Any) { 
     objects.insert(NSDate(), at: 0) 
     let indexPath = IndexPath(row: 0, section: 0) 
     tableView.insertRows(at: [indexPath], with: .automatic) 
    } 

    // MARK: - Segues 

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
     if segue.identifier == "showContactDetail" { 
      if let indexPath = tableView.indexPathForSelectedRow { 
       let object = objects[indexPath.row] as! NSDate 
       let controller = (segue.destination as! UINavigationController).topViewController as! ContactsDetail 
       controller.detailItem = object 
       controller.navigationItem.leftBarButtonItem = splitViewController?.displayModeButtonItem 
       controller.navigationItem.leftItemsSupplementBackButton = true 
      } 
     } 
    } 

    // MARK: - Table View 

    override func numberOfSections(in tableView: UITableView) -> Int { 
     return 1 
    } 

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

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) 

     let object = objects[indexPath.row] as! NSDate 
     cell.textLabel!.text = object.description 
     return cell 
    } 

    override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool { 
     // Return false if you do not want the specified item to be editable. 
     return true 
    } 

    override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { 
     if editingStyle == .delete { 
      objects.remove(at: indexPath.row) 
      tableView.deleteRows(at: [indexPath], with: .fade) 
     } else if editingStyle == .insert { 
      // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view. 
     } 
    } 


} 

그리고 나의 상세이다

내 문제는 내 애플 리케이션을 실행하고 분할보기 컨트롤러로 이동하고 마스터에서 항목을 선택하면, 그것은 세부 사항을 이동하지 않지만 대신 마스터를 대체합니다.

저는 Split View Controller 인 샘플 응용 프로그램을 가지고 있으며 샘플 응용 프로그램의 App Delegate 파일에서 응용 프로그램에이 코드가 있음을 알았습니다 (_ 응용 프로그램 : UIApplication, didFinishLaunchingWithOptions launchOptions : [UIApplicationLaunchOptionsKey : Any]?) -> BOOL 방법 :

let splitViewController = window!.rootViewController as! UISplitViewController 
     let navigationController = splitViewController.viewControllers[splitViewController.viewControllers.count-1] as! UINavigationController 
     navigationController.topViewController!.navigationItem.leftBarButtonItem = splitViewController.displayModeButtonItem 
     splitViewController.delegate = self 

그리고이 방법도 있습니다 :

func splitViewController(_ splitViewController: UISplitViewController, collapseSecondary secondaryViewController:UIViewController, onto primaryViewController:UIViewController) -> Bool { 
     guard let secondaryAsNavController = secondaryViewController as? UINavigationController else { return false } 
     guard let topAsDetailController = secondaryAsNavController.topViewController as? DetailViewController else { return false } 
     if topAsDetailController.detailItem == nil { 
      // Return true to indicate that we have handled the collapse by doing nothing; the secondary controller will be discarded. 
      return true 
     } 
     return false 
    } 

이 코드 내 문제는 내 분할보기 컨트롤러가 inital 컨트롤러와 splitViewControlle 내 문제가되지 않는 것입니다 r 메서드를 사용하면 두 개의 뷰 컨트롤러가 분리되어 있으며 그 중 하나만 지정할 수 있습니다. inital 컨트롤러를 만들지 않고 분할 뷰 컨트롤러를 얻으려면 어떻게해야합니까?

답변

0

마스터 클래스는 UISplitViewControllerDelegate를 구현해야합니다.

class ContactsMaster: UITableViewController,UISplitViewControllerDelegate { 

및 마스터에서이 함수를 오버라이드 (override) :

의 viewDidLoad (마스터 클래스) 추가에 다음
func splitViewController(splitViewController: UISplitViewController, collapseSecondaryViewController secondaryViewController: UIViewController, ontoPrimaryViewController primaryViewController: UIViewController) -> Bool { 
return true  
} 

다음 cods :

self.splitViewController!.delegate = self; 

self.splitViewController!.preferredDisplayMode = UISplitViewControllerDisplayMode.AllVisible 

self.extendedLayoutIncludesOpaqueBars = true 

I 그래서 제일 먼저 당신이해야 할 splitviewcontroller를 구성하기위한 많은 단계를 건너 뛰었다고 생각하십시오. 다음과 같이 작성된 많은 자습서 중 하나를 읽을 수있는 모든 방법을 이해하려면 :

012 스토리 보드에 상세 VC에 대한 ContactsDetail 클래스를 설정하는 것을 잊지

http://nshipster.com/uisplitviewcontroller/

0

3,516,우연히 당신을 했습니까?