Friday, 27 May 2016

Delegate in swift

When working with swift, it's common case where we encounter in a situation where we want an object to perform the task on behalf of another object. The rescue to this is delegate. It is very same process of working of delegate like in objective c. But creating a custom delegate is little different then objective c in swift .

Lets look in example, here we are creating a custom delegate :


protocol userDelegate:class{

  //list of methods

func userDidLogIn()
func userDidLogOut()

}

Class User: NSObject{

//make a singleton of the User class
static let sharedUser =  User()

//define a property of the delegate
weak var delegate:userDelegate?

func logInUser(){
   //here we can store the state of the user and perform some other task
     
//call delegate method
delegate?.userDidLogIn() 
 
}

func  logoutUser(){
  //delete user detail from app
 
//call delegate method
 delegate?.userDidLogOut()
}
}


Now to use this delegate, we can create a login view controller, where user will login , on successful login , we make a singleton User() object and call its method logInUser . In logIn view controller will  conform to the userDelegate .

import UIKit

class LogInVc: UIViewController, userDelegate{

@IBAction func btnActn_userLogIn(sender:AnyObject){
      
       //create user
      let user  = User.sharedUser

    user. logInUser()

}

@IBAction func btnActn_logOut(sender:AnyObject){
   let user  = User.sharedUser

    user. logoutUser()
}

Now it's time to implement delegate methods, here we can perform the post log in task.
func userDidLogIn(){
 //perform post log in task here
}

func userDidLogOut{
//perform log out task here
}

}

Friday, 6 May 2016

Change UITabBar item's image color and title text color

Some time there is a need to add tab bar in our app. Apple provide UITabBarController to add a tab bar . 
Here is an example to add tab bar in our app
To add  a tab bar  in our app, we create  a UITabBarController instance.

let tabVC  =  UITabBarController()

Then create  the view controller instances we want to add  in tab bar . For example , we create two UIViewController instance here. You can  create the subclasses of UIViewController as per your requirements.

let vc1 =  MyViewController1()
let vc2 = MyViewController2()


Then add  both view controller to tabVC 's viewControllers property as:

tabVC.viewControllers = [vc1, vc2]

Then set the index of the view controller we want to see selected by default as:

tabVC.selectedIndex = 1

Now we have set up tab bar controller and add two view controllers to it . Now we add the  tabBarItem property of both view controllers  like as:

vc1.tabBarItem   = UITabBarItem(title:"Profile", image:nil, tag:1)
 or 
vc1.tabBarItem  =  UITabBarItem(title:String, image:UIImage?, unSelectedImage:UIImage?)
 
Here title and image color are of system color that is light gray . We can change the image color for both selected and unselected state of UITabBar items as

vc1.tabBarItem  = UITabBarItem(title:"Profile", image:UIImage(named:"profile.png")?.imageWithRenderingMode(.AlwaysOriginal), tag: 1)

Then above code assign a title Profile for the vc1 tab and set a image to the tab. The title will appear at the bottom of the  tab image. The  tab image color will be now of the same color as profile.png(red, white whatever.)

But the title color is same as before. So to change the color of title also , add this line:

UITabBatItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.whiteColor()], forState: .Normal) 

This will change the color  of the title of the tab bar item  in tab bar to white for normal state. You can  change the title color for selected state also in same way , just change the state in above statement .