【问题标题】:Swipe UIWebView to Advance to Next Blog Article滑动 UIWebView 以前进到下一篇博客文章
【发布时间】:2012-09-06 17:29:02
【问题描述】:

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

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

         _webViewControllerplay.entry = entry;

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

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

【问题讨论】:

    标签: iphone uitableview nsmutablearray uigesturerecognizer


    【解决方案1】:

    我不太确定我是否理解您的意思。我最初的理解类似于@sergio - 如何实现手势识别器。但似乎您正在寻找“如何设计”您的 MVC(基于您对 @sergio 的评论:“...我的问题实际上是获取数组和所有内容...”。

    如果是这样,这里有一个可能有用的建议:

    如果你重构你的模型,所有条目,到它自己的类(NSObject 的子类)并声明一些方便的属性,这样当EntriesViewControllerWebViewController 推入导航堆栈时,而不是传递条目本身,您可以传递一个指向模型的指针。 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
    

    【讨论】:

    • 帮助弄清楚我的问题是什么,只是不知道如何实施。 Basically thought I could have an NSInteger, and when row is selected, first get the number of what indexPath.row was selected.然后,当它推送包含 webview 的新视图时,我可以告诉它加载数组并显示 objectAtIndex:无论所选行的数量是多少,然后滑动方法都可以工作。但是,它一直告诉我尝试将类型 NSInteger 设置为类型 NSInteger 并为 indexPath.row 返回 null 不兼容
    • 这里是在论坛中提出的获取所需的 webview 控制器的建议:您必须做出的另一个更改是将整个提要数组 + 当前索引传递给 WebViewController,以便它具有循环浏览所需的信息。我可以轻松地将提要数组传递给它,但不确定如何获取它的当前索引。这是目前的主要问题。
    • @user717452 我编辑了我的答案以包含一些代码 sn-ps。检查一下。
    猜你喜欢
    • 1970-01-01
    • 2018-07-22
    • 2021-10-17
    • 2015-12-01
    • 2015-06-26
    • 1970-01-01
    • 1970-01-01
    • 2019-02-27
    • 1970-01-01
    相关资源
    最近更新 更多