简化UIView和UIViewController交互。
您不是要创建一个UIViewController子类和一个UIView子类,而是创建一个YOCView子类。
#import <YOCView/YOCView.h>
@interface MainView : YOCView
@end
要创建视图控制器,使用:[YOCViewController viewControllerForView:view];
。例如,在您的AppDelegate中,您可能会这样做:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
MainView *mainView = [[MainView alloc] init]; // Subclasses YOCView
UIViewController *viewController = [YOCView viewControllerForView:mainView];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
self.window.rootViewController = navigationController;
}
然后在您的YOCView中,通过访问self.navigation
可以执行push、pop、present、dismiss操作。
@implementation MyView
- (void)viewDidLoadWithLayout {
[super viewDidLoadWithLayout];
self.navigationTitle = @"Title";
}
- (void)pushAView {
AnotherView *view = [[AnotherView alloc] init];
[self.navigation pushView:view animated:YES];
}
- (void)presentAView {
AnotherView *view = [[AnotherView alloc] init];
[self.navigation presentView:view animated:YES];
}
- (void)popMyself {
[self.navigation popViewAnimated:YES];
}
- (void)popToRoot {
[self.navigation popToRootViewAnimated:YES];
}
- (void)dismissMyself {
[self.navigation dismissAnimated:YES];
}
- (void)pushAViewHideNav {
AnotherView *view = [[AnotherView alloc] init];
view.viewOptions = YOCViewOptionsHideNavigation;
[self.navigation pushView:view animated:YES];
}
@end
YOCView
还会获得以下通知
// Same as UIViewController notifications
- (void)viewWillAppear:(BOOL)animated;
- (void)viewDidAppear:(BOOL)animated;
- (void)viewWillDisappear:(BOOL)animated;
- (void)viewDidDisappear:(BOOL)animated;
- (void)viewDidLayoutSubviews;
- (void)viewDidLoad;
// When your app becomes active and this view is shown (after resigning the app)
- (void)viewDidBecomeActive;
- (void)viewWillResign;
// Same as viewDidLoad but after its been layout'ed.
- (void)viewDidLoadWithLayout;
// View will appear but only ever called once (after load)
- (void)viewWillAppearAfterLoad:(BOOL)animated;
// View did appear but only ever called once (after load)
- (void)viewDidAppearAfterLoad:(BOOL)animated;
调用self.navigation.viewController
将为您提供对您的UIViewController的访问权限。例如,要显示一个警告:
@implementation MyView
- (void)showAlert {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Oops" message:@"An alert view" preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
[self.navigation.viewController dismissViewControllerAnimated:YES completion:nil];
}]];
[self.navigation.viewController presentViewController:alert animated:YES completion:nil];
}
@end
pod "YOCView"
了解和使用YOCView的最好方式是通过看到它是如何工作的。打开示例项目:示例。