swift4 代理的写法

简介: 简单记录swift中的代理的写法。首先在storyboard中画出两个VC:image.png然后,把对应的类文件添加上//// ViewController.

简单记录swift中的代理的写法。首先在storyboard中画出两个VC:


img_9bcf96a2fb1d8ebd3419e15c881ca68e.png
image.png

然后,把对应的类文件添加上

//
//  ViewController.swift
//  DelegateTest
//
//  Created by iOS on 2018/2/27.
//  Copyright © 2018年 iOS. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var showTextLabel: UILabel!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func nextAction(_ sender: UIButton) {
        let vc = NextViewController.instance()
        vc.delegate = self
        present(vc, animated: true) {}
    }
    
}

extension ViewController: NextViewControllerProtocol {
    func callBack(string: String) {
        showTextLabel.text = string
    }
}
//
//  NextViewController.swift
//  DelegateTest
//
//  Created by iOS on 2018/2/27.
//  Copyright © 2018年 iOS. All rights reserved.
//

import UIKit

protocol NextViewControllerProtocol: NSObjectProtocol {
    func callBack(string: String)
}

class NextViewController: UIViewController {

    @IBOutlet weak var ContentStr: UILabel!
    weak var delegate:NextViewControllerProtocol?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white
        
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    class func instance() -> NextViewController {
        let storeB = UIStoryboard.init(name: "Main", bundle: nil)
        let vc = storeB.instantiateViewController(withIdentifier: "NextViewController") as! NextViewController
        return vc
    }
    
    @IBAction func back(_ sender: UIButton) {
        delegate?.callBack(string: ContentStr.text ?? "没有数据")
        dismiss(animated: true) {}
    }
}

实现效果是反向传值。

目录
相关文章
|
Swift 开发者
Swift - swift3.0中代理方法的使用
Swift - swift3.0中代理方法的使用
72 0
|
存储 前端开发 中间件
使用 Swift 搭建一个 HTTP 代理
我将通过这篇文章详述一下如何用 Swift 搭建一个 HTTP 代理服务器。本文将使用 Hummingbird 作为服务端的基本HTTP框架,以及使用 AsyncHTTPClient 作为 Swift 的 HTTP 客户端来请求目标服务。
256 0
|
iOS开发
手把手带你在 Swift 中应用代理(Delegate)
本文讲的是手把手带你在 Swift 中应用代理(Delegate),什么是代理呢?在软件开发中,存在许多用于解决特定场景中的普遍问题的通用方案架构,这些所谓的“模板”,常被称为设计模式。
1834 0
|
4月前
|
安全 编译器 Swift
IOS开发基础知识: 对比 Swift 和 Objective-C 的优缺点。
IOS开发基础知识: 对比 Swift 和 Objective-C 的优缺点。
287 2
|
2月前
|
Unix 调度 Swift
苹果iOS新手开发之Swift 中获取时间戳有哪些方式?
在Swift中获取时间戳有四种常见方式:1) 使用`Date`对象获取秒级或毫秒级时间戳;2) 通过`CFAbsoluteTimeGetCurrent`获取Core Foundation的秒数,需转换为Unix时间戳;3) 使用`DispatchTime.now()`获取纳秒级精度的调度时间点;4) `ProcessInfo`提供设备启动后的秒数,不表示绝对时间。不同方法适用于不同的精度和场景需求。
54 3
|
1月前
|
存储 移动开发 Swift
使用Swift进行iOS应用开发:探索现代移动开发的魅力
【8月更文挑战第12天】使用Swift进行iOS应用开发,不仅能够享受到Swift语言带来的简洁、快速、安全的编程体验,还能够充分利用iOS平台提供的丰富资源和强大功能。然而,iOS应用开发并非易事,需要开发者具备扎实的编程基础、丰富的实践经验和不断学习的精神。希望本文能够为您的iOS应用开发之旅提供一些有益的参考和帮助。
|
2月前
|
Swift iOS开发 Kotlin
苹果iOS新手开发之Swift中实现类似Kotlin的作用域函数
Swift可通过扩展实现类似Kotlin作用域函数效果。如自定义`let`, `run`, `with`, `apply`, `also`,增强代码可读性和简洁性。虽无直接内置支持,但利用Swift特性可达成相似功能。
48 7
|
2月前
|
调度 Swift Android开发
苹果iOS新手开发之Swift中的并发任务和消息机制
Swift的消息机制类似Android的Handler,实现任务调度有三种方式: 1. **Grand Central Dispatch (GCD)**:使用`DispatchQueue`在主线程或后台线程执行任务。 2. **OperationQueue**:提供高级接口管理`Operation`对象。 3. **RunLoop**:处理事件如输入源、计时器,类似Android的`Looper`和`Handler`。 **示例**: - GCD:在不同线程执行代码块。 - OperationQueue:创建操作并执行。 - RunLoop:用Timer添加到RunLoop中。
78 2
|
2月前
|
安全 编译器 Swift
探索iOS开发:Swift语言的现代魔法
【7月更文挑战第11天】本文深入探讨了Swift编程语言,它如何革新iOS开发领域,以及它为开发者带来的独特优势。我们将从Swift的基础语法出发,通过实际案例分析其性能优化技巧,最后讨论Swift在跨平台开发中的潜力。文章旨在为读者提供一个全面而深入的视角,了解Swift不仅仅是一门语言,更是一种推动创新的力量。