我一直坚持这个问题一段时间,找不到有关如何做到这一点的任何有用信息..
我有一个基本视图(视图1),我可以在其中选择tableview中的项目.在项目“页面”(视图2)上,我可以选择编辑该项目,触发模态视图(视图3).
在此模态视图中,我可以选择删除此项目.如果用户按下该按钮并确认他们要删除该项目,我想将该应用程序发送回视图1 ..
我已经尝试了很多不同的东西(popToViewController,pushViewController,dismissViewController等等),但我无法得到任何工作.如果我关闭模态,则视图2不会关闭.有时甚至模态也不会消失.基本视图是一个UITableViewController,另外两个是UIViewControllers,我正在使用storyboard.
解决方法
您有几个选项可以使用NSNotificationCenter或使用
delegate模式.
NSNotificationCenter易于使用,但也很棘手.
NSNotificationCenter易于使用,但也很棘手.
要使用通知中心,您需要将观察者添加到视图控制器类.当您关闭模态视图控制器时,您通知视图2视图3现在被解除,view2可以解雇自己…..
让我们在第3点说你要解雇你的观点.
在view3 .m
-(IBAction)yourMethodHere { //dissmiss view [self.navigationController dismissModalViewControllerAnimated:YES]; // or [self dismissModalViewControllerAnimated:YES]; whateever works for you //send notification to parent controller and start chain reaction of poping views [[NSNotificationCenter defaultCenter] postNotificationName:@"goToView2" object:nil]; }
在视图2中. H
// For name of notification extern NSString * const NOTIF_LoggingOut_Settings;
在视图2.在#i进入之后的@implementation之前
NSString * const NOTIF_LoggingOut_Settings = @"goToView2"; @implementation -(void)viewDidAppear:(BOOL)animated{ // Register observer to be called when logging out [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loggingOutSettings:) name:NOTIF_LoggingOut_Settings object:nil]; } /*--------------------------------------------------------------------------- * Notifications of 2 view *--------------------------------------------------------------------------*/ - (void)loggingOutSettings:(NSNotification *)notif { NSLog(@"Received Notification - Settings Pop Over popped"); [self.navigationController popViewControllerAnimated:NO];// change this if you do not have navigation controller //call another notification to go to view 1 [[NSNotificationCenter defaultCenter] postNotificationName:@"goToFirstView" object:nil]; }
在第一个视图中添加另一个观察者
在你的view1.h
extern NSString * const NOTIF_FirstView;
在#impount之后的@implementation之前的m
NSString * const NOTIF_FirstView = @“goToFirstView”;
@implementation -(void)viewDidAppear:(BOOL)animated{ // Register observer to be called when logging out [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(doYourThing:) name:NOTIF_FirstView object:nil]; } /*--------------------------------------------------------------------------- * Notifications of 1 view *--------------------------------------------------------------------------*/ - (void)ldoYourThing:(NSNotification *)notif { // do your thing }