swift之代理传值(delegate的用法)
2017-01-03 12:54
309 查看
目录(?)[+]
具体的代码,详见Demo:链接: http://download.csdn.net/download/riven_wn/9401970
view plain
copy
print?
class ViewController: UIViewController,GetMessageDelegate
{
var _button:UIButton?
var _label:UILabel?
override func viewDidLoad()
{
super.viewDidLoad()
self.title = "RootViewController"
//创建label 用来接收传过来的值
_label = UILabel(frame: CGRect(x: 50, y: 100, width: 220, height: 44))
_label?.text = "get message from next page"
_label?.textAlignment = NSTextAlignment.Center
_label?.backgroundColor = UIColor.cyanColor()
self.view.addSubview(_label!)
//创建button 点击跳转到下一个界面
_button = UIButton(frame:CGRect(x:60,y:200,width:200,height:44))
_button?.setTitle("go to next page", forState: UIControlState.Normal)
_button?.setTitleColor(UIColor.yellowColor(), forState: UIControlState.Normal)
_button?.backgroundColor = UIColor.blueColor()
_button?.addTarget(self, action: "nextPage", forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(_button!)
}
//push
func nextPage()
{
let nextVC = NextViewController()
//指定代理
nextVC.delegate = self
self.navigationController!.pushViewController(nextVC,animated:true)
}
//接收传过来的值
func getMessage(controller:NextViewController,string:String)
{
_label?.text = string
if(string == "")
{
_label?.text = "null"
}
}
}
view plain
copy
print?
//创建协议
protocol GetMessageDelegate:NSObjectProtocol
{
//回调方法 传一个String类型的值
func getMessage(controller:NextViewController,string:String)
}
class NextViewController: UIViewController
{
var delegate:GetMessageDelegate?
var _textField:UITextField?
override func viewDidLoad()
{
super.viewDidLoad()
self.title = "SecondViewController"
self.view.backgroundColor = UIColor.whiteColor()
//创建textField 用来输入要传的值
_textField = UITextField(frame: CGRect(x: 60, y: 100, width: 200, height: 44))
_textField?.borderStyle = UITextBorderStyle.RoundedRect
_textField?.placeholder = "input sth to send back"
self.view.addSubview(_textField!)
//创建返回的button
var myButton = UIButton(frame:CGRect(x:60,y:200,width:200,height:44))
myButton.center = CGPointMake(160,200)
myButton.setTitle("send message back",forState:.Normal)
myButton.addTarget(self,action:"goBack",forControlEvents:.TouchUpInside)
myButton.backgroundColor = UIColor.blueColor()
self.view.addSubview(myButton)
}
func goBack()
{
//调用代理方法
if((delegate) != nil)
{
delegate?.getMessage(self,string:_textField!.text)
self.navigationController?.popToRootViewControllerAnimated(true)
}
}
}
具体的代码,详见Demo:链接: http://download.csdn.net/download/riven_wn/9401970
rootViewController
[objc]view plain
copy
print?
class ViewController: UIViewController,GetMessageDelegate
{
var _button:UIButton?
var _label:UILabel?
override func viewDidLoad()
{
super.viewDidLoad()
self.title = "RootViewController"
//创建label 用来接收传过来的值
_label = UILabel(frame: CGRect(x: 50, y: 100, width: 220, height: 44))
_label?.text = "get message from next page"
_label?.textAlignment = NSTextAlignment.Center
_label?.backgroundColor = UIColor.cyanColor()
self.view.addSubview(_label!)
//创建button 点击跳转到下一个界面
_button = UIButton(frame:CGRect(x:60,y:200,width:200,height:44))
_button?.setTitle("go to next page", forState: UIControlState.Normal)
_button?.setTitleColor(UIColor.yellowColor(), forState: UIControlState.Normal)
_button?.backgroundColor = UIColor.blueColor()
_button?.addTarget(self, action: "nextPage", forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(_button!)
}
//push
func nextPage()
{
let nextVC = NextViewController()
//指定代理
nextVC.delegate = self
self.navigationController!.pushViewController(nextVC,animated:true)
}
//接收传过来的值
func getMessage(controller:NextViewController,string:String)
{
_label?.text = string
if(string == "")
{
_label?.text = "null"
}
}
}
secondViewController
[objc]view plain
copy
print?
//创建协议
protocol GetMessageDelegate:NSObjectProtocol
{
//回调方法 传一个String类型的值
func getMessage(controller:NextViewController,string:String)
}
class NextViewController: UIViewController
{
var delegate:GetMessageDelegate?
var _textField:UITextField?
override func viewDidLoad()
{
super.viewDidLoad()
self.title = "SecondViewController"
self.view.backgroundColor = UIColor.whiteColor()
//创建textField 用来输入要传的值
_textField = UITextField(frame: CGRect(x: 60, y: 100, width: 200, height: 44))
_textField?.borderStyle = UITextBorderStyle.RoundedRect
_textField?.placeholder = "input sth to send back"
self.view.addSubview(_textField!)
//创建返回的button
var myButton = UIButton(frame:CGRect(x:60,y:200,width:200,height:44))
myButton.center = CGPointMake(160,200)
myButton.setTitle("send message back",forState:.Normal)
myButton.addTarget(self,action:"goBack",forControlEvents:.TouchUpInside)
myButton.backgroundColor = UIColor.blueColor()
self.view.addSubview(myButton)
}
func goBack()
{
//调用代理方法
if((delegate) != nil)
{
delegate?.getMessage(self,string:_textField!.text)
self.navigationController?.popToRootViewControllerAnimated(true)
}
}
}
相关文章推荐
- 【swift_4】swift之代理传值(delegate的用法)
- Swift之delegate代理和Block闭包的反向传值
- swift-delegate(代理)或者block传值
- 导航控制器下UIViewController之间的传值 ------ 代理(delegate)传值 单例传值 Target-Action传值 属性传值 block传值 通知传值
- iOS学习11--多界面使用代理传值delegate
- Swift中Delegate代理声明和实现
- 【转】iOS 开发之协议protocal-代理传值delegate
- iOS学习笔记-----使用代理(Delegate)的页面传值
- ios 页面传值4种方式(四) 之通过delegate(代理)
- Swift UIViewController中的delegate方式传值
- Swift 传值之代理传值
- Swift 使用代理和闭包(closure)反向传值
- View 与 Controller 之间的delegate(代理)传值
- 常见的6种传值 ------ 代理(delegate)传值 单例传值 Target-Action传值 属性传值 block传值 通知传值
- Snail—UI学习之代理传值Delegate
- Swift 协议代理传值(十)
- swift 中怎么取得UIApplication的对象实例,并且利用appdelegate传值~
- 导航控制器下UIViewController之间的传值 ------ 代理(delegate)传值 单例传值 Target-Action传值 属性传值
- swift详解之二十二-----------UINavigationController的基本用法和页面传值几种方式
- Swift代理方法反向传值