iOS项目开发实战(Swift)—View之间传递数据

来源:互联网 发布:淘宝网免费开店 编辑:程序博客网 时间:2024/06/04 18:11

1.ViewController.Swift

[objc] view plain copy print?
  1. import UIKit  
  2.   
  3. class ViewController: UIViewController {  
  4.     let textField = UITextField()  
  5.     let button = UIButton()  
  6.   
  7.     override func viewDidLoad() {  
  8.         super.viewDidLoad()  
  9.         textField.frame = CGRectMake(202010050)  
  10.         textField.textColor = UIColor.blueColor()  
  11.         textField.placeholder = "Input:"  
  12.           
  13.         button.frame = CGRectMake(0555050)  
  14.         button.backgroundColor = UIColor.blackColor()  
  15.         button.setTitle("UP", forState: UIControlState.Normal)  
  16.         button.addTarget(self, action"jump", forControlEvents: UIControlEvents.TouchUpInside)  
  17.         self.view.addSubview(button)  
  18.         self.view.addSubview(textField)  
  19.     }  
  20.       
  21.     func jump(){  
  22.         let viewController = NextViewController()  
  23.         //传递数据  
  24.         viewController.labelContent = textField.text!  
  25.         presentViewController(viewController, animatedtrue, completion: nil)  
  26.     }  
  27.   
  28.     override func didReceiveMemoryWarning() {  
  29.         super.didReceiveMemoryWarning()  
  30.         // Dispose of any resources that can be recreated.  
  31.     }  
  32.   
  33.   
  34. }  


2.NextViewController.swift

[objc] view plain copy print?
  1. import UIKit  
  2.   
  3. class NextViewController: UIViewController {  
  4.   
  5.     var label = UILabel()  
  6.     var labelContent = " "  
  7.     var button = UIButton()  
  8.     override func viewDidLoad() {  
  9.         super.viewDidLoad()  
  10.         self.view.backgroundColor = UIColor.whiteColor()  
  11.         label.frame = CGRectMake(202010050)  
  12.         label.textColor = UIColor.blackColor()  
  13.         label.text = labelContent  
  14.         self.view.addSubview(label)  
  15.           
  16.         button.frame = CGRectMake(20805050)  
  17.         button.setTitle("Back", forState: UIControlState.Normal)  
  18.         button.backgroundColor = UIColor.blackColor()  
  19.         button.addTarget(self, action"backPressed:", forControlEvents: UIControlEvents.TouchUpInside)  
  20.         self.view.addSubview(button)  
  21.         // Do any additional setup after loading the view.  
  22.     }  
  23.   
  24.     override func didReceiveMemoryWarning() {  
  25.         super.didReceiveMemoryWarning()  
  26.         // Dispose of any resources that can be recreated.  
  27.     }  
  28.       
  29.     func backPressed(sender: AnyObject){  
  30.         //返回上一个视图  
  31.         dismissViewControllerAnimated(true, completion: nil)  
  32.     }  
  33.   
  34.     /* 
  35.     // MARK: - Navigation 
  36.  
  37.     // In a storyboard-based application, you will often want to do a little preparation before navigation 
  38.     override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
  39.         // Get the new view controller using segue.destinationViewController. 
  40.         // Pass the selected object to the new view controller. 
  41.     } 
  42.     */  
  43.   

阅读全文
0 0
原创粉丝点击