IOS:移回两个视图 [英] IOS: Move back two views

查看:103
本文介绍了IOS:移回两个视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've been stuck with this problem for a while now, can't find any useful information on how to do this..

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

I have a base view (view 1), where I can select items in a tableview. While on the items "page" (view 2) I can choose to edit that item, triggering a modalview (view 3). In this modalview, I have the option to delete this item. If the user pressed that button and confirms they want to delete the item, I want to send the app back to view 1..

我尝试了很多不同的东西( popToViewController pushViewController dismissViewController 等等)但是我无法得到任何工作。如果我关闭模态,则视图2不会关闭。有时甚至模态也不会消失。基本视图是 UITableViewController ,另外两个是 UIViewControllers ,我正在使用故事板

I've tried a number of different things (popToViewController, pushViewController, dismissViewController etc etc) but I can't get anything to work. If I get the modal to close, view 2 doesn't close. Sometimes even the modal doesn't disappear. The base view is a UITableViewController, the other two are UIViewControllers, and I'm using storyboard.

推荐答案

您有几个选项可以使用 NSNotificationCenter 或使用委托模式。
NSNotificationCenter 易于使用,但也很棘手。

You have several options you can either use NSNotificationCenter or use the delegate pattern. NSNotificationCenter is easy to use but also it is tricky.

要使用通知中心,您需要将观察者添加到视图控制器类。当您关闭模态视图时控制器你通知你的观点2视图3现在被解雇,view2可以解雇自己.....

To use notification center you need to add observers to your view controller classes.When you dismiss your modal view controller you notify your view 2 that view 3 is dismissed now, view2 can dismiss itself.....

所以基本上当你通知中心时,无论通知它运行一个方法等......

So basically when you notify the center, whatever in notified it runs a method etc....

让我们在第3点说你要解雇你的意见。

Lets say your at view 3, you want to dismiss your views.

在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. m之前 @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;

add another observer to your first view in your view1.h extern NSString * const NOTIF_FirstView;

视图1. m之前 @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
}

这篇关于IOS:移回两个视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