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


overrideinit(frame: CGRect) { super.init(frame: frame) xibSetup()     }
requiredinit?(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(nibName: "NearByOffer", bundle: bundle) // Assumes UIView is top level and only object in CustomView.xib file let view = nib.instantiateWithOwner(self, options: nil)[0] as! UIView return view     }


USAGE in ViewController
func showNearByOfferPopup()     { let window = UIApplic…

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?

@IBActionfunc 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 = () -> Voidvar cMoreButtonCallBack:completionBlockSeeMore?if(self.respondsToSelector("cMoreButtonCallBack")){self.cMoreButtonCallBack!() }

//USAGE cell.cMoreButtonCallBack = { () -> Voidin                    printf("Cal…