Skip to main content

Posts

Showing posts from February, 2016

Load custom xib from viewcontroller in SWIFT

In custom UIView class   class NearByOffer: UIView,  override the init method as follows     override init (frame: CGRect ) {         super . init (frame: frame)         xibSetup ()     }     required init ?(coder aDecoder: NSCoder ) {         super . init (coder: aDecoder)  } Set up the xib as follows:- func xibSetup() {         view = loadViewFromNib ()                  view . frame = bounds         view . autoresizingMask = [ UIViewAutoresizing .FlexibleWidth, UIViewAutoresizing .FlexibleHeight]                 // Adding custom subview on top of our view (over any custom drawing > see note below)         view . backgroundColor = UIColor (white: 1.0 , alpha: 0.5 )         addSubview ( view )     } Load view from custom nib/xib as follows:-     func loadViewFromNib() -> UIView {         let bundle = NSBundle (forClass: self . dynamicType )         let nib = UINib

Blocks in SWIFT 3 and SWIFT 2

SWIFT 3 //Custom view/cell say  LocationPickerView (: UIView) typealias blockDefination_DepatureCityAction = ( _ button : UIButton ) -> Void var handler_DepatureCityAction: blockDefination_DepatureCityAction ? @IBAction func btnDepatureCityAction( _ sender: UIButton ) {         if ( self . responds (to: #selector (getter: LocationPickerView . handler_DepatureCityAction ))){             self . handler_DepatureCityAction ?(sender)         }     } //View controller where you want callback let locationPickerObject = LocationPickerView (frame: viewLocation . bounds ) locationPickerObject. handler_DepatureCityAction = { button in             print ( "DepatureCityAction in view controller" )             print (button)         } SWIFT 2 //**************** BLOCK WITHOUT PARAMETER //DECLARE as property typealias completionBlockSeeMore = () -> Void var cMoreButtonCallBa