iOS项目开发实战(Swift)—View之间传递数据
2017-06-21 16:21
537 查看
1.ViewController.Swift
[objc]
view plain
copy
print?
import UIKit
class ViewController: UIViewController {
let textField = UITextField()
let button = UIButton()
override func viewDidLoad() {
super.viewDidLoad()
textField.frame = CGRectMake(20, 20, 100, 50)
textField.textColor = UIColor.blueColor()
textField.placeholder = "Input:"
button.frame = CGRectMake(0, 55, 50, 50)
button.backgroundColor = UIColor.blackColor()
button.setTitle("UP", forState: UIControlState.Normal)
button.addTarget(self, action: "jump", forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(button)
self.view.addSubview(textField)
}
func jump(){
let viewController = NextViewController()
//传递数据
viewController.labelContent = textField.text!
presentViewController(viewController, animated: true, completion: nil)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
2.NextViewController.swift
[objc]
view plain
copy
print?
import UIKit
class NextViewController: UIViewController {
var label = UILabel()
var labelContent = " "
var button = UIButton()
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.whiteColor()
label.frame = CGRectMake(20, 20, 100, 50)
label.textColor = UIColor.blackColor()
label.text = labelContent
self.view.addSubview(label)
button.frame = CGRectMake(20, 80, 50, 50)
button.setTitle("Back", forState: UIControlState.Normal)
button.backgroundColor = UIColor.blackColor()
button.addTarget(self, action: "backPressed:", forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(button)
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func backPressed(sender: AnyObject){
//返回上一个视图
dismissViewControllerAnimated(true, completion: nil)
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
[objc]
view plain
copy
print?
import UIKit
class ViewController: UIViewController {
let textField = UITextField()
let button = UIButton()
override func viewDidLoad() {
super.viewDidLoad()
textField.frame = CGRectMake(20, 20, 100, 50)
textField.textColor = UIColor.blueColor()
textField.placeholder = "Input:"
button.frame = CGRectMake(0, 55, 50, 50)
button.backgroundColor = UIColor.blackColor()
button.setTitle("UP", forState: UIControlState.Normal)
button.addTarget(self, action: "jump", forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(button)
self.view.addSubview(textField)
}
func jump(){
let viewController = NextViewController()
//传递数据
viewController.labelContent = textField.text!
presentViewController(viewController, animated: true, completion: nil)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
2.NextViewController.swift
[objc]
view plain
copy
print?
import UIKit
class NextViewController: UIViewController {
var label = UILabel()
var labelContent = " "
var button = UIButton()
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.whiteColor()
label.frame = CGRectMake(20, 20, 100, 50)
label.textColor = UIColor.blackColor()
label.text = labelContent
self.view.addSubview(label)
button.frame = CGRectMake(20, 80, 50, 50)
button.setTitle("Back", forState: UIControlState.Normal)
button.backgroundColor = UIColor.blackColor()
button.addTarget(self, action: "backPressed:", forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(button)
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func backPressed(sender: AnyObject){
//返回上一个视图
dismissViewControllerAnimated(true, completion: nil)
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
相关文章推荐
- iOS项目开发实战(Swift)—View之间传递数据
- iOS项目开发实战——Swift实现多个TableView的侧滑与切换
- iOS开发点滴 - 如何通过Segue写代码传递数据从一个ViewController到另一个ViewController(Swift代码)
- iOS开发点滴 - 如何通过Segue写代码传递数据从一个ViewController到另一个ViewController(Swift代码)
- IOS菜鸟的所感所思——UITableView中数据之间的顺序与反向传递
- iOS开发拓展篇—应用之间的跳转和数据传递
- iOS的View之间的数据传递
- iOS开发拓展篇—应用之间的跳转和数据传递
- IOS开发使用委托delegate在不同窗口之间传递数据
- 在ViewController之间传递数据的方法【iOS】
- Swift-iOS-项目实战-1-开发环境篇(Mac OS10.10 + Xcode 6.0)
- [视频教程] 聚合数据 iOS 项目开发实战:条码查询器
- iOS的View之间的数据传递
- iOS开发拓展篇—应用之间的跳转和数据传递
- ios不同的viewcontroller之间数据传递方法
- iOS 应用view之间数据传递的方式
- ios delegate代理,多个/不同/两个view之间传递数据,传递批量数据,多个数据
- iOS的View之间的数据传递
- IOS开发基础教程学习笔记4 在storyboard的场景之间传递数据
- IOS开发使用委托delegate在不同窗口之间传递数据