第一個(gè)類
UIAlertView:繼承于UIView,類的對(duì)象為彈框雕沉。彈框在屏幕中央
彈提示控件的方法(彈框):提示控件為UIAlertView類型,創(chuàng)建一個(gè)UIAlertView類型的的對(duì)象后美澳,使用VIAlertView對(duì)象的show方法僚害,即可彈出創(chuàng)建的控件∈钊埃控件控件可以使用該類的alloc骆莹、init方法。
第一個(gè)參數(shù)為標(biāo)題担猛,第二個(gè)參數(shù)為內(nèi)容幕垦,第三個(gè)為代理,第四個(gè)參數(shù)為取消按鈕的內(nèi)容(文字會(huì)加粗傅联,此按鈕的tag == 0)先改,第五個(gè)參數(shù)為其他按鈕(按鈕字體不會(huì)加粗,tag依次增加)蒸走。
- (instancetype)initWithTitle:(nullableNSString *)title message:(nullable NSString *)message delegate:(nullable id/**/)delegate cancelButtonTitle:(nullable NSString*)cancelButtonTitle otherButtonTitles:(nullable NSString *)otherButtonTitles,... NS_REQUIRES_NIL_TERMINATION NS_EXTENSION_UNAVAILABLE_IOS("UseUIAlertController instead.");
UIAlertViewDelegate:UIAlertView的代理需要遵守的協(xié)議仇奶,遵守代理協(xié)議,可實(shí)現(xiàn)以下方法比驻,當(dāng)點(diǎn)擊相應(yīng)的按鈕時(shí)该溯,執(zhí)行的方法,其中的buttonIndex為被點(diǎn)擊按鈕的tag
-(void)alertView:(UIAlertView*) alertViewclickedButtonAtIndex:(NSInteger) buttonIndex NS_DEPRECATED_IOS(2_0, 9_0);
保存UIAlertView對(duì)象類型的屬性:
@property(nonatomic,assign) UIAlertViewStylealertViewStyle NS_AVAILABLE_IOS(5_0); // Alert view style - defaults toUIAlertViewStyleDefault
枚舉變量(
UIAlertViewStyleDefault默認(rèn)值别惦,不帶文本框的樣式
UIAlertViewStyleSecureTextInput帶一個(gè)密碼文本框狈茉,勾選了secure屬性的文本框
UIAlertViewStylePlainTextInput一個(gè)普通的文本框
UIAlertViewStyleLoginAndPasswordInput帶兩個(gè)文本框,一個(gè)是普通的文本框论皆,一個(gè)是密碼文本框)
當(dāng)有文本輸入框時(shí),根據(jù)按鈕的tag返回對(duì)應(yīng)的UITextField對(duì)象(前提是存在UITextField對(duì)象)
- (nullable UITextField*)textFieldAtIndex:(NSInteger)textFieldIndex NS_AVAILABLE_IOS(5_0);
// Retrieve a text field at an index
// The field at index 0 will be the firsttext field (the single field or the login field), the field at index 1 will bethe password field. */
第二個(gè)類
UIActionSheet :繼承于UIView猾漫,類的對(duì)象為彈框,彈框在屏幕底部悯周,顯示時(shí)需要使用對(duì)象的show方法。
創(chuàng)建對(duì)象的方法:第一個(gè)參數(shù)為標(biāo)題陪竿,第二個(gè)參數(shù)為代理禽翼,第三個(gè)參數(shù)為取消按鈕內(nèi)容,第四個(gè)參數(shù)為次要按鈕內(nèi)容闰挡,第五個(gè)參數(shù)為其他按鈕锐墙。其按鈕tag為從第四個(gè)參數(shù)依次增加长酗,最后一個(gè)為tag為取消按鈕的tag
- (instancetype)initWithTitle:(nullableNSString *)title delegate:(nullable id)delegatecancelButtonTitle:(nullable NSString *)cancelButtonTitledestructiveButtonTitle:(nullable NSString *)destructiveButtonTitleotherButtonTitles:(nullable NSString *)otherButtonTitles, ...NS_REQUIRES_NIL_TERMINATION NS_EXTENSION_UNAVAILABLE_IOS("UseUIAlertController instead.");
UIAlertViewDelegate:UIActionSheet代理需要遵守的協(xié)議溪北,遵守代理協(xié)議夺脾,可實(shí)現(xiàn)以下方法,當(dāng)點(diǎn)擊相應(yīng)的按鈕時(shí)咧叭,執(zhí)行的方法蚀乔。其中的buttonIndex為被點(diǎn)擊按鈕的tag
- (void)actionSheet:(UIActionSheet*)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndexNS_DEPRECATED_IOS(2_0, 8_3) __TVOS_PROHIBITED;
UIAlertController :繼承于UIViewController類,類的對(duì)象為彈框吉挣,
創(chuàng)建對(duì)象的方法:第一個(gè)參數(shù)為標(biāo)題,第二個(gè)參數(shù)為內(nèi)容婉弹,第三個(gè)參數(shù)為為顯示的樣式睬魂。
顯示樣式可選為UIAlertControllerStyleActionSheet\
UIAlertControllerStyleAlert兩種中的一種
+(instancetype)alertControllerWithTitle:(nullable NSString *)titlemessage:(nullable NSString *)messagepreferredStyle:(UIAlertControllerStyle)preferredStyle;
為UIAlertController對(duì)象添加按鈕時(shí)要聲明UIAlertAction類的對(duì)象,并添加進(jìn)當(dāng)前的UIAlertController對(duì)象。方法為:參數(shù)為被添加的UIAlertAction對(duì)象马胧。
- (void)addAction:(UIAlertAction *)action;
最后把創(chuàng)建的對(duì)象和按鈕聯(lián)系起來(lái):調(diào)用方法的對(duì)象為當(dāng)前的View(self)汉买,第二個(gè)參數(shù)可以寫YES,第三個(gè)參數(shù)為block,當(dāng)點(diǎn)擊按鈕時(shí)執(zhí)行的代碼佩脊,如果沒(méi)有要執(zhí)行的代碼蛙粘,則可寫nil,在這個(gè)block中寫代碼時(shí)要注意內(nèi)存管理威彰,避免強(qiáng)指針的相互引用;
-(void)presentViewController:(UIViewController *)viewControllerToPresentanimated: (BOOL)flag completion:(void (^ __nullable)(void))completionNS_AVAILABLE_IOS(5_0);
UIAlertAction:繼承于NSObject類出牧。其創(chuàng)建的對(duì)象專門用于給UIAlertController對(duì)象添加按鈕的類。
創(chuàng)建對(duì)象的方法:第一個(gè)參數(shù)為按鈕標(biāo)題歇盼,第二個(gè)參數(shù)為按鈕類型舔痕,第三個(gè)參數(shù)為點(diǎn)擊按鈕后執(zhí)行的代碼。按鈕類型有三種:UIAlertActionStyleDefault= 0(為默認(rèn)類型黑色)豹缀,UIAlertActionStyleCancel(加粗),UIAlertActionStyleDestructive(紅色)
+ (instancetype)actionWithTitle:(nullableNSString *)title style:(UIAlertActionStyle)style handler:(void (^ __nullable)(UIAlertAction*action))handler;