// 以下是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
}
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")
}
}
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.
-
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. -
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. -
Apply this modifier to a declaration to indicate the declaration can be accessed only by code in the same source file as the declaration.
public
internal
private