Questions tagged [uiviewcontroller]

The UIViewController class manages the views in iOS apps and mediates the interaction between an app's views, its underlying model objects, and the overall workflow.

The UIViewController class manages the views in iOS and watch-os apps and mediates the interaction between an app's views, its underlying model objects, and the overall workflow.

View controllers are the foundation of your app’s internal structure. Every app has at least one view controller, and most apps have several. Each view controller manages a portion of your app’s user interface as well as the interactions between that interface and the underlying data. View controllers also facilitate transitions between different parts of your user interface.

To quote from the Overview of the UIViewController Class Reference:

The UIViewController class provides the fundamental view-management model for all iOS apps. You rarely instantiate UIViewController objects directly. Instead, you instantiate subclasses of the UIViewController class based on the specific task each subclass performs. A view controller manages a set of views that make up a portion of your app’s user interface. As part of the controller layer of your app, a view controller coordinates its efforts with model objects and other controller objects—including other view controllers—so your app presents a single coherent user interface.

References:

16290 questions
200
votes
29 answers

Get to UIViewController from UIView?

Is there a built-in way to get from a UIView to its UIViewController? I know you can get from UIViewController to its UIView via [self view] but I was wondering if there is a reverse reference?
bertrandom
  • 2,131
  • 2
  • 13
  • 8
167
votes
13 answers

Detecting sheet was dismissed on iOS 13

Before iOS 13, presented view controllers used to cover the entire screen. And, when dismissed, the parent view controller viewDidAppear function were executed. Now iOS 13 will present view controllers as a sheet as default, which means the card…
Marcos Tanaka
  • 3,112
  • 3
  • 25
  • 41
167
votes
4 answers

UIViewController viewDidLoad vs. viewWillAppear: What is the proper division of labor?

I have always been a bit unclear on the type of tasks that should be assigned to viewDidLoad vs. viewWillAppear: in a UIViewController subclass. e.g. I am doing an app where I have a UIViewController subclass hitting a server, getting data, feeding…
dugla
  • 12,774
  • 26
  • 88
  • 136
167
votes
12 answers

Given a view, how do I get its viewController?

I have a pointer to a UIView. How do I access its UIViewController? [self superview] is another UIView, but not the UIViewController, right?
mahboudz
  • 39,196
  • 16
  • 97
  • 124
158
votes
17 answers

How to check if a view controller is presented modally or pushed on a navigation stack?

How can I, in my view controller code, differentiate between: presented modally pushed on navigation stack Both presentingViewController and isMovingToParentViewController are YES in both cases, so are not very helpful. What complicates things is…
meaning-matters
  • 21,929
  • 10
  • 82
  • 142
157
votes
6 answers

Fatal error: use of unimplemented initializer 'init(coder:)' for class

I decided to continue my remaining project with Swift. When I add the custom class (subclass of UIViewcontroller) to my storyboard view controller and load the project, the app crashes suddenly with the following error: fatal error: use of…
Pratik Bhiyani
  • 2,489
  • 3
  • 17
  • 27
154
votes
16 answers

Display clearColor UIViewController over UIViewController

I have a UIViewController view as a subview/modal on top of another UIViewController view, such as that the subview/modal should be transparent and whatever components is added to the subview should be visible. The problem is that I have is the…
hightech
  • 3,742
  • 3
  • 25
  • 36
148
votes
25 answers

How do I Disable the swipe gesture of UIPageViewController?

In my case parent UIViewController contains UIPageViewController which contains UINavigationController which contains UIViewController. I need to add a swipe gesture to the last view controller, but swipes are handled as if they belong to page view…
143
votes
17 answers

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

Since my app got support for all orientation. I would like to lock only portrait mode to specific UIViewController. e.g. assume it was Tabbed Application and when SignIn View appear modally, I only want that SignIn View to the portrait mode only no…
Thiha Aung
  • 5,036
  • 8
  • 36
  • 79
143
votes
9 answers

How to get root view controller?

I need an instance of root view controller. I tried those approaches: UIViewController *rootViewController = (UIViewController*)[[[UIApplication sharedApplication] keyWindow] rootViewController]; Returns: null: Also when I try to get an array of…
Streetboy
  • 4,351
  • 12
  • 56
  • 101
142
votes
6 answers

Animate change of view controllers without using navigation controller stack, subviews or modal controllers?

NavigationControllers have ViewController stacks to manage, and limited animation transitions. Adding a view controller as a sub-view to an existing view controller requires passing events to the sub-view controller, which is a pain to manage,…
TigerCoding
  • 8,710
  • 10
  • 47
  • 72
140
votes
23 answers

Detect when a presented view controller is dismissed

Let's say, I have an instance of a view controller class called VC2. In VC2, there is a "cancel" button that will dismiss itself. But I can't detect or receive any callback when the "cancel" button got trigger. VC2 is a black box. A view…
user523234
  • 14,323
  • 10
  • 62
  • 102
138
votes
13 answers

viewWillDisappear: Determine whether view controller is being popped or is showing a sub-view controller

I'm struggling to find a good solution to this problem. In a view controller's -viewWillDisappear: method, I need to find a way to determine whether it is because a view controller is being pushed onto the navigation controller's stack, or whether…
Michael Waterfall
  • 20,497
  • 27
  • 111
  • 168
137
votes
21 answers

Completion block for popViewController

When dismissing a modal view controller using dismissViewController, there is the option to provide a completion block. Is there a similar equivalent for popViewController? The completion argument is quite handy. For instance, I can use it to hold…
Ben Packard
  • 26,102
  • 25
  • 102
  • 183
136
votes
20 answers

Get the current displaying UIViewController on the screen in AppDelegate.m

The current UIViewController on the screen need to response to push-notifications from APNs, by setting some badge views. But how could I get the UIViewController in methodapplication:didReceiveRemoteNotification: of AppDelegate.m? I tried use…
lu yuan
  • 7,207
  • 9
  • 44
  • 78