1. Introduction
SHNavigationControllerBlocks is a framework that adds block-based completion handlers to UINavigationController methods. This library enhances the functionality of UINavigationController by allowing developers to easily define completion blocks that get called upon completion of specific navigation actions.
1.1 What is SHNavigationControllerBlocks?
SHNavigationControllerBlocks is a lightweight library that extends the capabilities of UINavigationController. It adds support for completion blocks, making it easier to perform actions after navigation operations have completed.
1.2 Key Features
- Block-based completion handlers for navigation methods.
- Easy integration with existing UINavigationController code.
- Support for both push and pop navigation operations.
- Handles both animated and non-animated transitions.
2. Installation
2.1 Cocoapods
To install SHNavigationControllerBlocks using Cocoapods, simply add the following line to your Podfile and run pod install
:
// Podfile
pod 'SHNavigationControllerBlocks'
2.2 Manual Installation
To install SHNavigationControllerBlocks manually, follow these steps:
- Download the SHNavigationControllerBlocks framework.
- Drag and drop the framework into your Xcode project.
- Ensure that the framework is added to your project’s Build Phases under “Link Binary with Libraries”.
- Import SHNavigationControllerBlocks in the relevant source files.
3. Getting Started
3.1 Importing the Framework
Once you have installed SHNavigationControllerBlocks, import it into your source file:
// Swift
import SHNavigationControllerBlocks
// Objective-C
#import <SHNavigationControllerBlocks/SHNavigationControllerBlocks-Swift.h>
3.2 Adding Completion Handlers to UINavigationController methods
To add completion handlers to UINavigationController methods, simply call the respective method and provide a completion block.
4. Usage Examples
4.1 Pushing View Controllers
To push a view controller onto the navigation stack and execute a completion block:
// Swift
navigationController?.pushViewController(viewController, animated: true, completion: {
// Completion block code here
})
// Objective-C
[navigationController pushViewController:viewController
animated:YES
completion:^{
// Completion block code here
}];
4.2 Popping View Controllers
To pop a view controller from the navigation stack and execute a completion block:
// Swift
navigationController?.popViewController(animated: true, completion: {
// Completion block code here
})
// Objective-C
[navigationController popViewControllerAnimated:YES
completion:^{
// Completion block code here
}];
4.3 Presenting View Controllers
To present a view controller and execute a completion block when the presentation finishes:
// Swift
present(viewController, animated: true, completion: {
// Completion block code here
})
// Objective-C
[self presentViewController:viewController
animated:YES
completion:^{
// Completion block code here
}];
4.4 Dismissing View Controllers
To dismiss a presented view controller and execute a completion block upon dismissal:
// Swift
dismiss(animated: true, completion: {
// Completion block code here
})
// Objective-C
[self dismissViewControllerAnimated:YES
completion:^{
// Completion block code here
}];
5. Additional Functionality
5.1 Getting the Parent Navigation Controller
You can easily obtain the parent navigation controller of a view controller by using the sh_navigationController
property defined in the SHNavigationControllerBlocks category.
// Swift
let parentNavigationController = viewController.sh_navigationController
// Objective-C
UINavigationController *parentNavigationController = viewController.sh_navigationController;
5.2 Adding Completion Handlers to UINavigationControllerDelegate methods
SHNavigationControllerBlocks also provides completion block support for UINavigationControllerDelegate methods. Implement the corresponding delegate method and provide a completion block parameter to execute code upon method completion.
6. Conclusion
SHNavigationControllerBlocks simplifies handling of completion blocks for UINavigationController methods, making it easier than ever to perform actions after navigation operations have finished. With its straightforward integration and extensive support for various navigation operations, it is a valuable addition to any UINavigationController-based project.