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);
}
4-1原来的UISlider的使用
UISlider *mySlider = [[UISlideralloc] init];
mySlider.frame = CGRectMake(0.0,100.0,200.0,30.0);
//设置最小值
mySlider.minimumValue=0.0;
//设置最大值
mySlider.maximumValue=1.0;
[mySlider addTarget:selfaction:@selector(changeValue:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:mySlider];
- (void)changeValue:(UISlider *)slider{
NSLog(@"我的值==%f",slider.value);
}
4-2swift的UISlider的使用
var mySlider = UISlider();
mySlider.frame = CGRectMake(0.0,100.0,200.0,30.0);
//绑定方法
mySlider.addTarget(self, action: "changeValue:", forControlEvents : UIControlEvents.ValueChanged);
//设置最小值、
mySlider.minimumValue=0.0;
//设置最大值
mySlider.maximumValue=1.0;
self.view.addSubview(mySlider);
//当滑竿滑动的时候调用
func changeValue(sender:UISlider){
let currentValue = sender.value;
println("当前的值为==%f",currentValue);
}