我不太确定我是否理解你的意思。我最初的理解类似于@sergio - 如何实现手势识别器。但似乎您正在寻找“如何设计”您的 MVC(基于您对@sergio 的评论:“ ......我的问题实际上是让数组和所有内容都通过...... ”。
如果是这样,这里有一个可能有用的建议:
如果你重构你的模型,所有条目,到它自己的类(子类NSObject
)并声明一些方便的属性,这样当,比如说,EntriesViewController
推WebViewController
入导航堆栈时,而不是传递条目本身,你可以传递一个指向模型。WebViewController
可以自己查询模型。
像这样的图表:
希望这可以帮助!
编辑:
这是您的模型界面
#import <Foundation/Foundation.h>
@interface SARSSEntryModel : NSObject
@property (nonatomic, strong) NSMutableArray *allEntries;
@property (nonatomic) NSUInteger currentEntryIndex;
@end
这是模型实现
#import "SARSSEntryModel.h"
@implementation SARSSEntryModel
@synthesize allEntries = _allEntries;
@synthesize currentEntryIndex = _currentEntryIndex;
// instantiate on-demand
- (NSMutableArray *)allEntries {
if (!_allEntries) {
_allEntries = [[NSMutableArray alloc] initWithObjects:@"http://www.google.com", @"http://www.yahoo.com", @"http://www.bing.com", nil];
}
return _allEntries;
}
这是EntriesViewController接口
#import <UIKit/UIKit.h>
#import "SARSSEntryModel.h"
#import "SAWebViewController.h"
@interface SAEntriesViewController : UITableViewController
@property (nonatomic, strong) SARSSEntryModel *model; // this is your model
@end
这是 EntriesViewController 实现
#import "SAEntriesViewController.h"
@implementation SAEntriesViewController
@synthesize model = _model;
- (void)viewDidLoad {
self.model = [[SARSSEntryModel alloc] init];
[super viewDidLoad];
}
- (void)viewDidUnload {
[self setModel:nil];
[super viewDidUnload];
}
#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.model.allEntries.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"cell identifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
cell.textLabel.text = [self.model.allEntries objectAtIndex:indexPath.row];
return cell;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
self.model.currentEntryIndex = indexPath.row;
SAWebViewController *wvc = [self.storyboard instantiateViewControllerWithIdentifier:@"WebViewControllerIdentifier"];
wvc.model = self.model;
[self.navigationController pushViewController:wvc animated:YES];
}
这是WebViewController接口
#import <UIKit/UIKit.h>
#import "SARSSEntryModel.h"
@interface SAWebViewController : UIViewController
@property (nonatomic, strong) SARSSEntryModel *model;
@property (weak, nonatomic) IBOutlet UIWebView *webView;
@end
这是 WebViewController 的实现
#import "SAWebViewController.h"
@implementation SAWebViewController
@synthesize webView = _webView;
@synthesize model = _model;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
// for testing. to prevent conflict with one touch swipe
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(navigate:)];
[self.webView addGestureRecognizer:swipe];
swipe = nil;
}
- (void)viewDidUnload {
[self setWebView:nil];
[self setModel:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (void)viewDidAppear:(BOOL)animated {
[self loadURL];
[super viewDidAppear:animated];
}
- (void)loadURL {
NSString *urlString = [self.model.allEntries objectAtIndex:self.model.currentEntryIndex];
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
[self.webView loadRequest:request];
}
- (void)navigate:(id)sender {
UISwipeGestureRecognizer *swipe = (UISwipeGestureRecognizer *)sender;
if (swipe.direction == UISwipeGestureRecognizerDirectionLeft) {
if (self.model.currentEntryIndex < self.model.allEntries.count) {
self.model.currentEntryIndex++;
}
else {
self.model.currentEntryIndex = 0;
}
}
else if (swipe.direction == UISwipeGestureRecognizerDirectionRight) {
if (self.model.currentEntryIndex > 0) {
self.model.currentEntryIndex--;
}
else {
self.model.currentEntryIndex = self.model.allEntries.count - 1;
}
}
[self loadURL];
}
@end