uiviewcontroller interview questions
Top uiviewcontroller frequently asked interview questions
in iOS6 I noticed the new Container View but am not quite sure how to access it's controller from the containing view.
Scenario:
I want to access the labels in Alert view controller from the view controller that houses the container view.
There's a segue between them, can I use that?
Source: (StackOverflow)
Is it possible to cancel a segue in the prepareForSegue:
method?
I want to perform some check before the segue, and if the condition is not true (in this case, if some UITextField
is empty), display an error message instead of performing the segue.
Source: (StackOverflow)
Can anyone point me to any good examples of creating a Custom View Controller as a Container View Controller? The only documentation I can find is a couple of paragraphs in the UIViewController Class Reference. I feel I need a little more information than that and an example implementation would be nice. Google has turned up nothing at all.
I am specifically interested in the method:
transitionFromViewController:toViewController:duration:options:animations:completion:
Source: (StackOverflow)
Which init
method is called by the storyboard for UIViewControllers
added to the storyboard?
Source: (StackOverflow)
I'm just dipping my feet for the first time into iOS development, and one of the first things I've had to do is implement a custom container view controller - lets call it SideBarViewController
- that swaps out which of several possible child view controllers it shows, almost exactly like a standard Tab Bar Controller
. (It's pretty much a Tab Bar Controller
but with a hideable side menu instead of a tab bar.
As per the instructions in the Apple documentation, I call addChildViewController
whenever I add a child ViewController to my container. My code for swapping out the current child view controller being shown by the SideBarViewController
looks like this:
- (void)showViewController:(UIViewController *)newViewController {
UIViewController* oldViewController = [self.childViewControllers
objectAtIndex:0];
[oldViewController removeFromParentViewController];
[oldViewController.view removeFromSuperview];
newViewController.view.frame = CGRectMake(
0, 0, self.view.frame.size.width, self.view.frame.size.height
);
[self addChildViewController: newViewController];
[self.view addSubview: newViewController.view];
}
Then I started trying to figure out just what addChildViewController
does here, and I realised that I have no idea. Besides sticking the new ViewController
in the .childViewControllers
array, it seems to have no effect on anything. Actions and outlets from the child controller's view to the child controller that I've set on the storyboard still work just fine even if I never call addChildViewController
, and I can't imagine what else it could affect.
Indeed, if I rewrite my code to not call addChildViewController
, and instead look like this...
- (void)showViewController:(UIViewController *)newViewController {
// Get the current child from a member variable of `SideBarViewController`
UIViewController* oldViewController = currentChildViewController;
[oldViewController.view removeFromSuperview];
newViewController.view.frame = CGRectMake(
0, 0, self.view.frame.size.width, self.view.frame.size.height
);
[self.view addSubview: newViewController.view];
currentChildViewController = newViewController;
}
... then my app still works perfectly, so far as I can tell!
The Apple documentation doesn't shed much light on what addChildViewController
does, or why we're supposed to call it. The entire extent of the relevant description of what the method does or why it should be used in its section in the UIViewController
Class Reference is, at present:
Adds the given view controller as a child.
...
This method is only intended to be called by an implementation of a custom container view controller. If you override this method, you must call super in your implementation.
There's also this paragraph earlier on the same page:
Your container view controller must associate a child view controller with itself before adding the child’s root view to the view hierarchy. This allows iOS to properly route events to child view controllers and the views those controllers manage. Likewise, after it removes a child’s root view from its view hierarchy, it should disconnect that child view controller from itself. To make or break these associations, your container calls specific methods defined by the base class. These methods are not intended to be called by clients of your container class; they are to be used only by your container’s implementation to provide the expected containment behavior.
Here are the essential methods you might need to call:
addChildViewController:
removeFromParentViewController
willMoveToParentViewController:
didMoveToParentViewController:
but it doesn't offer any clue as to what the 'events' or 'expected containment behavior' that it's talking about are, or why (or even when) calling these methods is 'essential'.
The examples of custom container view controllers in the "Custom Container View Controllers" section of the Apple documentation all call this method, so I assume that it serves some important purpose beyond just popping the child ViewController onto an array, but I can't figure out what that purpose is. What does this method do, and why should I call it?
Source: (StackOverflow)
I have a UIPageViewController
with translucent status bar and navigation bar. Its topLayoutGuide
is 64 pixels, as expected.
However, the child view controllers of the UIPageViewController
report a topLayoutGuide
of 0 pixels, even if they're shown under the status bar and navigation bar.
Is this the expected behavior? If so, what's the best way to position a view of a child view controller under the real topLayoutGuide
?
(short of using parentViewController.topLayoutGuide
, which I'd consider a hack)
Source: (StackOverflow)
I have a tabbar application, with many views. Is there a way to know if a particular UIViewController is currently visible from within the UIViewController? (looking for a property)
Source: (StackOverflow)
What I am trying to do is click a button (that was created in code) and have it call up a different view controller then have it run a function in the new view controller.
I know it could be done relatively easily in IB but that isn't an option.
An example of what I want to do would be if you had two view controllers one with a splash screen of house. The other view controller had a walk through of the house on it that you could go through all the rooms in a set order. The splash screen would have buttons for each room that would allow you to jump to any point on the walk through.
Source: (StackOverflow)
I am currently making an app that will have multiple timers, which are basically all the same.
I want to create a custom class that uses all of the code for the timers as well as the layout/animations, so I can have 5 identical timers that operate independently of each other.
I created the layout using IB (xcode 4.2) and all the code for the timers is currently just in the viewcontroller class.
I am having difficulty wrapping my brain around how to encapsulate everything into a custom class and then add it to the viewcontroller, any help would be much appreciated.
Source: (StackOverflow)
I have been reading a lot about iOS7 UI transition.
I am not able to get what these three properties automaticallyAdjustsScrollViewInsets
, extendedLayoutIncludesOpaqueBars
, edgesForExtendedLayout
??
For example I am trying to make my view controllers start below the status bar but I am not able to achieve it.
Source: (StackOverflow)
I have always been a bit unclear on the type of tasks that should be assigned to viewDidLoad vs. viewWillAppear: in a UIViewController subclass. For example, I am doing an app where I have a UIViewController subclass hitting a server, getting data, feeding it to a view and then displaying that view. What are the pros and cons of doing this in viewDidLoad vs. viewWillAppear?
Thanks in advance.
Cheers,
Doug
Source: (StackOverflow)
Could you explain me the correct manner to manage the UIViewController
lifecycle?
In particular, I would like to know how to use Initialize
, ViewDidLoad
, ViewWillAppear
, ViewDidAppear
, ViewWillDisappear
, ViewDidDisappear
, ViewDidUnload
and Dispose
methods in Monotouch for a UIViewController
class.
Thank you in advance.
Source: (StackOverflow)
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?
Source: (StackOverflow)
I want to check the type of an Object. How can I do that?
The scenario is I'm getting an object. If that object is of type A then do some operations. If it is of type B then do some operations. Currently the type of the object is C that is parent of A and B.
I have two classes AViewController
andBViewController
. The object I'm getting in UIViewController
. Now how to check whether the object is AViewController
or BViewController
?
Source: (StackOverflow)
I have 2 seperate navigationcontrollers, one with RootViewController A and the other with RootViewController B.
I am able to push ViewController C onto either A or B's navigation stack.
Question: When I am in ViewController C, how can I find out if I am in the stack belonging to A or B?
Source: (StackOverflow)