View Controllers in iOS

Throughout the videos in this article, we will explore how to create a new view controller and how (from interface) go through a segue to transition to a different view controller. But before that, let’s understand what view controllers in iOS actually are.

What are View Controllers in iOS

View controllers are like a container that hold on to all UI objects for a certain class and they also provide user interactivity.

View Controllers in iOS

What is a Segue?

The next thing we need to learn is how to call a segue programmatically and within the interface builder. First we will use, segues from buttons to view controllers. We will also use segues from view controllers to view controllers.

Segues in iOS

Beyond using the basic segues, you can also benefit from unwind segues. To use unwind segue, you would need a function with IBAction return type and an argument of the type UIStoryBoardSegue in the View Controller you are unwinding back to. Please refer to the following code:

// Unwind segue

// Objective-C
- (IBAction)unwindToThisViewController:(UIStoryboardSegue *)unwindSegue
{
}

// Swift
@IBAction func unwindToThisViewController(segue: UIStoryboardSegue) {
}

Firstly, if this part is a little confusing, don’t worry. You are in good company. Unwind segues are the only type of functions that you don’t code them in the class of your view controller. Secondly, Rather you code them on where you want to go back to.

The difference between Unwinde and Pop (in a Navigation Controller) is in the ability to send data.

Passing data between View Controllers in iOS

The next thing we need to learn is how to prepare for a segue and pass data between our different view controller in iOS. To do so, we need to prepare for an upcoming segue using the prepareForSegue built-in function

// Prepare for segue Objective-C

- (void)prepareForSegue:(UIStoryboardSegue *)segue 
                 sender:(id)sender
     {
     }
     
// Swift

func prepare(for segue: UIStoryboardSegue, 
      sender: Any?)
  {
  }
  
      

Passing data in practice

Passing data between view controllers.

View Controllers in iOS life cycle

View controller life cycle is an important aspect of any app. Most importantly, in a view controller life cycle are the following functions:

  • viewDidLoad: View is now created. Initialize your objects.
  • viewWillAppear: It runs every time the view appears. Don’t run codes that shouldn’t be repeated.
  • viewDidLayoutSubviews: When the sub views are properly laid out.
  • viewWillAppear: when the view has been laid out and is visible to user. Good place to run animation, etc.
  • viewDidAppear: often happening before the transition to another view controller.
  • viewDidDisappear: when the view controller is no longer visible. Useful to stop any continuously running tasks
  • didReceiveMemoryWarning: gets notified when you used up the available memory. As a result, you will receive a memory warning in here.
View Controller Lifecycle

Summary

In this article we covered the basics of using a variety of view controllers in iOS features. We discussed initializing new view controllers as well as passing data between them.

Interested in learning more in-depth concepts?

If you are interested in learning more about mobile app development, I invite you to join my complete iPhone or Android course(s):

Leave a Comment