日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

【IOS】自定义UIAlertView样式,实现可替换背景和按钮

發(fā)布時(shí)間:2024/9/30 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【IOS】自定义UIAlertView样式,实现可替换背景和按钮 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

【原創(chuàng)作品, 歡迎轉(zhuǎn)載,轉(zhuǎn)載請(qǐng)?jiān)诿黠@處注明! 謝謝。????

??? 原文地址:http://blog.csdn.net/toss156/article/details/7552075


???

???? ?UIAlertView 是一個(gè)十分常用的控件,網(wǎng)上也有好多類似的自定義AlertView的方法。但是感覺(jué)效果都不是很好,它們有的是在系統(tǒng)自帶的上面添加文本框,也有的是完全自己用UIView來(lái)實(shí)現(xiàn),還有的就是繼承了UIAlertView 。

????? 今天給大家?guī)?lái)的這個(gè)UIAlertView ,它也是繼承了UIAlertView,然后屏蔽了系統(tǒng)的背景圖片,和 按鈕,然后自己添加,事件響應(yīng),從而完成了樣式的自定義,這樣做的好處是保留了 UIAlertView的模態(tài)窗口。

?

最終的效果圖:

?

?

[cpp]?view plaincopy
  • //??
  • //??JKCustomAlert.m??
  • //??AlertTest??
  • //??
  • //??Created?by??on?12-5-9.??
  • //??Copyright?(c)?2012年?__MyCompanyName__.?All?rights?reserved.??
  • //??
  • ??
  • #import?<UIKit/UIKit.h>??
  • @protocol?JKCustomAlertDelegate?<NSObject>??
  • @optional??
  • -?(void)alertView:(UIAlertView?*)alertView?clickedButtonAtIndex:(NSInteger)buttonIndex;??
  • @end??
  • ??
  • @interface?JKCustomAlert?:?UIAlertView?{??
  • ????id??JKdelegate;??
  • ????UIImage?*backgroundImage;??
  • ????UIImage?*contentImage;??
  • ????NSMutableArray?*_buttonArrays;??
  • ??
  • }??
  • ??
  • @property(readwrite,?retain)?UIImage?*backgroundImage;??
  • @property(readwrite,?retain)?UIImage?*contentImage;??
  • @property(nonatomic,?assign)?id?JKdelegate;??
  • -?(id)initWithImage:(UIImage?*)image?contentImage:(UIImage?*)content;??
  • -(void)?addButtonWithUIButton:(UIButton?*)?btn;??
  • @end??

  • ?

    [cpp]?view plaincopy
  • //??
  • //????
  • //??JKCustomAlert.m??
  • //??AlertTest??
  • //??
  • //??Created?by??on?12-5-9.??
  • //??Copyright?(c)?2012年?__MyCompanyName__.?All?rights?reserved.??
  • //??
  • ??
  • #import?"JKCustomAlert.h"??
  • ??
  • @interface?JKCustomAlert?()??
  • ????@property(nonatomic,?retain)?NSMutableArray?*_buttonArrays;??
  • @end??
  • ??
  • @implementation?JKCustomAlert??
  • ??
  • @synthesize?backgroundImage,contentImage,_buttonArrays,JKdelegate;??
  • ??
  • -?(id)initWithImage:(UIImage?*)image?contentImage:(UIImage?*)content{??
  • ????if?(self?==?[super?init])?{??
  • ??????????
  • ????????self.backgroundImage?=?image;??
  • ????????self.contentImage?=?content;??
  • ????????self._buttonArrays?=?[NSMutableArray?arrayWithCapacity:4];??
  • ????????}??
  • ????return?self;??
  • }??
  • ??
  • -(void)?addButtonWithUIButton:(UIButton?*)?btn??
  • {??
  • ????[_buttonArrays?addObject:btn];??
  • }??
  • ??
  • ??
  • -?(void)drawRect:(CGRect)rect?{??
  • ??????
  • ????CGSize?imageSize?=?self.backgroundImage.size;??
  • ????[self.backgroundImage?drawInRect:CGRectMake(0,?0,?imageSize.width,?imageSize.height)];??
  • ??????
  • }??
  • ??
  • -?(void)?layoutSubviews?{??
  • ????//屏蔽系統(tǒng)的ImageView?和?UIButton??
  • ????for?(UIView?*v?in?[self?subviews])?{??
  • ????????if?([v?class]?==?[UIImageView?class]){??
  • ????????????[v?setHidden:YES];??
  • ????????}??
  • ?????????????
  • ???????
  • ????????if?([v?isKindOfClass:[UIButton?class]]?||??
  • ????????????[v?isKindOfClass:NSClassFromString(@"UIThreePartButton")])?{??
  • ????????????[v?setHidden:YES];??
  • ????????}??
  • ????}??
  • ??????
  • ????for?(int?i=0;i<[_buttonArrays?count];?i++)?{??
  • ????????UIButton?*btn?=?[_buttonArrays?objectAtIndex:i];??
  • ????????btn.tag?=?i;??
  • ????????[self?addSubview:btn];??
  • ????????[btn?addTarget:self?action:@selector(buttonClicked:)?forControlEvents:UIControlEventTouchUpInside];??
  • ????}??
  • ??????
  • ????if?(contentImage)?{??
  • ????????UIImageView?*contentview?=?[[UIImageView?alloc]?initWithImage:self.contentImage];??
  • ????????contentview.frame?=?CGRectMake(0,?0,?backgroundImage.size.width,?backgroundImage.size.height);??
  • ????????[self?addSubview:contentview];??
  • ????}??
  • }??
  • ??
  • -(void)?buttonClicked:(id)sender??
  • {??
  • ????UIButton?*btn?=?(UIButton?*)?sender;??
  • ??????
  • ????if?(JKdelegate)?{??
  • ????????if?([JKdelegate?respondsToSelector:@selector(alertView:clickedButtonAtIndex:)])??
  • ????????{??
  • ????????????[JKdelegate?alertView:self?clickedButtonAtIndex:btn.tag];??
  • ????????}??
  • ????}??
  • ??????
  • ????[self?dismissWithClickedButtonIndex:0?animated:YES];??
  • ??
  • }??
  • ??
  • -?(void)?show?{??
  • ????????[super?show];??
  • ????????CGSize?imageSize?=?self.backgroundImage.size;??
  • ????????self.bounds?=?CGRectMake(0,?0,?imageSize.width,?imageSize.height);??
  • ??????????
  • ??
  • }??
  • ??
  • ??
  • -?(void)dealloc?{??
  • ????[_buttonArrays?removeAllObjects];??
  • ????[backgroundImage?release];??
  • ????if?(contentImage)?{??
  • ????????[contentImage?release];??
  • ????????contentImage?=?nil;??
  • ????}??
  • ?????
  • ????[super?dealloc];??
  • }??
  • ??
  • ??
  • @end??

  • ?

    Demo下載地址:http://download.csdn.net/detail/toss156/4289966

    總結(jié)

    以上是生活随笔為你收集整理的【IOS】自定义UIAlertView样式,实现可替换背景和按钮的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

    如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。