天天看點

[翻譯] STAlertView

STAlertView

[翻譯] STAlertView

The idea of this component is to improve the readability while using the native UIAlertView. The UIKit alert view works with delegates. When you have two or more alerts at the same controller, it becomes a problem because you have to use the TAG to identify from which UIAlertView comes the user.

設計這個控件的目的是為了增強系統原生UIAlertView的可用性,UIKit的alert view是用代理的方式工作的。當你在一個控制器中有着兩個或更多個alert時,問題來了(學挖掘機哪家強-_-|||),你需要用TAG值來标示不同的UIAlertView。

With STAlertView you will be able to define the behavior of the 'Ok' and 'Cancel' button, at the same place where you declare the alert view. So, let's see some code:

使用STAlertView的話,在block中标示就行了。

[[STAlertView alloc] initWithTitle:@"Title" 
        message:@"Message"
        cancelButtonTitle:@"Cancel"
        otherButtonTitles:@"Ok"
        cancelButtonBlock:^{
            NSLog(@"do something at cancel");

        } otherButtonBlock:^{
            NSLog(@"do something at ok");

        }];
      

And that's it, no pieces of code everywhere, just a few lines and all the related code together. As it is a native UIAlertView, the result of using STAlertView is like the native one:

就這些,你不用在其他地方寫代碼了,僅僅幾行代碼就行了。

[翻譯] STAlertView

This component has been made thanks to the answer of Ricky Helgesson at StackOverflow.

設計這個元件歸功于StackOverflow上的Ricky Helgesson所提供的答案。

Demo usage

To run the example project, clone the repo, and run 

pod install

 from the Example directory first.

為了運作demo,複制repo,運作pod,安裝即可

Requirements

Is compatible with ARC and non-ARC.

相容ARC和non-ARC

Installation

STAlertView is available through CocoaPods. To install it, simply add the following line to your Podfile:

STAlertView支援CocoaPods,使用下面的Podfile即可:

pod "STAlertView"
           

Then at the view controller that you want to show the alert view add at the .h:

#import <STAlertView/STAlertView.h>
...
@property (nonatomic, strong) STAlertView *alertView;
...
@end
      
...
self.alertView = [[STAlertView alloc] initWithTitle:@"Title of the alert" 
        message:@"Message you want to show"
        cancelButtonTitle:@"No" otherButtonTitles:@"Yes"
        cancelButtonBlock:^{
            // Code todo when the user cancel
            ...
        } otherButtonBlock:^{
            // Code todo when the user accept
            ...
        }];
...