2016-09-12 22 views
18

Tôi đang sử dụng mã sau để điều hướng theo chương trình đến một ViewController khác. Nó hoạt động tốt, nhưng nó một số cách ẩn navigation bar. Làm cách nào để khắc phục sự cố này? (thanh điều hướng được tạo ra bằng cách nhúng các ViewController trong navigation controller nếu có vấn đề.)Swift lập trình điều hướng đến một bộ điều khiển chế độ xem khác/cảnh

let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil) 

let nextViewController = storyBoard.instantiateViewControllerWithIdentifier("nextView") as NextViewController 
self.presentViewController(nextViewController, animated:true, completion:nil) 

Trả lời

54

Trong Swift 3

Nếu bạn muốn điều hướng đến điều khiển tạo lập trình, sau đó làm điều này:

let newViewController = NewViewController() 
self.navigationController?.pushViewController(newViewController, animated: true) 

Nếu bạn muốn điều hướng đến Bộ điều khiển trên StoryBoard bằng Mã định danh " newViewController", sau đó làm điều này:

let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil) 
let newViewController = storyBoard.instantiateViewController(withIdentifier: "newViewController") as! NewViewController 
     self.present(newViewController, animated: true, completion: nil) 
+0

" as! NewViewController "là không cần thiết trong tùy chọn storyboard –

+0

yeh tôi biết đó là tùy chọn, nhưng nếu chúng tôi hiển thị thì nó trở nên rõ ràng mà viewcontroller là điểm đến cho nhà phát triển khác –

8

Bạn nên đẩy viewController mới bằng cách sử dụng điều khiển chuyển hướng hiện tại, không có mặt.

self.navigationController.pushViewController(nextViewController, animated: true) 
1

Vì vậy, nếu bạn trình bày bộ điều khiển chế độ xem, bộ điều khiển sẽ không hiển thị trong bộ điều khiển điều hướng. Nó sẽ chỉ mất màn hình hoàn chỉnh. Đối với trường hợp này, bạn phải tạo một bộ điều khiển điều hướng khác và thêm nextViewController của bạn làm gốc cho điều này và trình bày điều hướng mới nàyController.

Một cách khác là chỉ cần đẩy trình điều khiển chế độ xem.

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

Để biết thêm thông kiểm tra giấy tờ của Apple: - https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIViewController_Class/#//apple_ref/doc/uid/TP40006926-CH3-SW96

+0

'presentViewController' đã được đổi tên thành' hiện nay (loginController, hoạt ảnh: true, completion: nil) ' –

0
 OperationQueue.main.addOperation { 

        let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil) 
        let newViewController = storyBoard.instantiateViewController(withIdentifier: "Storyboard ID") as! NewViewController 
         self.present(newViewController, animated: true, completion: nil) 

        } 

Nó làm việc cho tôi đặt bên trong OperationQueue.main.addOperation, mà sẽ thực hiện trong thread chính

Các vấn đề liên quan