IPhone: Custom uialertview

Source: Internet
Author: User

Because there is a need in the project: You need to select a place to save in the "save" dialog box. Select uialertview for implementation, but add the uiswitch control to uialertview. Therefore, you need to customize a class that inherits uialertview to customize uialertview.

The implementation result is as follows: (the image is not added yet)

When I click "OK", I need to know the status of the two switches before proceeding with the relevant functions.

The savealertview class is customized.

In. H, you need to define a @ protocol as the exit for outgoing switch status.

Declare the corresponding delegate. View Source Code

 # Import <Uikit/uikit. h> @ Protocol Savealertviewdelegate <nsobject> @ Optional -( Void ) Alertview :( uialertview *) Alertview clickedbuttonatindex :( nsinteger) buttonindex save2file :( bool) save2file save2album :( bool) save2album;  @ End  @ Interface  Savealertview: uialertview @ property (nonatomic, assign)  ID <Savealertviewdelegate> Savealertdelegate; @ property (readwrite, retain) uiimage * Backgroundimage; @ property (readwrite, retain) uiimage * Contentimage; @ property (strong, nonatomic) iboutlet uiswitch * Swititch; @ property (strong, nonatomic) iboutlet uiswitch *Switch2; -( ID ) Initwithimage :( uiimage *) image contentimage :( uiimage * ) Content;  @ End 

In. m, the original control of the system is hidden (implemented in layoutsubviews). add your own control and clickCode.

Hide system controls in layoutsubviews

For(Uiview * VIn[Self subviews]) {If([VClass] = [UiimageviewClass]) {[V sethidden: Yes];}If([V iskindofclass: [uibuttonClass] |[V iskindofclass: nsclassfromstring (@"Uithreepartbutton")]) {[V sethidden: Yes] ;}}

Complete. m code

 # Import   " Savealertview. h  "  @ Implementation  Savealertview  @ Synthesize  Savealertdelegate;  @ Synthesize  Backgroundimage;  @ Synthesize  Contentimage;  @ Synthesize  Switch1;  @ Synthesize  Switch2; -( ID ) Initwithframe :( cgrect) frame {self = [Super initwithframe: frame];  If  (Self ){  //  Initialization code  }  Return  Self ;} -( ID ) Initwithimage :( uiimage *) image contentimage :( uiimage * ) Content {  If (Self = [Super init]) {self. backgroundimage = Image; self. contentimage = Content ;}  Return  Self ;} -( Void  ) Drawrect :( cgrect) rect {cgsize imagesize = Self. backgroundimage. size; [self. backgroundimage drawinrect: cgrectmake (  0 , 0  , Imagesize. Width, imagesize. Height)];} -( Void  ) Layoutsubviews { //  Shielding system imageview and 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] ;}}  If  (Contentimage) {uiimageview * Contentview = [[Uiimageview alloc] initwithimage: Self. contentimage]; contentview. Frame = Cgrectmake ( 0 , 0  , Backgroundimage. Size. Width, backgroundimage. Size. Height); [self addsubview: contentview];} uilabel * Label1 = [[uilabel alloc] initwithframe: cgrectmake ( 20 ,20 , 136 , 21  )]; Label1.text = @"  Save as an editable File  "  ; [Self addsubview: label1]; uilabel * Label2 = [[uilabel alloc] initwithframe: cgrectmake ( 20 , 65 , 85 , 21  )]; Label2.text =@"  Save to album  "  ; [Self addsubview: label2]; switch1 = [[Uiswitch alloc] initwithframe: cgrectmake ( 206 , 17 , 79 , 27  )]; [Switch1 Seton: Yes]; [self addsubview: switview]; switch2 = [[Uiswitch alloc] initwithframe: cgrectmake ( 206 , 62 , 79 ,27  )]; [Self addsubview: switch2]; uibutton * Button1 = [[uibutton alloc] initwithframe: cgrectmake ( 20 , 118 , 86 , 36  )]; Button1.tag = 1  ; [Button1 settitle:  @"  OK  "  Forstate: uicontrolstatenormal]; button1.backgroundcolor =[Uicolor bluecolor]; [button1 addtarget: Self action: @ selector (buttonclicked :) forcontrolevents: uicontroleventtouchupinside]; [self addsubview: button1]; uibutton * Button2 = [[uibutton alloc] initwithframe: cgrectmake ( 199 , 118 , 86 , 36  )]; Button2.tag = 2  ; [Button2 settitle:  @"  Cancel  " Forstate: uicontrolstatenormal]; button2.backgroundcolor = [Uicolor bluecolor]; [button2 addtarget: Self action: @ selector (buttonclicked :) forcontrolevents: uicontroleventtouchupinside]; [self addsubview: button2]; self. backgroundcolor = [Uicolor graycolor];} -( Void ) Buttonclicked :( ID  ) Sender {uibutton * BTN = (uibutton * ) Sender;  If  (Savealertdelegate ){  If ([Savealertdelegate respondstoselector: @ selector (alertview: clickedbuttonatindex :)]) {[savealertdelegate alertview: Self clickedbuttonatindex: BTN. tag save2file: [switch1 ISON] save2album: [switch2 ISON] ;}} [self dismisswithclickedbuttonindex:  0  Animated: Yes];} -( Void  ) Show {[Super Show];  //  Cgsize imagesize = self. backgroundimage. size;  //  Self. bounds = cgrectmake (0, 0, imagesize. Width, imagesize. Height ); Self. Frame = cgrectmake (350 , 300 , 320 , 191  );}  @ End 

Then call the API. Do not forget to set the delegate.

 
Savealertview * savealert = [[savealertview alloc] initwithframe: cgrectmake (340,221,305,191)]; Savealert. savealertdelegate=Self; [savealert show];

 

This is almost the case.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.