casparjespersen
casparjespersen

Reputation: 3840

UINavigationController not returning

I am trying to set up a system of ViewControllers and navigate between them. I am trying to navigate between DefaultViewController and CreateViewController. I am new to iOS programming, so I am having a hard time figuring this out. I have mainly been trying to use code from the Utility App template. It would be great if someone could spot my mistake!

The log CreateViewController: cancel is reached when I click on my Cancel-button, but DefaultViewController: createViewControllerDidFinish is not.

This is the relevant code:

DefaultViewController.h

#import <UIKit/UIKit.h>
#import <Social/Social.h>
#import "CreateViewController.h"

@interface DefaultViewController : UIViewController <CreateViewControllerDelegate, UIWebViewDelegate> {
    ...
}

@property (strong, nonatomic) UIWebView *webView;

- (IBAction)create:(id)sender;

@end

CreateViewController.h

#import <UIKit/UIKit.h>
#import <Social/Social.h>

@class CreateViewController;

@protocol CreateViewControllerDelegate
    - (void)createViewControllerDidFinish:(CreateViewController *)controller;
@end

@interface CreateViewController : UIViewController <UIWebViewDelegate> {
    ...
}

@property (weak, nonatomic) id <CreateViewControllerDelegate> delegate;

- (IBAction)cancel:(id)sender;
- (IBAction)submit:(id)sender;

@end

DefaultViewController.m

#import "AppDelegate.h"
#import "DefaultViewController.h"

...

@implementation DefaultViewController

...

#pragma mark - CreateViewController

- (void)createViewControllerDidFinish:(CreateViewController *)controller {
    NSLog(@"DefaultViewController: createViewControllerDidFinish");
    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
}

- (IBAction)create:(id)sender {
    CreateViewController *vc2 = [[CreateViewController alloc] init];
    [self.navigationController pushViewController:vc2 animated:YES];
}

@end

CreateViewController.m

#import "AppDelegate.h"
#import "CreateViewController.h"

...

@implementation CreateViewController

...

#pragma mark - Actions

- (IBAction)cancel:(id)sender {
    NSLog(@"CreateViewController: cancel");
    [self.delegate createViewControllerDidFinish:self];
}

- (IBAction)submit:(id)sender {
    [webView stringByEvaluatingJavaScriptFromString:@"EntryCreate.submit();"];
}

@end

Upvotes: 0

Views: 51

Answers (1)

Lithu T.V
Lithu T.V

Reputation: 20021

Using Navigation Controller,when you push a view controller you have to pop to dismiss

- (IBAction)cancel:(id)sender {
    NSLog(@"CreateViewController: cancel");
    [self.navigationController popViewControllerAnimated:YES];
}

No need for the controller instance Nav Controller is basically a stack and when you pop it removes from the top of the stack and move to the previous viewcontroller from where it is pushed.

Upvotes: 1

Related Questions