ස්විෆ්ට් 5
වෙල් මැට් ප්රයිස්ගේ පිළිතුර දත්ත සම්ප්රේෂණය කිරීම සඳහා ඉතා සුදුසු නමුත් නවතම ස්විෆ්ට් අනුවාදයෙන් මම එය නැවත ලිවීමට යන්නේ නව සින්ටැක්ස් සහ ක්රම / රාමු හේතුවෙන් නව ක්රමලේඛකයින් එය අභියෝගාත්මක නොවන බව මා විශ්වාස කරන හෙයිනි.
දර්ශන පාලකයන් අතර දත්ත සම්ප්රේෂණය කිරීම සඳහා විවිධ විකල්ප තිබේ.
- සංචාලන පාලක තල්ලු භාවිතා කිරීම
- Segue භාවිතා කිරීම
- නියෝජිතයා භාවිතා කිරීම
- දැනුම්දීම් නිරීක්ෂකය භාවිතා කිරීම
- බ්ලොක් භාවිතා කිරීම
මම ඔහුගේ තර්කනය ස්විෆ්ට් හි නවතම iOS රාමුව සමඟ නැවත ලිවීමට යන්නෙමි
සංචාලන පාලක තල්ලු හරහා දත්ත යැවීම : ViewControllerA සිට ViewControllerB දක්වා
පියවර 1. ViewControllerB හි විචල්යය ප්රකාශ කරන්න
var isSomethingEnabled = false
පියවර 2. ViewControllerB 'ViewDidLoad ක්රමයේදී විචල්යය මුද්රණය කරන්න
override func viewDidLoad() {
super.viewDidLoad()
//Print value received through segue, navigation push
print("Value of 'isSomethingEnabled' from ViewControllerA : ", isSomethingEnabled)
}
පියවර 3. සංචාලන පාලකය හරහා තල්ලු කරන අතරතුර ViewControllerA පාස් දත්ත
if let viewControllerB = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ViewControllerB") as? ViewControllerB {
viewControllerB.isSomethingEnabled = true
if let navigator = navigationController {
navigator.pushViewController(viewControllerB, animated: true)
}
}
එබැවින් සම්පූර්ණ කේතය මෙන්න:
ViewControllerA
import UIKit
class ViewControllerA: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
//MARK:Passing Data through Navigation PushViewController
@IBAction func goToViewControllerB(_ sender: Any) {
if let viewControllerB = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ViewControllerB") as? ViewControllerB {
viewControllerB.isSomethingEnabled = true
if let navigator = navigationController {
navigator.pushViewController(viewControllerB, animated: true)
}
}
}
}
ViewControllerB
import UIKit
class ViewControllerB: UIViewController {
//MARK: - Variable for Passing Data through Navigation push
var isSomethingEnabled = false
override func viewDidLoad() {
super.viewDidLoad()
//Print value received through navigation push
print("Value of 'isSomethingEnabled' from ViewControllerA : ", isSomethingEnabled)
}
}
Segue හරහා දත්ත සම්ප්රේෂණය කිරීම : ViewControllerA සිට ViewControllerB දක්වා
පියවර 1. ViewControllerA සිට ViewControllerB වෙත Segue සාදන්න සහ පහත දැක්වෙන පරිදි කතන්දර පුවරුවේ Identifier = showDetailSegue ලබා දෙන්න

