ios的swift 与Object-c之后使用的一些变化

首先比较一下,把ViewController当作导航的根试图控制器,

Object-c的方法

SZMyViewController *mVC =
[[SZMyViewControlleralloc] init];

UINavigationController *nav =
[[UINavigationControlleralloc]
initWithRootViewController:mVC];

self.window.rootViewController = nav;

swift的方法

var viewController = ViewController();

var nav = UINavigationController(rootViewController:viewController);

self.window!.rootViewController = nav;

下面介绍UI部分吧:两者之间对比一下

1-1、原来的 UIView 的使用

UIView
*myView = [[UIViewalloc] initWithFrame:CGRectMake(0.0,
100.0, 200.0, 200.0)];

myView.backgroundColor = [UIColor colorWithRed:155/255.0 green:155/255.0 blue:155/255.0 alpha:1.0];

[self.view addSubview:myView];

1-2、swift的UIView的使用

var myView = UIView(frame:CGRectMake(0.0, 100.0,
200.0, 200.0))

myView.backgroundColor = UIColor(red: 155/255.0, green: 155/255.0, blue: 155/255.0,
alpha: 1.0);

self.view.addSubview(myView)

2-1 原来的UILabel的使用

UILabel *mylabel= [[UILabel alloc] initWithFrame:CGRectMake(0.0, 100.0,
200.0, 200.0)];

mylabel.backgroundColor = [UIColor colorWithRed:155/255.0 green:155/255.0 blue:155/255.0 alpha:1.0];

mylabel.text = @"原来的UILabel";

//设置文字大小

mylabel.font =
[UIFontsystemFontOfSize:17];

//设置文字的颜色

mylabel.textColor = [UIColorredColor];

//设置文字的对齐方式

mylabel.textAlignment =
NSTextAlignmentCenter;

mylabel.lineBreakMode =
NSLineBreakByCharWrapping;

[self.view addSubview:mylabel];

2-2swift的UILabel的使用

var myLabel = UILabel(frame:CGRectMake(0.0, 100.0,
200.0, 200.0))

myLabel.backgroundColor = UIColor(red: 155/255.0, green: 155/255.0, blue: 155/255.0,
alpha: 1.0);

myLabel.text = "swift的UILabel";

//设置文字的大小

myLabel.font = UIFont.systemFontOfSize(30);

//设置文字颜色

myLabel.textColor = UIColor.redColor();

//设置文字的对齐方式

myLabel.textAlignment =
NSTextAlignment.Center;

myLabel.lineBreakMode =
NSLineBreakMode.ByCharWrapping;

self.view.addSubview(myLabel)

3-1原来的UIButton的使用

UIButton *myBtn =
[UIButtonbuttonWithType:UIButtonTypeCustom];

myBtn.tag=110;

myBtn.backgroundColor =
[UIColorredColor];

[myBtn addTarget:selfaction:@selector(myBtnSelector:)
forControlEvents:UIControlEventTouchUpInside];

[myBtn setTitle:@"正常"forState:UIControlStateNormal];

[myBtn setTitle:@"高亮"forState:UIControlStateHighlighted];

[self.view addSubview:myBtn];

- (void)myBtnSelector:(UIButton *)btn{

NSLog(@"按钮的tag值为==%i",btn.tag);

}

3-2swift的UIButton的使用

var myBtn = UIButton.buttonWithType (UIButtonType.Custom) as?UIButton;

myBtn!.frame = CGRectMake (0.0, 100.0, 200.0, 200.0);

//设置按钮的tag值

myBtn!.tag=110;

//设置背景颜色

myBtn!.backgroundColor = UIColor.greenColor();

//设置正常情况按钮的标题与状态

myBtn?.setTitle("正常" ,
forState : UIControlState.Normal)

myBtn?.setTitle("高亮" ,
forState :UIControlState.Highlighted);

//设置按钮的点击事件按钮

myBtn?.addTarget(self, action:"myBtnSelector:",
forControlEvents: UIControlEvents.TouchUpInside);

self.view.addSubview(myBtn);

//btn按钮的点击事件

