用过java中的swing和awt开发过的同仁有没有发现,和IOS的开发的流程极其相似啊。接着上一篇的文章,这篇文章主要是了解UIButton的使用过程,其实相对来说还是非常简单的。我们再创建一个方法,方法名为createUIButton,代码如下
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.view.addSubview(createUILabel())
self.view.addSubview(createUIButton())
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func createUILabel()->UILabel{
var rectRect:CGRect = CGRectMake(20, 60, 60, 80)
var label:UILabel = UILabel(frame:rectRect)
label.text = "here"
label.textColor = UIColor.redColor()
return label
}
func createUIButton()->UIButton{
var button:UIButton = UIButton()
var frame = CGRectMake(100, 60, 100, 60)
button.setTitleColor(UIColor.redColor(), forState: UIControlState.Normal)
button.frame = frame
button.setTitle("点我有惊喜", forState: UIControlState.Normal)
button.addTarget(self, action: "onClick", forControlEvents: UIControlEvents.TouchUpInside)
return button
}
func onClick(){
createUIAlertView()
}
func createUIAlertView(){
var alertView:UIAlertView = UIAlertView()
alertView.title = "提示"
alertView.message = "你好,我是007"
alertView.addButtonWithTitle("点击我")
alertView.show()
}
}
主要创建了一个Button,点击Button后弹出一个对话框,作为对比,以下是OC的代码
//
// ViewController.m
// TestOc
//
// Created by gitserver on 14-10-20.
// Copyright (c) 2014年 gitserver. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self createUILabel];
[self createUIButton];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)createUILabel{
UILabel * label = [[UILabel alloc] init];
label.text = @"Here";
label.textColor = [UIColor redColor];
CGRect frame = CGRectMake(60, 60, 60, 40);
label.frame = frame;
[self.view addSubview:label];
}
-(void)createUIButton{
UIButton * button = [[UIButton alloc] init];
CGRect frame = CGRectMake(60, 130, 100, 40);
button.frame = frame;
[button setTitle:@"点击我,有惊喜" forState:UIControlStateNormal];
[button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[button addTarget:self action:@selector(onClick) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
-(void)createAlertView{
UIAlertView *alertView = [[UIAlertView alloc]init];
alertView.title = @"提示";
alertView.message = @"hello Lee";
alertView.delegate = self;
[alertView show];
}
-(void)onClick{
[self createAlertView];
}
@end
(注:还是要多看官方文档,才能多了解API,下一篇主要是了解UIAlertView的使用)