Có cách nào để có bộ điều khiển chế độ xem hiện tại từ AppDelegate không? Tôi biết có rootViewController, nhưng đó không phải là những gì tôi đang tìm kiếm.Trình điều khiển chế độ xem hiện tại từ AppDelegate?
Trả lời
Nếu bạn có UINavigationController vào appdelegate sau đó sử dụng tài sản của mình topViewController hoặc visibleViewController
Lấy đối tượng appdelegate:
MyAppDelegate *tmpDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
Như berili gợi ý bạn có thể sử dụng thuộc tính của UINavigationController để truy cập điều khiển xem hiện tại của bạn.
Vì vậy, các mã sẽ trông như thế:
id myCurrentController = tmpDelegate.myNavigationController.topViewController;
hay:
NSArray *myCurrentViewControllers = tmpDelegate.myNavigationController.viewControllers;
Nếu điều khiển xem gốc của ứng dụng của bạn là một UINavigationController
bạn có thể làm điều này:
((UINavigationController*)appDelegate.window.rootViewController).visibleViewController;
Tương tự như vậy, nếu đó là UITabBarController
bạn có thể thực hiện việc này:
((UITabBarController*)appDelegate.window.rootViewController).selectedViewController;
Tất nhiên, việc truyền rõ ràng như thế này bị bẩn. Tốt hơn là nên tự mình nắm bắt tham chiếu bằng các loại mạnh.
Đối với bất cứ ai không sử dụng một UINavigationController
nhưng thay vì điều khiển giao diện mặc định của họ là một UIViewController
bạn có thể kiểm tra xem điều khiển đang hoạt động (hoặc trình bày) những điều sau đây AppDelegate
:
func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> Int {
if let rootViewController = self.window!.rootViewController {
if let presentedViewController = rootViewController.presentedViewController {
return presentedViewController.supportedInterfaceOrientations()
}
} // Else current view controller is DefaultViewController
return Int(UIInterfaceOrientationMask.Portrait.rawValue)
}
Như bạn có thể nhìn thấy Tôi đang kiểm tra bộ điều khiển xem hiện tại để hỗ trợ các định hướng giao diện khác nhau cho các bộ điều khiển xem cụ thể. Đối với bất cứ ai quan tâm đến việc sử dụng phương pháp này để hỗ trợ cụ thể sau đây nên được đặt trong mỗi bộ điều khiển xem cần một định hướng cụ thể.
override func supportedInterfaceOrientations() -> Int {
return Int(UIInterfaceOrientationMask.All.rawValue)
}
Lưu ý: Mã này đã được viết với Swift 1.2
Thậm chí tốt hơn, thay đổi 'if let' thành' trong khi let' để nó đạt đến đỉnh cao nhất. – milesper
này có thể giúp
- (UIViewController *)topViewController{
return [self topViewController:[UIApplication sharedApplication].keyWindow.rootViewController];
}
- (UIViewController *)topViewController:(UIViewController *)rootViewController
{
if (rootViewController.presentedViewController == nil) {
return rootViewController;
}
if ([rootViewController.presentedViewController isKindOfClass:[UINavigationController class]]) {
UINavigationController *navigationController = (UINavigationController *)rootViewController.presentedViewController;
UIViewController *lastViewController = [[navigationController viewControllers] lastObject];
return [self topViewController:lastViewController];
}
UIViewController *presentedViewController = (UIViewController *)rootViewController.presentedViewController;
return [self topViewController:presentedViewController];
}
Swift phiên bản:
extension UIApplication {
class func topViewController(base: UIViewController? = UIApplication.sharedApplication().keyWindow?.rootViewController) -> UIViewController? {
if let nav = base as? UINavigationController {
return topViewController(base: nav.visibleViewController)
}
if let tab = base as? UITabBarController {
if let selected = tab.selectedViewController {
return topViewController(base: selected)
}
}
if let presented = base?.presentedViewController {
return topViewController(base: presented)
}
return base
}
}
Trích từ: https://gist.github.com/snikch/3661188
gotta yêu đệ quy, cảm ơn :) –
Bạn có thể nhận được bộ điều khiển xem hiện tại từ rootViewController bằng cách tìm kiếm presentedViewController của nó, như thế này:
UIViewController *parentViewController = [[[UIApplication sharedApplication] delegate] window].rootViewController;
while (parentViewController.presentedViewController != nil){
parentViewController = parentViewController.presentedViewController;
}
UIViewController *currentViewController = parentViewController;
Nó hoạt động với tôi.Hy vọng nó sẽ giúp :)
Đã lưu ngày của tôi. Cảm ơn +1 – crash
tốt nhất, Cảm ơn –
Swift Giải pháp:
self.window.rootViewController.presentedViewController.
Điều đó sẽ giúp bạn có được những gì bạn cần.
Hãy một phần mở rộng:
extension UIApplication {
class func topViewController(base: UIViewController? = UIApplication.sharedApplication().keyWindow?.rootViewController) -> UIViewController? {
if let nav = base as? UINavigationController {
return topViewController(nav.visibleViewController)
}
if let tab = base as? UITabBarController {
let moreNavigationController = tab.moreNavigationController
if let top = moreNavigationController.topViewController where top.view.window != nil {
return topViewController(top)
} else if let selected = tab.selectedViewController {
return topViewController(selected)
}
}
if let presented = base?.presentedViewController {
return topViewController(presented)
}
return base
}
}
Cách sử dụng:
if let rootViewController = UIApplication.topViewController() {
//do sth with root view controller
}
Câu trả lời này đã khắc phục sự cố gây phiền toái với các lớp mẫu Nâng cao NSOperations của Apple. Nếu xảy ra lỗi, bộ điều khiển chế độ xem trên cùng là bộ điều khiển duy nhất có thể hiển thị cảnh báo. Tôi có thể đã làm cho nó lặp đi lặp lại thay vì đệ quy, bởi vì tôi là cũ thời, nhưng điều này là tốt hơn nhiều so với mù quáng bằng cách sử dụng UIApplication.sharedApplication(). KeyWindow? .rootViewController –
Thường thì tôi cần phải lấy điều khiển xem mà hiện tại đang hiển thị. Nó có thể có nghĩa là bộ điều khiển xem ở trên cùng của ngăn xếp của UINavigationController hiện tại, bộ điều khiển xem hiện được trình bày, vv Vì vậy, tôi đã viết hàm này mà nó ra hầu hết thời gian, và bạn có thể sử dụng bên trong một phần mở rộng UIViewController.
Mã trong Swift 3:
func currentViewController(
_ viewController: UIViewController? =
UIApplication.shared.keyWindow?.rootViewController)
-> UIViewController? {
guard let viewController =
viewController else { return nil }
if let viewController =
viewController as? UINavigationController {
if let viewController =
viewController.visibleViewController {
return currentViewController(viewController)
} else {
return currentViewController(
viewController.topViewController)
}
} else if let viewController =
viewController as? UITabBarController {
if let viewControllers =
viewController.viewControllers,
viewControllers.count > 5,
viewController.selectedIndex >= 4 {
return currentViewController(
viewController.moreNavigationController)
} else {
return currentViewController(
viewController.selectedViewController)
}
} else if let viewController =
viewController.presentedViewController {
return viewController
} else if viewController.childViewControllers.count > 0 {
return viewController.childViewControllers[0]
} else {
return viewController
}
}
Gọi nó với: currentViewController()
- 1. Làm cách nào để tải một trình điều khiển chế độ xem khác từ tệp triển khai của trình điều khiển chế độ xem hiện tại?
- 2. Trình điều khiển chế độ xem bảng phân cảnh hiện tại từ đại biểu ứng dụng?
- 3. Thêm chế độ xem của trình điều khiển chế độ xem con vào chế độ xem phụ của bộ điều khiển chế độ xem cha mẹ
- 4. Có một bộ điều khiển xem khác trước khi bộ điều khiển chế độ xem hiện tại xuất hiện
- 5. Trình bày một bộ điều khiển xem cụ thể từ AppDelegate
- 6. Cách bật từ một bộ điều khiển chế độ xem sang một bộ điều khiển chế độ xem khác
- 7. Sự khác nhau giữa Chế độ xem và Trình điều khiển chế độ xem là gì?
- 8. Truy cập Trình điều khiển Điều hướng Hàng đầu từ Trình điều khiển Điều hướng của Chế độ xem Phụ
- 9. iOS 6 Xoay: Đẩy Trình điều khiển Chế độ xem
- 10. cách tải lại chế độ xem bảng của bộ điều khiển uiviewcontroller khác trong chế độ xem hiện tại
- 11. Chuyển biến giữa các trình điều khiển chế độ xem
- 12. Bộ điều khiển xem đẩy vào chế độ xem giao diện chế độ xem
- 13. Đi từ Bộ điều khiển đến Chế độ xem
- 14. LÀM THẾ NÀO? Bộ điều khiển trả về không có gì/chế độ xem hiện tại
- 15. Thanh tab xuất hiện sau khi bật trình điều khiển chế độ xem
- 16. Cách đính kèm Chế độ xem hiện tại vào hành động của bộ điều khiển?
- 17. Trong chế độ xem (CakePHP), cách thích hợp để có bộ điều khiển hiện tại?
- 18. Làm cách nào để nhận URL của trình điều khiển từ chế độ xem trong Codeigniter?
- 19. làm cách nào tôi có thể truy cập chế độ xem của trình điều khiển chế độ xem của cha mẹ từ trình điều khiển chế độ xem con?
- 20. 'Đã cố gắng bật đến trình điều khiển chế độ xem không tồn tại.'
- 21. Loại bỏ hai bộ điều khiển chế độ xem
- 22. MVC: Trình điều khiển chế độ xem mô hình - Chế độ xem có gọi là Mô hình không?
- 23. Hiển thị chế độ xem của bộ điều khiển khác
- 24. Sử dụng Trình điều khiển Xem từ bảng phân cảnh theo chương trình trong Trình điều khiển Chế độ xem Trang
- 25. Chế độ xem trang phân trang với bộ điều khiển chế độ xem trẻ em
- 26. Truyền dữ liệu từ bộ điều khiển chế độ xem này sang bộ điều khiển chế độ xem khác; iOS <= 4 so với iOS 5
- 27. Trình bày một bộ điều khiển phương thức mà không biết bộ điều khiển xem hiện tại?
- 28. Tìm điều khiển trong chế độ xem đăng nhập
- 29. ExtJS 4 cách tạo và hiển thị bộ điều khiển/chế độ xem mới từ bộ điều khiển/chế độ xem khác?
- 30. ios thay đổi bộ điều khiển chế độ xem mặc định của bảng phân cảnh tại thời gian chạy
Man Tôi nhìn khắp mọi nơi cho việc này. cảm ơn một tấn. –