Get to UIViewController from UIView?

Using the example posted by Brock, I modified it so that it is a category of UIView instead UIViewController and made it recursive so that any subview can (hopefully) find the parent UIViewController. @interface UIView (FindUIViewController) – (UIViewController *) firstAvailableUIViewController; @end @implementation UIView (FindUIViewController) – (UIViewController *) firstAvailableUIViewController { UIResponder *responder = [self nextResponder]; while … Read more

Get top most UIViewController

presentViewController shows a view controller. It doesn’t return a view controller. If you’re not using a UINavigationController, you’re probably looking for presentedViewController and you’ll need to start at the root and iterate down through the presented views. if var topController = UIApplication.sharedApplication().keyWindow?.rootViewController { while let presentedViewController = topController.presentedViewController { topController = presentedViewController } // topController … Read more

How to lock orientation of one view controller to portrait mode only in Swift

Things can get quite messy when you have a complicated view hierarchy, like having multiple navigation controllers and/or tab view controllers. This implementation puts it on the individual view controllers to set when they would like to lock orientations, instead of relying on the App Delegate to find them by iterating through subviews. Swift 3, … Read more

Instantiate and Present a viewController in Swift

This answer was last revised for Swift 5.4 and iOS 14.5 SDK. It’s all a matter of new syntax and slightly revised APIs. The underlying functionality of UIKit hasn’t changed. This is true for a vast majority of iOS SDK frameworks. let storyboard = UIStoryboard(name: “myStoryboardName”, bundle: nil) let vc = storyboard.instantiateViewController(withIdentifier: “myVCID”) self.present(vc, animated: … Read more

How to find topmost view controller on iOS

I think you need a combination of the accepted answer and @fishstix’s + (UIViewController*) topMostController { UIViewController *topController = [UIApplication sharedApplication].keyWindow.rootViewController; while (topController.presentedViewController) { topController = topController.presentedViewController; } return topController; } Swift 3.0+ func topMostController() -> UIViewController? { guard let window = UIApplication.shared.keyWindow, let rootViewController = window.rootViewController else { return nil } var topController = … Read more

Looking to understand the iOS UIViewController lifecycle

All these commands are called automatically at the appropriate times by iOS when you load/present/hide the view controller. It’s important to note that these methods are attached to UIViewController and not to UIViews themselves. You won’t get any of these features just using a UIView. There’s great documentation on Apple’s site here. Putting in simply … Read more

Displaying images on another view controller when button is clicked

Swift 4 First, add a notification in each button action: NotificationCenter.default.post(name: Notification.Name(“buttonAIsPressed”), object: nil) now add a notification listener in the viewDidLoad of the collection view class and call the function in the selector: NotificationCenter.default.addObserver(self, selector: #selector(self.YourFunctionName), name: Notification.Name(“buttonAIsPressed”), object: nil) now whenever a button is pressed the function you named will be called.