Introduction
TBAlertController is a flexible and customizable UIAlertController replacement that allows you to present alert and action sheet dialogs in your iOS apps.
Installation
To install TBAlertController, you can use CocoaPods or manually add the source files to your project.
Using CocoaPods
- Add the following line to your Podfile:
- Run
pod install
to install the library. - Import TBAlertController in your project:
pod 'TBAlertController'
#import <TBAlertController/TBAlertController.h>
Manually Adding Source Files
- Download the latest version of TBAlertController.
- Drag and drop the
TBAlertController
folder into your Xcode project. - Make sure the
"Copy items if needed"
checkbox is selected. - Import TBAlertController in your project:
#import "TBAlertController.h"
Usage
TBAlertController provides a simple and intuitive API to display alerts and action sheets.
Creating an Alert
To create an alert, use the initWithTitle:message:preferredStyle:
method. You can set the title, message, and style of the alert.
TBAlertController *alert = [[TBAlertController alloc] initWithTitle:@"Title"
message:@"Message"
preferredStyle:TBAlertControllerStyleAlert];
Adding Actions
You can add actions to the alert using the addActionWithTitle:style:handler:
method. Each action consists of a title, style, and a handler block that gets executed when the action is selected.
[alert addActionWithTitle:@"OK"
style:TBAlertActionStyleDefault
handler:^(TBAlertAction * _Nonnull action) {
// Action handler code here
}];
Presenting the Alert
To present the alert, use the presentFromViewController:animated:completion:
method.
[alert presentFromViewController:self
animated:YES
completion:nil];
Sample Code
Here’s a sample code snippet showing how to create and present a basic alert:
// Create the alert
TBAlertController *alert = [[TBAlertController alloc] initWithTitle:@"Title"
message:@"Message"
preferredStyle:TBAlertControllerStyleAlert];
// Add an action
[alert addActionWithTitle:@"OK"
style:TBAlertActionStyleDefault
handler:^(TBAlertAction * _Nonnull action) {
// Action handler code here
}];
// Present the alert
[alert presentFromViewController:self
animated:YES
completion:nil];
Additional Information
For more details on how to customize and use TBAlertController, refer to the official GitHub repository.