技术文章分类(180)

技术随笔(11)

使用Swift开发iOS项目、UI创建、方法调用

 

//1、root控制器的创建
       var rootCtrl =RootViewController()
       var root:UINavigationController =UINavigationController(rootViewController: rootCtrl)
       self.window!.rootViewController = root


//2、tab控制器的创建
        var tab =UITabBarController()
        tab.tabBar.barTintColor =UIColor.blackColor()
        tab.viewControllers = [oneCtrl, twoCtrl, threeCtrl, fourCtrl, fiveCtrl]
        self.window!.rootViewController = tab


//3、声明属性
   var tableView:UITableView?


//4、抽出TableView的创建方法
   func _initTableView(){
        //TableView的创建和设置
        self.tableView=UITableView(frame:CGRectMake(0,20,CGRectGetWidth(self.view.frame),CGRectGetHeight(self.view.frame)-64))
        self.tableView!.delegate =self
        self.tableView!.dataSource =self 
        self.tableView!.autoresizingMask = UIViewAutoresizing.FlexibleHeight |UIViewAutoresizing.FlexibleWidth
        self.tableView!.registerClass(UITableViewCell.self, forCellReuseIdentifier:"cell")
        self.view?.addSubview(self.tableView)
        self.tableView!.separatorColor =UIColor.cyanColor()
   }

    //dataSource 返回100个row
   func tableView(tableView:UITableView!, numberOfRowsInSection section: Int) ->Int
   {
          return 100
   }

 

//cell的创建
   func tableView(tableView:UITableView!, cellForRowAtIndexPath indexPath:NSIndexPath!) ->UITableViewCell!
    {
      let cell = tableView .dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)asUITableViewCell
           cell.textLabel.text =String(format:"%i", indexPath.row)
       return cell
    }


UIKit
// UILabel
   func createLabel() ->UILabel {
        var label:UILabel =UILabel(frame:CGRectMake(10,80,self.view.frame.size.width-20,50))
        label.backgroundColor =UIColor.clearColor()
        label.textAlignment =NSTextAlignment.Center
        label.textColor =UIColor.blackColor()
        label.font =UIFont.systemFontOfSize(25)
        label.text ="Hello Swift"
       return label
    }
    
    // UIView
   func createView() ->UIView {
       var orginY =CGRectGetMaxY(self.myLabel.frame) +10
       var myView:UIView =UIView(frame:CGRectMake(10, orginY,self.view.frame.size.width-20,30))
           myView.backgroundColor =UIColor.whiteColor()
       return myView;
    }
    
    // UIButton
   func createButton() ->UIButton {
       var orginY =CGRectGetMaxY(self.myView.frame) +10
       var button:UIButton =UIButton(frame:CGRectMake(10, orginY,self.view.frame.size.width-20,30))
        button.backgroundColor =UIColor.greenColor()
        button.setTitle("Button", forState:UIControlState.Normal)
        button.titleLabel.font =UIFont.systemFontOfSize(12)
        button.addTarget(self, action:"tappedButton:", forControlEvents:UIControlEvents.TouchUpInside)
        button.tag =100
       return button
    }
    
    // UIImageView
   func createImageView() ->UIImageView {
       var orginY =CGRectGetMaxY(self.myButton.frame) +10
       var imageView:UIImageView =UIImageView(frame:CGRectMake((self.view.frame.size.width-100)/2, orginY,100,50))
       var image:UIImage =UIImage(named:"user")
           imageView.image = image
       return imageView
    }
    
    // Button target
   func tappedButton(sender:UIButton!) {
       println(sender.tag)
    }


  push 控制器的方法
var listCtrl:UIViewController =UIViewController()
            listCtrl.title ="View Controller"
            listCtrl.view.backgroundColor =UIColor.redColor()
       self.navigationController.pushViewController(listCtrl, animated:true)

  pop
self.navigationController.popViewControllerAnimated(true)

 

posted @ 2014-06-19 08:48  坤哥MartinLi  阅读(281)  评论(0编辑  收藏  举报