Pages

Presenting a ViewController Programmatically in User Interface

Designing View Controller: 
     Presenting a view programmatically in user interface is as easy as you draw Segment Controller , UIButton & UILabel. and you will get use to it as you practice. And I bet it is pretty easy & interesting task as well.To design the ViewController programmatically either you can use the XIB/NIB file or you can just create an object of the UIViewController() class && the in the AppDelegate.Swift class use the window property to set the size of the ViewController window && then use the "window" property of AppDelegate.Swift file to present the view Controller without using XIB/NIB file buw in this tutorial I will be using the 1st method to present the ViewController from XIB/NIB file. Now lets design the User Interface:

1. Now lets again as usual create a Xcode project by usual setting of Single View Application & selecting the language Swift.


2. Create a new Cocoa Touch Class file call it ProgrammaticallyViewController & make it a subclass of UIViewController. Also check the XIB file check box, & follow the steps to create new file. Now open the ProgrammaticallyViewController.swift file.

3. Add the following method definition code in your ProgrammaticallyViewController file:

                //Calling the designated initializer of same class
      convenience override init() {   
                       self.init(nibName: "ProgrammaticallyViewController", bundle: nil)
                      //initializing the view Controller form specified NIB file
           }


     This bit of code is simple as we see but performing a lot of tasks so it really needs quite an explanation. Here, "convenience" is a secondary initializer which supports initializers for a class. Well you can say that convenience initializers are used to call the designated initializers of a class. These are not mandatory to provide if your class not require them. But here we are in a need of them.
     
          Here, inside the init() function there is an another init() function. All this function do is to initialize a view controller from a NIB/XIB file (here ProgrammaticallyViewController). At this point your Xcode window pretty much looks like



4. Now open the file named ProgrammaticallyViewController.xib & lets design the interface of the view controller. Now drag a label & a text field & position the corresponding to each other like figure:



5. Now open the Main.storyboard & drag a button & name it "Present New View Controller". Now add an action to the ViewController.Swift file. In the action button add the following code...

@IBAction func persent(sender: AnyObject) {
let programmaticallyViewController = ProgrammaticallyViewController()
         //creating an object of the ProgrammaticallyViewController 
  self.presentViewController(programmaticallyViewController, animated: true, completion: nil)   
         //displaying the view controller when you click on button
    }

Your ViewController.Swift file looks like:  


   

Well now go ahead and Build & Run your Application. With some luck when you Tap the "Present New View Controller"  titled Button, you will be presented with the ProgrammaticallyViewController with the label "Name" & corresponding text field.

 



Here in first fig It is how your first View Appear & second figure XIB/NIB file view controller is appearing from Downward to Upward animated.

Well thats it guys, I hope you enjoyed it. Now go & practice it.
        

No comments:

Post a Comment