Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.2k views
in Technique[技术] by (71.8m points)

xcode - IOS: Move back two views

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

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..

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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....

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

in 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];
}

in view 2 . h

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

in view 2. m before @implementation after#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];
    }

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

in view 1. m before @implementation after#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
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...