zoukankan      html  css  js  c++  java
  • 系统控件和自定义控件

    如下图创建一个导航视图控制器 三个场景,第二个场景和第三个有线连接modal

     

    上面的Edit是一个item

    中间那个的控制器

    import UIKit
    
    class EditModeTableviewControll: UITableViewController {
        
        
        
        var persons = Person.getDate()
        var per:Person?
        
        override func viewDidLoad() {
           
            super.viewDidLoad()
           //自定义的
            self.navigationItem.rightBarButtonItem=editButtonItem()
        }
        @IBAction func toEdit(sender: UIBarButtonItem) {
            tableView.setEditing(true, animated: true)
        }
        override func didReceiveMemoryWarning() {
           
            super.didReceiveMemoryWarning()
        }
        override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            
            return persons.count
        }
        override func tableView(tableView: UITableView, willSelectRowAtIndexPath indexPath: NSIndexPath) -> NSIndexPath? {
            
            per = persons[indexPath.row]
            
            return indexPath
        }
        
        
        override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
            
            let cell=tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath:indexPath)
            
            let p=persons[indexPath.row]
            
            cell.textLabel?.text = p.name
           
            return cell
        }
    //这个函数实现功能
    override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { if editingStyle == .Delete { persons.removeAtIndex(indexPath.row) tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade) }else{ let p = Person(name:"new") persons.append(p) tableView.insertRowsAtIndexPaths([NSIndexPath(forRow:indexPath.row+1,inSection:0)], withRowAnimation: .Fade) } }
    //写行样式
    override func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle { if indexPath.row == persons.count - 1{ return .Insert }else{ return .Delete } }
    //跳转的时候传值
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { let dest = segue.destinationViewController as! myviewdetil dest.person=per! dest.persondel = self } } //进行刷新 extension EditModeTableviewControll : persondelegate { func person() { tableView.reloadData() } }

    person 类

    import Foundation
    class Person{
        var name:String
        init(name:String){
         self.name = name
        
        }
        static func getDate()->[Person]{
         return [
            Person(name:"1111"),
            Person(name:"2222"),
            Person(name:"3333"),
            Person(name:"4444")
            
            
            ]
        
        
        }
    
    
    
    }

    第三个视图的

    import UIKit
    
    class myviewdetil: UIViewController {
        var person:Person?
        var persondel: persondelegate?
        
        @IBOutlet weak var mytxt: UITextField!
        
        override func viewDidLoad() {
            super.viewDidLoad()
            mytxt.text = person?.name
            
            
        }
        
        @IBAction func tojumplist(sender: UIButton) {
             person?.name = mytxt.text!
            persondel?.person()
            dismissViewControllerAnimated(true, completion: nil)
            
        }
    }

    delelegate

    import Foundation
    protocol persondelegate{
    
    
        func person()
    
    
    }

    上面的是系统的控件如下图写一个自定义的控件

    这个是放图片的地方

    如图创建一个场景

    下面是场景的控制器

    import UIKit
    
    class mytableviewcontroll: UIViewController {
        
        @IBOutlet weak var rartingyou: RatingControll!
        @IBOutlet weak var mylable: UILabel!
        
        override func viewDidLoad() {
            super.viewDidLoad()
           let vc = view.viewWithTag(1) as! RatingControll
            vc.startDe = self
           rartingyou.rating = 3
            mylable.text = "当前评价值指数:(rartingyou.rating)星"
            // Do any additional setup after loading the view.
        }
        
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
       
    
    }
    extension mytableviewcontroll : startdelegate {
        func Startcount(sender:RatingControll) {
           mylable.text = "当前评价值指数:(sender.rating)星"
        }
    }

    视图里面的view控制器

    import UIKit
    
    class RatingControll:UIView{
    var startDe:startdelegate?
      var rating = 0 //表示当前好评值
      var ratingcount = 5 //内部数量
      let spacing = 5 //表示按钮间的距离
      var buttons = [UIButton]()
        //把按钮创建出来
        required init?(coder aDecoder: NSCoder){
          super.init(coder:aDecoder)
            for _ in 0..<ratingcount {
                let btn = UIButton(frame: CGRect(x:0, y:0, 44, height:44))
                btn.addTarget(self, action: "btnClicked:", forControlEvents: .TouchDown)
                btn.setImage(UIImage(named: "empty"), forState: .Normal)
                 btn.setImage(UIImage(named: "filled"), forState: .Selected)
                btn.adjustsImageWhenHighlighted = false
                buttons.append(btn)
                addSubview(btn)
            }
        
        
        }
    //代表星星的位置
    override func layoutSubviews() { for (index,btn) in buttons.enumerate(){ btn.frame.origin.x = CGFloat((44 + spacing ) * index) } update() }
    // func btnClicked(btn: UIButton) { rating
    = buttons.indexOf(btn)!+1 update()
    // 进行传參 startDe
    ?.Startcount(self) // print("bynclick") } //设置星星的样式 func update() { for (index,btn) in buttons.enumerate() { if index < rating { btn.selected = true }else { btn.selected = false } } }

    startdelegate

    import Foundation
    protocol startdelegate{
    
        func Startcount(sender:RatingControll);
    
    }
  • 相关阅读:
    单例模式
    反射常见方法
    字符流,字节流总结
    泛型限定
    随机数判断最值
    字符流与字节流练习
    文件常见操作属性
    文件过滤器
    字符流读取文件
    目前最流行的IT编程语言
  • 原文地址:https://www.cnblogs.com/kangniuniu/p/5055289.html
Copyright © 2011-2022 走看看