有几种方法,所以大多数情况下您可以自己使用各种模式来完成此操作。您可以在应用程序委托中设置导航控制器,如下所示:
self.viewController = [[RootViewController alloc] initWithNibName:@"RootViewController" bundle:nil];
self.navigationController = [[ UINavigationController alloc ] initWithRootViewController:self.viewController ];
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
然后当你想展示一个新的 vc 时,你可以这样做:
OtherViewController *ovc = [[ OtherViewController alloc ] initWithNibName:@"OtherViewController" bundle:nil ];
[ self.navigationController pushViewController:ovc animated:YES ];
要返回,请执行以下操作:
[ self.navigationController popViewControllerAnimated:YES ];
就回调而言,实现此目的的一种方法是在项目中的某处制定这样的协议:
@protocol AbstractViewControllerDelegate <NSObject>
@required
- (void)abstractViewControllerDone;
@end
然后让每个视图控制器在委托中触发回调:
@interface OtherViewController : UIViewController <AbstractViewControllerDelegate>
@property (nonatomic, assign) id<AbstractViewControllerDelegate> delegate;
@end
最后,当您呈现一个新的 vc 时,将其指定为委托:
OtherViewController *ovc = [[ OtherViewController alloc ] initWithNibName:@"OtherViewController" bundle:nil ];
ovc.delegate = self;
[ self.navigationController pushViewController:ovc animated:YES ];
然后当你解雇 ovc 时,拨打这个电话
[self.delegate abstractViewControllerDone];
[ self.navigationController popViewControllerAnimated:YES ];
而在符合你制定的协议的rootVC中,你只需填写这个方法:
-(void) abstractViewControllerDone {
}
你刚刚打过电话的。这需要大量的设置,但其他选项包括查看 NSNotifications 和块,这可能会更简单,具体取决于您在做什么。