swift 常用控件


 // 以下是label button imageView 的设置


class ViewController: UIViewController {


    override func viewDidLoad() {

        super.viewDidLoad()

        

        self.createLabel()

        self.createButton()

        self.createImageView()

        self.check()

 

    }



    

    func createLabel()

    {

        let label:UILabel=UILabel(frame:CGRect(x:20, y: 20, width:180, height: 30));

        // label.text="hello world";

        self.view.addSubview(label);

        label.backgroundColor=UIColor.blueColor();

        label.font=UIFont.systemFontOfSize(13);

        label.textColor=UIColor.redColor();

        label.layer.cornerRadius=10;

        label.layer.masksToBounds=true;

        label.textAlignment=NSTextAlignment.Center;

        label.lineBreakMode=NSLineBreakMode.ByWordWrapping;

        let text:NSMutableAttributedString=NSMutableAttributedString(string:"hello world 你好世界");

        

        

        text.addAttribute(NSBaselineOffsetAttributeName, value: -10, range:NSMakeRange(2,3));

        label.attributedText=text;

    }

    

    func createButton()

    {

        let button:UIButton=UIButton(frame:CGRect(x: 20, y:80, width: 100, height:40));

        button.backgroundColor=UIColor.grayColor();

        button.addTarget(self, action:"click" , forControlEvents:UIControlEvents.TouchUpInside)

        self.view.addSubview(button);

        button.setTitle("AAAAA", forState:UIControlState.Normal)

        

        

    }

    

    func click()

    {

        print("llllll");

    }

    

    func createImageView()

    {

    let imageView:UIImageView=UIImageView(frame:CGRect(x: 30, y:200, width: 100, height:40))

       imageView.backgroundColor=UIColor.redColor()

        //imageView.image=UIImage(named: "")

       self.view .addSubview(imageView)

    

    

     }

    

    func check()

    {

        let success:Bool=true

        

        

        if success//注意这里的判断条件必须是布尔类型

        {

            print("success")

            

        }else

        {

            print("fail")

        }

        var i=0

        

        for i in1...5

        {

           print("\(i)")

        }

  

    }

 


}


*********************************************************

建立一个新的VC 在app delegate将其作为rootVc

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject:AnyObject]?) -> Bool {

       

        let vc:FirstViewController=FirstViewController()

        

        let rect:CGRect=UIScreen.mainScreen().bounds

        self.window=UIWindow(frame:rect)

        self.window?.backgroundColor=UIColor.whiteColor()

        self.window?.makeKeyAndVisible()

        self.window?.rootViewController=vc;

        // swift 是不需要导入自己建立的文件的,建立之后直接在任何地方使用

         

        return true

    }


在这个VC 里面建立tableView

import UIKit


private var StableView:UITableView?// 对于全局变量必须开始赋值 这句等价于 var StableView:UITableView=nil



class FirstViewController:UIViewController, UITableViewDelegate,UITableViewDataSource   {//遵循的协议用逗号隔开


    override func viewDidLoad() {

        super.viewDidLoad()

         self.view.backgroundColor=UIColor.grayColor()

        StableView=UITableView(frame:CGRect(x: 0, y:100, width: 300, height:400), style: UITableViewStyle.Plain)

        StableView?.backgroundColor=UIColor.redColor()

        StableView?.delegate=self

        StableView?.dataSource=self

        self.view.addSubview(StableView!)// StableView在定义的时候打了?代表可有值可没有值 这里! 代表我确定有值强制拆封

       

    }


    override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

        // Dispose of any resources that can be recreated.

    }

    

    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {

        return 80

    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) ->Int {

        return 4

    }

    

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        var cell:UITableViewCell=UITableViewCell(style:UITableViewCellStyle.Default, reuseIdentifier:"ID")

        

        

        return cell;

    }

    

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

        print("select")

    }

   

}


在swift中是没有.h文件的 所以我们对变量的访问控制是用以下的方法:

Access Control Levels

Swift provides three levels of access control: public, internal, and private. You can mark a declaration with one of the access-level modifiers below to specify the declaration’s access level. Access control is discussed in detail in Access Control.

public

Apply this modifier to a declaration to indicate the declaration can be accessed by code in the same module as the declaration. Declarations marked with the public access-level modifier can also be accessed by code in a module that imports the module that contains that declaration.

internal

Apply this modifier to a declaration to indicate the declaration can be accessed only by code in the same module as the declaration. By default, most declarations are implicitly marked with the internal access-level modifier.

private

Apply this modifier to a declaration to indicate the declaration can be accessed only by code in the same source file as the declaration.

我总结一下:一般我们都用 private和 internal  ,internal是变量默认的访问权限属性.至于public 定义的变量不仅整个app范围内可以访问,甚至其他app其他语言都可以访问.





  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值