IOS8 Alertview and Actionsheet incompatibility problem solutions

Source: Internet
Author: User

Specific code at the end of the article. Show me how to use it first.

The situation of Alertview

-  (void) Creatalertview{    uialertview *alertview = [[uialertview  alloc] initwithtitle:@ "title"  message:@ "Message"  delegate:self cancelbuttontitle:@ " Cancel " otherbuttontitles:@" OK ",  nil];    [alertview show];} #pragma  mark UIAlertViewDelegate-  (void) Alertview: (uialertview *) alertview  Clickedbuttonatindex: (Nsinteger) buttonindex{    switch  (buttonIndex)  {         case 0:                         break;         case 1:                         break;                     default:             break;    }} #pragma  mark use newobject (AlertView)-  ( void) Creatalertview{    boalertcontroller *alertview = [[boalertcontroller  alloc] initwithtitle:@ "title"  message:@ "message"  viewController:self];         ributtonitem *cancelitem = [ributtonitem itemwithlabel:@ "Cancel"  action:^{            }];     [alertView addButton:cancelItem type:RIButtonItemType_Cancel];         ributtonitem *okitem = [ributtonitem itemwithlabel:@ "OK"  action:^{             }];    [alertview  addbutton:okiteM type:ributtonitemtype_destructive];    [alertview show];} 

Actionsheet

-  (void) creatactionsheet{    uiactionsheet *actionsheet = [[ uiactionsheet alloc] initwithtitle:@ "title"  delegate:self cancelbuttontitle:@ "Cancel"   destructivebuttontitle:@ "OK"  otherbuttontitles:@ "other functions",  nil];    [actionsheet  showinview:self.view];} #pragma  mark UIActionSheetDelegate-  (void) Actionsheet: (uiactionsheet *) actionsheet  Clickedbuttonatindex: (Nsinteger) buttonindex{    switch  (buttonIndex)  {         case 0:                         break;         case 1:                         break;                     default:             break;    }} #pragma  mark use newobject (Actionsheet)-  (void) creatactionsheet{     boalertcontroller *actionsheet = [[boalertcontroller alloc] initwithtitle:@ "title"  message:nil viewController:self];        RIButtonItem  *cancelitem = [ributtonitem itemwithlabel:@ "Cancel"  action:^{             }];    [actionsheet addbutton:cancelitem  type:ributtonitemtype_cancel];        ributtonitem *okitem  = [ributtonitem itemwithlabel:@ "OK"  action:^{             }];    [actionsheet addbutton:okitem type:ributtonitemtype_destructive];         [actionsheet showinview:self.view];}


The use of this encapsulation class requires extended class Uialertview-blocks support. Https://github.com/jivadevoe/UIAlertView-Blocks


