前几天写的代码一直没有更新到博客,今天做了点点整理,多发几篇,以备忘记而后翻阅。这一篇主要是了解UIAlertView的使用,其实类似于android的Dialog。前一篇文章中也使用过了改控件。然而当我找类似于android中的Toast控件的时候,却发现苹果是在使用UIAlertView来实现的,用OC实现很简单,而且网络上有很多。但是用swift来实现却没有找到一篇,不过大同小异。
下面是接着上一篇UIButton来讲解的代码,内容如下:
//
// ViewController.swift
// ss
//
// Created by gitserver on 14-10-20.
// Copyright (c) 2014年 gitserver. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
var alertView:UIAlertView?
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(){
alertView = UIAlertView()
alertView!.title ="提示"
alertView!.message ="你好,我是007"
alertView!.addButtonWithTitle("点击我")
NSTimer.scheduledTimerWithTimeInterval(1, target:self, selector:"dismiss:", userInfo:alertView!, repeats:false)
alertView!.show()
}
func dismiss(timer:NSTimer){
alertView!.dismissWithClickedButtonIndex(0, animated:true)
}
}
作为对比,oc代码如下:
//
// ViewController.m
// TestOc
//
// Created by gitserver on 14-10-20.
// Copyright (c) 2014年 gitserver. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@property (retain, nonatomic) UIAlertView *alertView;
@end
@implementation ViewController
- (void)viewDidLoad {
[superviewDidLoad];
[selfcreateUILabel];
[selfcreateUIButton];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)createUILabel{
UILabel * label = [[UILabelalloc]init];
label.text = @"Here";
label.textColor = [UIColorredColor];
CGRect frame = CGRectMake(60,60,60, 40);
label.frame = frame;
[self.view addSubview:label];
}
-(void)createUIButton{
UIButton * button = [[UIButtonalloc]init];
CGRect frame = CGRectMake(60,130,100, 40);
button.frame = frame;
[button setTitle:@"点击我,有惊喜"forState:UIControlStateNormal];
[button setTitleColor:[UIColorredColor]forState:UIControlStateNormal];
[button addTarget:selfaction:@selector(onClick)forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
-(void)createAlertView{
_alertView = [[UIAlertViewalloc]init];
_alertView.title =@"提示";
_alertView.message =@"hello Lee";
_alertView.delegate =self;
[selfperformSelector:@selector(dissmissAlert:)withObject:_alertViewafterDelay:1.0];//1
// [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(dismiss:) userInfo:_alertView repeats:YES];//2
[_alertView show];
}
-(void)onClick{
[selfcreateAlertView];
}
-(void)dismiss:(NSTimer *)timer{
[_alertViewdismissWithClickedButtonIndex:0animated:YES];
}
-(void)dissmissAlert:(UIAlertView *) alert{
if(alert){
[alert dismissWithClickedButtonIndex:0animated:YES];
}
}
@end
其中在注释中标有1和2的部分可以选择,两个方法都能够实现改功能。