滑动UIWebView以前进到下一篇博客文章

时间:2012-09-06 17:29:02

标签: iphone uitableview nsmutablearray uigesturerecognizer

我有几个博客应用程序在其中解析RSS,并在TableView中显示文章标题和日期。我有一个RSSEntry类,我使用Ray Wenderlich的教程。它解析所有不同的元素,然后我创建了一个新的字符串,其中包含HTML代码并插入到文章标题中,并将内容添加到这个新的HTML字符串中。我在TableView中的selectedAtRowIndex的当前代码是:

RSSEntry *entry = [_allEntries objectAtIndex:indexPath.row];

         _webViewControllerplay.entry = entry;

其中_allEntries是NSMutableArray,_webViewControllerplay是带webview的视图控制器。

我想要做的是在webview的视图控制器中添加一个手势识别器,这样当我向左或向右滑动时,它将转到上一篇文章或下一篇文章,而不必返回到表格视图。有什么方法可以实现这个目标?我理解我认为我需要传递整个数组,然后告诉它当前在哪一行,并在使用手势识别器时简单地添加或减去它?

1 个答案:

答案 0 :(得分:1)

我不太确定我是否理解你的意思。我最初的理解与@sergio类似 - 如何实现手势识别器。但似乎你正在寻找"如何设计"你的MVC(基于你对@sergio的评论:" ......我的问题实际上是获得数组,一切都已经过去了...... "。

如果是这样,以下建议可能会有所帮助:

如果你将你的模型,所有条目重构到它自己的类(NSObject的子类)并声明一些方便的属性,那么当我们说EntriesViewControllerWebViewController推动{{ 1}}进入导航堆栈,而不是传递条目本身,您可以传递指向模型的指针。 WebViewController可以自行查询模型。

像这个图:

enter image description here

希望这有帮助!


编辑:

这是您的模型界面

#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