Swift_基础UI

UILabel

用于显示文本的控件,继承于UIView,实现来NSCoding协议

class UILabel : UIView, NSCoding {...}

基本使用

let label = UILabel(frame:CGRect(origin: CGPointMake(10.0, 50.0), size: CGSizeMake(150,50)))             
label.text = "This is a Label" 
self.view.addSubview(label) 

UIButton

按钮控件继承于UIControl ,实现NSCoding 协议,UIControl继承于UIView,UIControl 实现的添加点击时间函数

func addTarget(target: AnyObject?, action: Selector, forControlEvents controlEvents: UIControlEvents)

// remove the target/action for a set of events. pass in NULL for the action to remove all actions for that target
func removeTarget(target: AnyObject?, action: Selector, forControlEvents controlEvents: UIControlEvents)

基本使用

    let btn = UIButton(frame: CGRect(origin: CGPointMake(20.0, 100.0), size: CGSizeMake(150,50)))
    btn.setTitle("clickme", forState: UIControlState.Normal)
    btn.backgroundColor = UIColor.blueColor()
    btn.addTarget(self, action: "btnClickMe:", forControlEvents:UIControlEvents.TouchUpInside)
    self.view.addSubview(btn)


func btnClickMe(sender:UIButton){
    NSLog("btn clicked")
}

UIAlertView

弹出框控件,使用时实现 UIAlertViewDelegate,
基本使用

func btnClickMe(sender:UIButton){
    NSLog("btn clicked")
    var alertView = UIAlertView()
    alertView.title = "Tips"
    alertView.message = "密码错误"
    alertView.delegate = self
    alertView.addButtonWithTitle("Cancel")
    alertView.addButtonWithTitle("OK")
    alertView.show()
}

func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int) {
    NSLog("btn clicked \(buttonIndex)")//index 按照声明的顺序从0开始
}

UISwitch

开关按钮

    let switchBtn:UISwitch = UISwitch(frame: CGRect(origin: CGPointMake(100, 200), size: CGSizeMake(0, 0)))
    //未选中颜色,只能显示边框
    switchBtn.tintColor = UIColor.redColor()
    //小按钮
    switchBtn.thumbTintColor = UIColor.greenColor()
    //选中颜色
    switchBtn.onTintColor = UIColor.blueColor()
    self.view.addSubview(switchBtn)

弃用Storyboard

  • 创建一个SingleView项目

  • 删除Main.storyboard文件

  • 删除info.plist里的main的引用

       Main stroyboard file base name
    
  • 补充AppDelegate 文件中的 func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool 方法

 func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    
    self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
    // Override point for customization after application launch.
    self.window!.backgroundColor = UIColor.whiteColor()
    self.window!.makeKeyAndVisible()
    //定义一个视图控制器
    let one_vc = ViewController();
    //创建导航控制器
    let nvc=UINavigationController(rootViewController:one_vc);
    //设置根视图
    self.window!.rootViewController=nvc;
    self.window!.makeKeyAndVisible()
    
    
    return true
}

UITableView

声明一个tableView ,Controller实现 UITableViewDataSource, UITableViewDelegate 协议

var tableView : UITableView?

设置相关代码

    // 初始化tableView的数据
    self.tableView=UITableView(frame:self.view.frame,style:UITableViewStyle.Plain)
    // 设置tableView的数据源
    self.tableView!.dataSource=self
    // 设置tableView的委托
    self.tableView!.delegate = self
    self.tableView!.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
    self.view.addSubview(self.tableView!)

实现几个回调函数

//列表的item数量
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 10
}

// item的内容
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
    return UITableViewCell()
}

// 选中函数回调
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • *面试心声:其实这些题本人都没怎么背,但是在上海 两周半 面了大约10家 收到差不多3个offer,总结起来就是把...
    Dove_iOS阅读 27,397评论 30 472
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,546评论 4 61
  • 1、UILabel的创建 1.1、Label基本应用 1)label的基本应用代码示例: 2)带有阴影效果的lab...
    上帝也是码农阅读 7,026评论 0 1
  • 7、不使用IB是,下面这样做有什么问题? 6、请说说Layer和View的关系,以及你是如何使用它们的。 1.首先...
    AlanGe阅读 4,415评论 0 1
  • 单行 文/落枫 孤鸟寻林狐巢近 行猎追痕野身临 一度非门天何处 路上秋风瑟人魂
    ToyIHere阅读 1,373评论 0 1

友情链接更多精彩内容