Ans : Delegate is means of communication
between objects of iOS Applications. Delegate allows one object to send message
to another object when an event occurs.
i.e
UIAlertView *alert = [[UIAlertView
alloc] initWithTitle:@”title” message:@”message” delegate:self
cancelButtonTitle:@”Ok” otherButtonTitle:nil];
Here delegate is self. So now self is
responsible for handling all event fired
by this instance of UIAlertView class.
Which button of UIAlertView is
clicked, for that, event is clickedButtonAtIndex is called in or by Self or
currentViewController.
Create PreDefined delegate :
- There are two ViewController NameVC and SurNameVC.
- In NameVC, there are 2 textfield named as Name and FullName and 1 button as Submit.
- If I write in name and click on submit, it went to SurNameVC to take Surname parameter.
- On SurNameVC, after write Surname, on clicking of Submit, It call delegate method and went back to NameVC and Print Full Name in FullName textfield.
Implement above delegate and protocol in Objective-C :
I have made protocol on SurNameVC like
@protocol SurNameVCDelegate
-(void)setSurName:(NSString *) strSurName;
@end
@property (nonatomic, retain) id delegate;
Now on NameVC submit button click, choose delegate of SurNameVC object as self.
Now on NameVC submit button click, choose delegate of SurNameVC object as self.
objSurNameVC.delegate = self
and create method -(void)setSurName:(NSString *) strSurName;
on NameVC and it is called from
surNameVC submit button. So setSurName is delegate method. We can print
fullname by concatenating Name and Surname in FullName textfield.
So we delegate just pass message from one view controller to another
view controller by delegate method.
Implement delegate and protocol in Swift :
I have made custom UISlider. I want to send some value from custom UISlider value changed to view controller in which it is used. So for that, I have used delegate - protocol method.
customSlider.swift Custom Slider file
Implement delegate and protocol in Swift :
I have made custom UISlider. I want to send some value from custom UISlider value changed to view controller in which it is used. So for that, I have used delegate - protocol method.
customSlider.swift Custom Slider file
import UIKit
protocol SliderDelegate: class {
func sliderValueChanged(_ sender : UISlider)
}
class mpgpsSlider: UIView {
weak var delegate : SliderDelegate?
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
let bundle = Bundle.init(for: type(of: self))
let nib = UINib(nibName: "Slider", bundle: bundle)
let view = nib.instantiate(withOwner: self, options: nil)[0] as! UIView
view.frame = bounds
view.autoresizingMask = [.flexibleWidth,.flexibleHeight]
addSubview(view)
slider.addTarget(self, action: #selector(sliderValueChanged(_:)), for: .valueChanged)
}
@objc func sliderValueChanged(_ sender : UISlider) {
delegate?.sliderValueChanged(sender)
}
}
ViewController.swift ViewController in which custom slider is used.
import UIKit
class VehicleProfileVC: BaseViewController,SliderDelegate{
override func viewDidLoad() {
super.viewDidLoad()
slider.delegate = self
}
func sliderValueChanged(_ sender: UISlider) {
label.text = String(sender.value)
}
}
No comments:
Post a Comment
Thanks