0

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

CreateViewController: cancel当我点击我的取消按钮时会到达日志,但DefaultViewController: createViewControllerDidFinish不是。

这是相关代码:

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

创建视图控制器.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

创建视图控制器.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
4

1 回答 1

1

使用导航控制器,当您推送视图控制器时,您必须弹出关闭

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

不需要控制器实例 Nav Controller 基本上是一个堆栈,当您弹出它时,它会从堆栈顶部移除并移动到前一个视图控制器从它被推送的位置。

于 2013-02-24T20:41:11.013 回答