පියවර 2. ViewControllerB දී නම් ශක්ය ප්රකාශ isSomethingEnabled හා එහි අගය මුද්රණය කරන්න.
පියවර 3. ViewControllerA pass හි Segue පසු කරන විට SomethingEnabled හි අගය වේ
එබැවින් සම්පූර්ණ කේතය මෙන්න:
ViewControllerA
import UIKit
class ViewControllerA: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
//MARK: - - Passing Data through Segue - -
@IBAction func goToViewControllerBUsingSegue(_ sender: Any) {
performSegue(withIdentifier: "showDetailSegue", sender: nil)
}
//Segue Delegate Method
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if (segue.identifier == "showDetailSegue") {
let controller = segue.destination as? ViewControllerB
controller?.isSomethingEnabled = true//passing data
}
}
}
ViewControllerB
import UIKit
class ViewControllerB: UIViewController {
var isSomethingEnabled = false
override func viewDidLoad() {
super.viewDidLoad()
//Print value received through segue
print("Value of 'isSomethingEnabled' from ViewControllerA : ", isSomethingEnabled)
}
}
නියෝජිතයා හරහා දත්ත යැවීම : ViewControllerB සිට ViewControllerA දක්වා
පියවර 1. ViewControllerB ගොනුවේ ප්රොටෝකෝලය ViewControllerBDelegate ප්රකාශ කරන්න නමුත් පන්තියෙන් පිටත
protocol ViewControllerBDelegate: NSObjectProtocol {
// Classes that adopt this protocol MUST define
// this method -- and hopefully do something in
// that definition.
func addItemViewController(_ controller: ViewControllerB?, didFinishEnteringItem item: String?)
}
පියවර 2. ViewControllerB හි නියෝජිත විචල්ය අවස්ථාව ප්රකාශයට පත් කරන්න
var delegate: ViewControllerBDelegate?
පියවර 3. ViewControllerB හි viewDidLoad ක්රමය තුළ නියෝජිතයින් සඳහා දත්ත යවන්න
delegate?.addItemViewController(self, didFinishEnteringItem: "Data for ViewControllerA")
පියවර 4. ViewControllerA හි ViewControllerBDelegate තහවුරු කරන්න
class ViewControllerA: UIViewController, ViewControllerBDelegate {
// to do
}
පියවර 5. ඔබ ViewControllerA හි නියෝජිතයා ක්රියාත්මක කරන බව තහවුරු කරන්න
if let viewControllerB = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ViewControllerB") as? ViewControllerB {
viewControllerB.delegate = self//confirming delegate
if let navigator = navigationController {
navigator.pushViewController(viewControllerB, animated: true)
}
}
පියවර 6. ViewControllerA හි දත්ත ලැබීම සඳහා නියෝජිත ක්රමයක් ක්රියාත්මක කරන්න
func addItemViewController(_ controller: ViewControllerB?, didFinishEnteringItem item: String?) {
print("Value from ViewControllerB's Delegate", item!)
}
එබැවින් සම්පූර්ණ කේතය මෙන්න:
ViewControllerA
import UIKit
class ViewControllerA: UIViewController, ViewControllerBDelegate {
override func viewDidLoad() {
super.viewDidLoad()
}
//Delegate method
func addItemViewController(_ controller: ViewControllerB?, didFinishEnteringItem item: String?) {
print("Value from ViewControllerB's Delegate", item!)
}
@IBAction func goToViewControllerForDelegate(_ sender: Any) {
if let viewControllerB = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ViewControllerB") as? ViewControllerB {
viewControllerB.delegate = self
if let navigator = navigationController {
navigator.pushViewController(viewControllerB, animated: true)
}
}
}
}
ViewControllerB
import UIKit
//Protocol decleare
protocol ViewControllerBDelegate: NSObjectProtocol {
// Classes that adopt this protocol MUST define
// this method -- and hopefully do something in
// that definition.
func addItemViewController(_ controller: ViewControllerB?, didFinishEnteringItem item: String?)
}
class ViewControllerB: UIViewController {
var delegate: ViewControllerBDelegate?
override func viewDidLoad() {
super.viewDidLoad()
//MARK: - - - - Set Data for Passing Data through Delegate - - - - - -
delegate?.addItemViewController(self, didFinishEnteringItem: "Data for ViewControllerA")
}
}
දැනුම්දීම් නිරීක්ෂකය හරහා දත්ත යැවීම : ViewControllerB සිට ViewControllerA දක්වා
පියවර 1. ViewControllerB හි දැනුම්දීම් නිරීක්ෂකයා තුළ දත්ත සකසා පළ කරන්න
let objToBeSent = "Test Message from Notification"
NotificationCenter.default.post(name: Notification.Name("NotificationIdentifier"), object: objToBeSent)
පියවර 2. ViewControllerA හි දැනුම්දීම් නිරීක්ෂකය එක් කරන්න
NotificationCenter.default.addObserver(self, selector: #selector(self.methodOfReceivedNotification(notification:)), name: Notification.Name("NotificationIdentifier"), object: nil)
පියවර 3. ViewControllerA හි දැනුම්දීම් දත්ත අගය ලබා ගන්න
@objc func methodOfReceivedNotification(notification: Notification) {
print("Value of notification : ", notification.object ?? "")
}
එබැවින් සම්පූර්ණ කේතය මෙන්න:
ViewControllerA
import UIKit
class ViewControllerA: UIViewController{
override func viewDidLoad() {
super.viewDidLoad()
// add observer in controller(s) where you want to receive data
NotificationCenter.default.addObserver(self, selector: #selector(self.methodOfReceivedNotification(notification:)), name: Notification.Name("NotificationIdentifier"), object: nil)
}
//MARK: Method for receiving Data through Post Notification
@objc func methodOfReceivedNotification(notification: Notification) {
print("Value of notification : ", notification.object ?? "")
}
}
ViewControllerB
import UIKit
class ViewControllerB: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
//MARK:Set data for Passing Data through Post Notification
let objToBeSent = "Test Message from Notification"
NotificationCenter.default.post(name: Notification.Name("NotificationIdentifier"), object: objToBeSent)
}
}
බ්ලොක් හරහා දත්ත යැවීම : ViewControllerB සිට ViewControllerA දක්වා
පියවර 1. ViewControllerB හි වාරණය ප්රකාශ කරන්න
var AuthorizationCompletionBlock: ((බූල්) -> ())? = {_ in}
පියවර 2. ViewControllerB හි දත්ත වාරණය කරන්න
if authorizationCompletionBlock != nil
{
authorizationCompletionBlock!(true)
}
පියවර 3. ViewControllerA හි වාරණ දත්ත ලබා ගන්න
//Receiver Block
controller!.authorizationCompletionBlock = { isGranted in
print("Data received from Block is :", isGranted)
}
එබැවින් සම්පූර්ණ කේතය මෙන්න:
ViewControllerA
import UIKit
class ViewControllerA: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
//MARK:Method for receiving Data through Block
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if (segue.identifier == "showDetailSegue") {
let controller = segue.destination as? ViewControllerB
controller?.isSomethingEnabled = true
//Receiver Block
controller!.authorizationCompletionBlock = { isGranted in
print("Data received from Block is :", isGranted)
}
}
}
}
ViewControllerB
import UIKit
class ViewControllerB: UIViewController {
//MARK:Variable for Passing Data through Block
var authorizationCompletionBlock:((Bool)->())? = {_ in}
override func viewDidLoad() {
super.viewDidLoad()
//MARK:Set data for Passing Data through Block
if authorizationCompletionBlock != nil
{
authorizationCompletionBlock!(true)
}
}
}
ඔබට සම්පූර්ණ නියැදි අයදුම්පත මගේ GitHub වෙතින් සොයාගත හැකිය කරුණාකර ඔබට මේ පිළිබඳව කිසියම් ප්රශ්නයක් (ප්රශ්න) ඇත්නම් මට දන්වන්න.
@class ViewControllerB;
@ ප්රොටොකෝල් අර්ථ දැක්වීමට ඉහළින් තැබිය යුතුද? : එය තොරව මම අනුකූලව ViewControllerB මත "බලාපොරොත්තුවූ වර්ගය" දෝෂයක් ලබා- (void)addItemViewController:(ViewControllerB *)controller didFinishEnteringItem:(NSString *)item;
තුළ@protocol
ප්රකාශ