自带的uialertview 三个按钮上面的按钮,可以禁用吗

& 开源中国(OSChina.NET) |
开源中国社区(OSChina.net)是工信部
指定的官方社区下次自动登录
现在的位置:
& 综合 & 正文
自定义UIAlertView样式,实现可替换背景和按钮
UIAlertView 是一个十分常用的控件,网上也有好多类似的自定义AlertView的方法。但是感觉效果都不是很好,它们有的是在系统自带的上面添加文本框,也有的是完全自己用UIView来实现,还有的就是继承了UIAlertView 。
今天给大家带来的这个UIAlertView ,它也是继承了UIAlertView,然后屏蔽了系统的背景图片,和 按钮,然后自己添加,事件响应,从而完成了样式的自定义,这样做的好处是保留了 UIAlertView的模态窗口。
最终的效果图:
【上篇】【下篇】iOS开发小记:带输入框(TextField)的UIAlertView - isaced
December 19, 2012
iOS开发小记:带输入框(TextField)的UIAlertView
光写这篇文章的标题我就至少三次把Ctrl键按错成Alt(或Win)键,因为刚从Mac下来,恼火。。。
昨天写项目的时候有个地方需要用到UIAlertView(警告)上有一个输入框(UITextField),第一反应就是去Code4App上翻,还好翻到两个用得上的Demo,但是下下来很多错误,包括没有ARC什么的,一大堆警告。
于是就想着自己简单定制一个AlertView上添加UITextField,由于项目很简单,就没做很复杂,直接在alertView上addSubview个输入框,然后点击按钮的时候再获取出来这个输入框的值就ok了。是不是很简单呢?
下面是最初的想法,实现的代码:
//自己定义一个UITextField添加上去,后来发现ios5自带了此功能
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@&类别修改& message:@& & delegate:nil cancelButtonTitle:@&取消& otherButtonTitles:@&修改&,nil];
UITextField * txt = [[UITextField alloc] init];
txt.backgroundColor = [UIColor whiteColor];
txt.frame = CGRectMake(alert.center.x+65,alert.center.y+48, 150,23);
[alert addSubview:txt];
[alert show];
这张图就是上述代码的效果,可以看到这个输入框不是很美观,棱角很明显,没圆角,没阴影,控制也不是很方便。
这样其实是一个很笨的方法,没有封装起来一个独立的AlertView,只想投简单搞定一个类似这样功能的东西,不过还是有用的,记录下来以后方便改造。
后来发现经高人指点,ios5已经自带了上述功能,甚至更丰富,那就是 Alert 的 alertViewStyle 属性。
alertViewStyle 属性有以下三种选项:
UIAlertViewStylePlainTextInput - 添加一个普通输入框
UIAlertViewStyleSecureTextInput - 密码输入框
UIAlertViewStyleLoginAndPasswordInput - 普通输入框加密码输入框
下面分别来看看这三种属性的效果:
(UIAlertViewStylePlainTextInput)
(UIAlertViewStyleSecureTextInput)
(UIAlertViewStyleLoginAndPasswordInput)
可以看到自带的文本+密码输入框弹出的键盘有点点不一样,稍带透明。
初始化AlertView后,通过设置这个属性,达到AlertView上出现输入框的效果,然后再添加UIAlertViewDelegate代理,在下面棉纺就可以获取到这个文本框。
-(void)alertView : (UIAlertView*)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
//得到输入框
UITextField *tf=[alertView textFieldAtIndex:0];
是不是很简单呢,看来Xcode是越来越让开发者省心,这也是它不断更新带来的好处,这个只是最基本的实现方法,如果想达到一些额外的效果或者想要的功能,比如在Alert上添加一个UIImageView什么的,那就要自己动手丰衣足食了。。。当前位置:
& Swift - 告警框(UIAlertView)的用法
Swift - 告警框(UIAlertView)的用法
发布:hangge
浏览:2708
1,下面代码创建并弹出一个告警框,并带有“取消”“确定”两个按钮
(注:自IOS8起,建议使用UIAlertController。)
class ViewController: UIViewController{
override func viewDidLoad() {
super.viewDidLoad()
let alertView = UIAlertView()
alertView.title = "系统提示"
alertView.message = "您确定要离开吗?"
alertView.addButtonWithTitle("取消")
alertView.addButtonWithTitle("确定")
alertView.cancelButtonIndex=0
alertView.delegate=
alertView.show()
func alertView(alertView:UIAlertView, clickedButtonAtIndex buttonIndex: Int){
if(buttonIndex==alertView.cancelButtonIndex){
print("点击了取消")
print("点击了确认")
2,告警框有下面4种样式
Default:默认样式
PlainTextInput:带输入框的告警框
SecureTextInput:带密码框的告警框
LoginAndPasswordInput:带输入框和密码框的告警框
下面是一个使用输入框和密码框的告警框样例:
import UIKit
class ViewController: UIViewController {
var alertView = UIAlertView()
override func viewDidLoad() {
super.viewDidLoad()
alertView.title = "系统登录"
alertView.message = "请输入用户名和密码!"
alertView.addButtonWithTitle("取消")
alertView.addButtonWithTitle("确定")
alertView.cancelButtonIndex=0
alertView.delegate=
alertView.alertViewStyle = UIAlertViewStyle.LoginAndPasswordInput
alertView.show()
func alertView(alertView:UIAlertView, clickedButtonAtIndex buttonIndex: Int){
if(buttonIndex==alertView.cancelButtonIndex){
print("点击了取消")
let name = alertView.textFieldAtIndex(0)
let password = alertView.textFieldAtIndex(1)
print("用户名是:\(name!.text) 密码是:\(password!.text)")
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()为UIAlertView添加block支持 - 推酷
为UIAlertView添加block支持
系统自带的UIAlertView只能支持delegate方式. 如果你只有一个UIAlertView这种方式可能无关紧要. 但如果你有二个或多个UIAlertView, 你需要在委托方法中进行判断是哪个UIAlertView实例的产生的委托, 接着又要判断是响应哪个button. 如果你曾经这样做过, 想想这是多杂的代码. Objective-C是支持块代码的, 如果对UIAlertView添加块支持, 那岂不是一个美事.
这里推荐一个开源的实现:
如果你的项目使用Cocoapods管理. 在Podfile添加下面代码增加支持
pod &UIAlertView-Blocks&, &~& 1.0&
再使用命令更新
pod update
// 添加头文件
#import &UIAlertView+Blocks.h&
NSString *title = NSLocalizedString(@&Alert&, nil);
NSString *message = NSLocalizedString(@&UIAlertView-Blocks&, nil);
NSString *cancelButtonTitle = NSLocalizedString(@&Cancel&, nil);
NSString *otherTitle = NSLocalizedString(@&Set&, nil);
RIButtonItem *cancelButtonItem = [RIButtonItem itemWithLabel:cancelButtonTitle action:^{
NSLog(@&Press Button Cancel&);
RIButtonItem *otherButtonItem = [RIButtonItem itemWithLabel:otherTitle action:^{
NSLog(@&Press Button OK&);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:message cancelButtonItem:cancelButtonItem otherButtonItems:otherButtonItem, nil];
[alert show];
除了这种使用方式, &UIAlertView-Blocks还支持其它方法, 可以参考一下它的github主页.
已发表评论数()
请填写推刊名
描述不能大于100个字符!
权限设置: 公开
仅自己可见
正文不准确
标题不准确
排版有问题
主题不准确
没有分页内容
图片无法显示
视频无法显示
与原文不一致

我要回帖

更多关于 uialertview按钮颜色 的文章

 

随机推荐