首先比较一下,把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
原文地址:http://www.cnblogs.com/henanshenzhen/p/3777486.html