IOS:移回两个视图

时间:2013-01-09 20:11:23

标签: ios xcode uiviewcontroller

我一直坚持这个问题一段时间,找不到有关如何做到这一点的任何有用信息..

我有一个基本视图(视图1),我可以在其中选择tableview中的项目。在项目“页面”(视图2)上,我可以选择编辑该项目,触发模态视图(视图3)。 在此模态视图中,我可以选择删除此项目。如果用户按下该按钮并确认他们要删除该项目,我想将该应用程序发送回视图1 ..

我尝试过很多不同的事情(popToViewControllerpushViewControllerdismissViewController等等),但我无法解决任何问题。如果我关闭模态,则视图2不会关闭。有时甚至模态也不会消失。基本视图是UITableViewController,其他两个是UIViewControllers,我正在使用storyboard

1 个答案:

答案 0 :(得分:1)

您有几个选项可以使用NSNotificationCenter或使用delegate模式。 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中。 ħ

// For name of notification
extern NSString * const NOTIF_LoggingOut_Settings;
@implementation

之后的#imports之前的视图中的

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;

在视图中1.在@implementation

之后的#imports之前

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
}