Encapsulate Class Code

  Created by  CMA  on 14-9-25.//  Copyright  (c)  2014 year   yipinapp.ibrand. all rights reserved.//typedef enum {     ributtonitemtype_cancel         = 1,     ributtonitemtype_destructive       ,     Ributtonitemtype_other}ributtonitemtype;typedef enum {    boalertcontrollertype_ Alertview    = 1,    boalertcontrollertype_actionsheet} Boalertcontrollertype, #define  isIOS8   ([[Uidevice currentdevice]systemversion]floatvalue] >=8) #import  <Foundation/Foundation.h> #import   "RIButtonItem.h" @interface   boalertcontroller : nsobject-  (ID) initwithtitle: (nsstring *) Title message: (NSString  *) Message viewcontroller: (uiviewcontroller *)inviewcontroller;-  (void) AddButton: (ributtonitem *) Button type: (Ributtonitemtype) itemType;// show actionsheet in all versions-  (void) Showinview: (uiview *) view;//Show  alertview in all versions-  (void) show; @end #import  "BOAlertController.h" #import   " Uiactionsheet+blocks.h "#import   uialertview+blocks.h" @interface  boalertcontroller () @property   ( Nonatomic, strong)  NSString               *title; @property   (nonatomic, strong)  NSString               *message; @property   (nonatomic, weak)   uiviewcontroller        *inviewcontroller; @property   (nonatomic,  strong)  NSMutableDictionary   *buttonInfoDict; @end @implementation  boalertcontroller-  (ID) initwithtitle: (Nsstring *) Title message: (nsstring *) Message viewcontroller: (uiviewcontroller *) inviewcontroller{    if  (Self = [super init])  {         self.title = title;         self.message = message;        self.inviewcontroller =  inviewcontroller;    }    return self;} -  (nsmutabledictionary *) buttoninfodict{    if  (_buttonInfoDict ==  nil)  {        _buttoninfodict = [ Nsmutabledictionary dictionary];    }    return _ Buttoninfodict;} -  (void) AddButton: (ributtonitem *) Button type: (Ributtonitemtype) itemtype{     if  (button == nil | |  ! [ButtoN iskindofclass:[ributtonitem class]])  {        return;     }    switch  (ItemType)  {         case RIButtonItemType_Cancel:        {             [self.buttoninfodict setobject:button  forkey:@ "Ributtonitemtype_cancel"];        }             break;         case ributtonitemtype_destructive:        {             [self.buttoninfodict setobject:button forkey:@ "Ributtonitemtype_destructive"];        }             break;        case ributtonitemtype_other:         {             nsmutablearray *otherarray = self.buttoninfodict[@ "RIButtonItemType_Other"];             if  (Otherarray == nil)  {                 otherarray  = [NSMutableArray array];             }            [otherarray addobject: button];            [self.buttoninfodict  setobject:otherarray forkey:@ "Ributtonitemtype_other"];&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;}             break;                     default:             break;    }}-  (void) Showinview: (uiview *) view{     if  (isIOS8)  {        [self  showios8viewwithtype:boalertcontrollertype_actionsheet];             } else {        [self ios7showinview: view];    }}-  (void) show{    if  (isIOS8)  {         [self showIOS8ViewWithType:BOAlertControllerType_AlertView];             } else {         [self ios7Show];    }}-  (void) Showios8viewwithtype: (Boalertcontrollertype) viewtype{     if  (Self.inviewcontroller == nil)  {         return;    }         uialertcontroller *alertcontroller = nil;    switch  (ViewType)  {         case BOAlertControllerType_AlertView:         {             Alertcontroller = [uialertcontroller alertcontrollerwithtitle:self.title message: self.message preferredstyle:uialertcontrollerstylealert];         }            break;         case boalertcontrollertype_actionsheet:        {             alertcontroller = [uialertcontroller alertcontrollerwithtitle: self.title message:self.message preferredstyle:uialertcontrollerstyleactionsheet];         }            break;                      default:            break;     }        RIButtonItem *cancelItem =  self.buttoninfodict[@ "Ributtonitemtype_cancel"];    if  (CancelItem != nil)  {        uialertaction *cancelaction = [ Uialertaction actionwithtitle:cancelitem.label style:uialertactionstylecancel handler:^ (uialertaction *action)  {             cancelitem.action ();         }];        [alertcontroller addaction: cancelaction];    }        ributtonitem * destructiveitem = self.buttoninfodict[@ "Ributtonitemtype_destructive"];    if   (Destructiveitem != nil)  {        uialertaction  *destructiveaction = [uialertaction actionwithtitle:cancelitem.label style: uialertactionstyledestructive handler:^ (uialertaction *action)  {             destructiveitem.action ();         }];        [alertcontroller addaction:destructiveaction];    }         nsarray *otheritems = self.buttoninfodict[@ " Ributtonitemtype_other "];    if  (otheritems != nil &&  otheritems.count > 0)  {        for  ( Ributtonitem *buttonitem in otheritems)  {             uialertaction *otheraction = [uialertaction actionwithtitle: buttonitem.label style:uialertactionstyledefault handler:^ (uialertaction *action)  {                 buttonitem.action ();             }];             [alertcontroller addaction:otheraction];        }    }         [self.inviewcontroller presentviewcontroller: Alertcontroller animated:yes completion:nil];} -  (void) Ios7showinview: (uiview *) View{        uiactionsheet  *actionsheet = [[uiactionsheet alloc] initwithtitle:self.title cancelbuttonitem : self.buttoninfodict[@ "Ributtonitemtype_cancel"] destructivebuttonitem:self.buttoninfodict[@ " Ributtonitemtype_destructive "] otherbuttonitems: nil];         nsarray *otheritems = self.buttoninfodict[@ "Ributtonitemtype_other"];     if  (otheritems != nil && otheritems.count > 0)  {         for  (ributtonitem *buttonitem in Otheritems)  {            [actionSheet  addbuttonitem:buttonitem];        }    }     [actionsheet showinview:view];} -  (void) Ios7show{    uialertview *alertview = [[uialertview alloc ] initwithtitle:self.title message:self.message cancelbuttonitem:self.buttoninfodict[@ " Ributtonitemtype_cancel "] otherbuttonitems:nil];         ributtonitem *destructiveitem = self.buttoninfodict[@ "Ributtonitemtype_destructive"];     if  (Destructiveitem != nil)  {         [alertView addButtonItem:destructiveItem];    }         nsarray *otheritems = self.buttoninfodict[@ "RIButtonItemType_OtHer "];    if  (otheritems != nil && otheritems.count  > 0)  {        for  (ributtonitem *buttonitem  in otheritems)  {            [ alertview addbuttonitem:buttonitem];        }     }        [alertview show];} @end


IOS8 Alertview and Actionsheet incompatibility problem solutions

Related Article

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.