func myBtnSelector(sender:UIButton!){

let btnIndex=sender!.tag;

println("按钮的tag值为=%i",btnIndex);

}

ios的swift 与Object-c之后使用的一些变化,布布扣,bubuko.com

时间: 2024-08-22 16:32:28

ios的swift 与Object-c之后使用的一些变化的相关文章

iOS 10 / Swift 3.0 / XCode 8 总结

1,iOS10 新增的privacy settings iOS10添加了新的权限控制范围 如果你尝试访问这些隐私数据时得到如下错误: > This app has crashed because it attempted to access privacy-sensitive > data without a usage description. The app's Info.plist must contain > an NSCameraUsageDescription key wit

iOS开发Swift篇(02) NSThread线程相关简单说明

iOS开发Swift篇(02) NSThread线程相关简单说明 一 说明 1)关于多线程部分的理论知识和OC实现,在之前的博文中已经写明,所以这里不再说明. 2)该文仅仅简单讲解NSThread在swift语境中的一些使用和注意点,别他. 3)本文涉及代码可以从https://github.com/HanGangAndHanMeimei/Code地址获得. 二 NSThread的基本使用和创建 1)基本用法(主线程|当前线程) 1 //1.获得执行该方法的当前线程 2 let currentT

[book] iOS 8 Swift Programming Cookbook

iOS 8 Swift Programming Cookbook 资源地址 http://pan.baidu.com/s/1c0hn1Gc 书籍介绍 源码截图 书籍截图

iOS开发-Swift篇-(1)

iOS开发Swift篇—简单介绍 一.简介 Swift是苹果于2014年WWDC(苹果开发者大会)发布的全新编程语言 Swift在天朝译为“雨燕”,是它的LOGO 是一只燕子,跟Objective-C一样,可以用于开发iOS.Mac应用程序 苹果从2010年7月开始设计Swift语言,耗时4年打造 Swift的语法特点 从它的语法中能看到Objective-C.JavaScript.Python等语言的影子 语法简单.代码简洁.使用方便 可与Objective-C混合使用(相互调用) 为什么要设

iOS开发Swift篇(01) 变量&常量&元组

iOS开发Swift篇(01) 变量&常量&元组 说明: 1)终于要写一写swift了.其实早在14年就已经写了swift的部分博客,无奈时过境迁,此时早已不同往昔了.另外,对于14年部分iOS开发Swift篇专题的博文也不再做任何的校正和更新,特此说明. 2)该博文对应代码可以在https://github.com/HanGangAndHanMeimei/Code获得. 一.变量和常量 01 变量和常量的定义 在swift中变量使用var来修饰,常量使用let来修饰,变量可以修改而常量不

iOS开发Swift篇—(八)函数(2)

iOS开发Swift篇—(八)函数(2) 一.函数类型 函数类型也是数据类型的一种,它由形参类型和返回值类型组成,格式是 (形参类型列表) -> 返回值类型 1 func sum(num1: Int, num2: Int) -> Int { 2 return num1 + num2 3 } sum函数的函数类型是(Int, Int) -> Int 1 func printLine() 2 { 3 println("-----------") 4 } printLine

iOS 8 Swift Programming Cookbook

Book Description "About a year ago, noticing that Apple had not updated Objective-C much over the past few years, I got intimations that they were working on a new language or framework for iOS development, and even suggested it to my friends at work

iOS开发swift语法初级篇—————(swift技术交流群:361513739)

iOS开发之swift语法初级篇:点击打开链接 swift技术交流QQ群361513739 iOS开发swift语法初级篇-----(swift技术交流群:361513739)

iOS开发Swift篇—简单介绍

iOS开发Swift篇—简单介绍 一.简介 Swift是苹果于2014年WWDC(苹果开发者大会)发布的全新编程语言 Swift在天朝译为“雨燕”,是它的LOGO 是一只燕子,跟Objective-C一样,可以用于开发iOS.Mac应用程序 苹果从2010年7月开始设计Swift语言,耗时4年打造 Swift的语法特点 从它的语法中能看到Objective-C.JavaScript.Python等语言的影子 语法简单.代码简洁.使用方便 可与Objective-C混合使用(相互调用) 为什么要设