苹果开发之路--swift之UIButton

3 篇文章 0 订阅
1 篇文章 0 订阅

     用过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的使用)


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值