我正在尝试建立一个 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