0

RSS が解析され、記事のタイトルと日付が TableView に表示されるブログ アプリがいくつかあります。Ray Wenderlich のチュートリアルから使用した RSSEntry クラスがあります。さまざまな要素をすべて解析し、HTML コードを含む新しい文字列を作成して記事のタイトルに挿入し、コンテンツをこの新しい HTML 文字列に挿入します。TableView の selectedAtRowIndex の現在のコードは次のとおりです。

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

         _webViewControllerplay.entry = entry;

ここで、_allEntries は NSMutableArray であり、_webViewControllerplay は webview を持つビュー コントローラーです。

私がやりたいのは、Web ビューのビュー コントローラーにジェスチャ認識機能を追加して、左右にスワイプすると、テーブル ビューに戻ることなく、前の記事または次の記事に移動することです。これを達成するには、どのような方法がありますか? 配列全体を渡し、現在どの行にあるかを伝え、ジェスチャ認識エンジンを使用するときに単純にそれを加算または減算する必要があると思いますか?

4

1 に答える 1

1

あなたの言いたいことを理解できたかどうかはよくわかりません。私の最初の理解は@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
于 2012-09-06T19:02:42.683 に答える