我试图建立一个ViewControllers系统并在它们之间导航。我正在尝试在DefaultViewControllerCreateViewController之间导航。我是iOS编程的新手,因此很难弄清楚这一点。我主要尝试使用Utility Utility模板中的代码。如果有人能发现我的错误,那就太好了!

当我单击“取消”按钮时,到达日志CreateViewController: cancel,但没有。

这是相关代码:

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

最佳答案

使用导航控制器时,当按下视图控制器时,必须弹出才能关闭

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


不需要控制器实例Nav Controller本质上是一个堆栈,当您弹出时,它会从堆栈顶部移除,并移至上一个从其推入的viewcontroller

快乐的编码:)

关于ios - UINavigationController不返回,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15056303/

10-10 03:16