IOS學習筆記(頁面傳值:屬性傳值,協議代理傳值,閉包傳值)
1.屬性傳值,代理協議傳值
建立檢視控制區,導航檢視控制器
在AppDelegate類下
func application()方法下
let vc = ViewController()
let navc = UINavigationController(rootViewController: vc)
window = UIWindow(frame: UIScreen.mainScreen().bounds)
window?.rootViewController = navc
window?.makeKeyAndVisible()
ViewController類下
var labelStr:UILabel?//設定全域性變數labelStr
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.whiteColor() //設定背景顏色
navigationItem.title = "first"//設定導航欄標題
labelStr = UILabel(frame: CGRectMake(100, 300, 100, 30))//設定labelStr的座標,寬高
labelStr!.text = "海賊王"
labelStr!.textColor = UIColor.blackColor()
view.addSubview(labelStr!)
let nextBtn = UIButton(frame: CGRectMake(100, 400, 80, 30)) //設定按鈕
nextBtn.setTitleColor(UIColor.redColor(), forState: .Normal)
nextBtn.setTitle("下一頁", forState: UIControlState.Normal)
nextBtn.backgroundColor = UIColor.yellowColor()
nextBtn.addTarget(self, action: "leftBtn", forControlEvents: UIControlEvents.TouchUpInside)//設定nextBtn的點選事件
view.addSubview(nextBtn)
}
func leftBtn(){
let vc2 = ViewController2()
//屬性傳值
vc2.labelStr = labelStr!.text
//將當前控制器設定vc2的代理
vc2.delegate = self
navigationController?.pushViewController(vc2, animated: true)//跳轉到ViewController2
}
//完成協議內容
extension ViewController:secondProtocol{
func translateStr(str: String) {
//將ViewController的label.text 設定為 ViewController2 中的str
labelStr?.text = str
}
}
在ViewController2類下
import UIKit
//從後往前傳值的協議
protocol secondProtocol {
//需要傳什麼型別的引數,引數列表就寫什麼
func translateStr(str:String)
}
class ViewController2: UIViewController {
//定義代理屬性
var delegate:secondProtocol?
//定義一個labelStr屬性負責接收來自前一個頁面傳過來的值
var labelStr:String?
var textFeild:UITextField?
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.title = "Second"
view.backgroundColor = UIColor.grayColor()
//設定文字框
textFeild! = UITextField(frame: CGRectMake(100, 300, 180, 30))
textFeild!.placeholder = "請輸入文字"
textFeild?.text = labelStr
view.addSubview(textFeild!)
//設定按鈕
let nextBtn = UIButton(frame: CGRectMake(100, 400, 80, 30))
nextBtn.setTitle("上一頁", forState: UIControlState.Normal)
nextBtn.setTitleColor(UIColor.redColor(), forState: .Normal)
nextBtn.backgroundColor = UIColor.yellowColor()
nextBtn.addTarget(self, action: "rightBtn", forControlEvents: UIControlEvents.TouchUpInside)
view.addSubview(nextBtn)
}
func rightBtn(){
//執行協議
delegate?.translateStr((textFeild?.text!)!)
navigationController?.popViewControllerAnimated(true)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
2.閉包傳值,用閉包傳值改變上一頁的背景顏色
ViewController類下
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.title = "first"
view.backgroundColor = UIColor.whiteColor()
let btn = UIButton(frame: CGRectMake(100, 100, 100, 50))
btn.layer.borderWidth = 2
btn.setTitle("Push", forState: .Normal)
btn.setTitleColor(UIColor.blackColor(), forState: .Normal)
btn.addTarget(self, action: "btn", forControlEvents: UIControlEvents.TouchUpInside)
view.addSubview(btn)
}
func btn(){
let vc2 = ViewController2()
//閉包的實現
vc2.color = {
(str:UIColor) -> Void
in
self.view.backgroundColor = str
}
navigationController?.pushViewController(vc2, animated: true)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
import UIKit
class ViewController2: UIViewController {
var color:((str:UIColor)->Void)?//設定閉包
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.title = "Second"
view.backgroundColor = UIColor.whiteColor()
let btn = UIButton(frame: CGRectMake(100, 100, 100, 50))
btn.setTitle("Pop", forState: .Normal)
btn.setTitleColor(UIColor.blackColor(), forState: .Normal)
btn.layer.borderWidth = 2
btn.addTarget(self, action: "btn", forControlEvents: UIControlEvents.TouchUpInside)
view.addSubview(btn)
}
func btn(){
let str = UIColor.blueColor()//設定str為藍色
//呼叫閉包
color!(str: str)
navigationController?.popViewControllerAnimated(true)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
相關文章
- iOS Block傳值、代理傳值、通知中心iOSBloC
- swift 閉包傳值Swift
- iOS傳值iOS
- block,代理,通知傳值BloC
- iOS——使用StroryBoard頁面跳轉及傳值iOS
- vue父子傳值與非父子傳值Vue
- 網頁間傳值怎麼傳網頁
- iOS block 反向傳值iOSBloC
- iOS 常用傳值方式iOS
- JavaWeb開發之頁面傳值JavaWeb
- asp.net 頁面傳值方法ASP.NET
- JSP頁面間傳值方法JS
- Java傳參傳值Java
- 元件:非父子間傳值(同級傳值)元件
- Vue--子元件互相傳值,子元件來回傳值,傳值反覆橫跳Vue元件
- ASP.NET頁面傳值基本方式ASP.NET
- Asp.Net頁面傳值的方法ASP.NET
- golang工作筆記(二)值傳遞與引用傳遞Golang筆記
- Android 元件系列-----Activity的傳值和回傳值Android元件
- iOS開發之通過代理逆向傳值iOS
- 值傳遞和引用傳遞
- 值傳遞與引用傳遞
- Memory記憶體傳值記憶體
- Java學習之值傳遞(轉)Java
- Vue 元件傳值Vue元件
- Flutter11 路由、頁面傳值、NavigatorFlutter路由
- Flutter多個頁面廣播通知傳值Flutter
- [MUI] mui框架實現頁面間傳值UI框架
- 頁面傳值地址爛亂碼問題
- ASP.NET頁面間傳值彙總ASP.NET
- Intent傳值與Bundle傳值的區別(原始碼分析)Intent原始碼
- Vue 路由傳值(傳參)詳解Vue路由
- JAVA值傳參和引用傳參Java
- Ajax傳值的初級學習(一)
- 如何利用javascript實現頁面間互相傳值JavaScript
- 專案分享三:頁面之間的傳值
- 頁面間傳值與跳轉的區別
- jsp,struts,mysql分頁_Iterator有沒屬性可以傳值?JSMySql