0

我有一个 HTML 字符串数组,每次用户滑动时,我都试图让下一个字符串出现在 UIWebView 中。根据我的输出,问题似乎是当我在 viewWillAppear 中加载第一个 HTML 字符串时,数组就在那里并且第一个 HTML 字符串出现在 webview 中,但是一旦我在 swipedRight 或 swipedLeft 中注册滑动,当我检查数组突然空了。我将数组作为类中的属性,所以我无法弄清楚为什么会发生这种情况。感觉有点蠢。。

我是提问(和 Objective-C)的新手,所以如果你还需要什么,请告诉我。

在标题...

@property (nonatomic, weak)NSArray * slideDesctiptions;
@property (weak, nonatomic) IBOutlet UIWebView *webView;

这是我的实现代码..

@interface TestViewController () <ArticleTableViewControllerDelegate>
@property (nonatomic, strong) IBOutlet TestView* testView;
@property (nonatomic, strong) NSString* description;
@property int marker;
@end

@implementation TestViewController
@synthesize slideDescriptions = _slideDescriptions;
@synthesize description = _description;
@synthesize marker = _marker;

@synthesize testView = _testView;
@synthesize webView = _webView;

-(void)setTestView:(TestView *)testView {
    _testView = testView;
    UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipedRight:)];
    [swipeRight setDirection:UISwipeGestureRecognizerDirectionRight];
    [self.testView addGestureRecognizer:swipeRight];
    UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipedLeft:)];
    [swipeRight setDirection:UISwipeGestureRecognizerDirectionLeft];
    [self.testView addGestureRecognizer:swipeLeft];
   // [self.testView addSubview:self.webView];

}

-(void)viewWillAppear:(BOOL)animated{       
        self.marker =0;
    self.description = [self.slideDescriptions objectAtIndex:0];
    NSLog(@"description %@",[self.description description]);
            NSLog([[NSString stringWithFormat:@"count %d", [self.slideDescriptions count]]description]);


    self.webView.scalesPageToFit = YES;
    [self.testView addSubview:self.webView];
    [self.webView loadHTMLString:self.description baseURL:nil];
}


    -(void)swipedRight :(UISwipeGestureRecognizer *) gesture{
 NSLog([[NSString stringWithFormat:@"count %d", [self.slideDesctiptions count]]description]);
        if ((gesture.state == UIGestureRecognizerStateChanged)
            || (gesture.state == UIGestureRecognizerStateEnded)){
            NSLog(@"swiped right");
            self.marker ++;
            NSLog([[NSString stringWithFormat:@"count %d", [self.slideDescriptions count]]description]);
    NSLog(@"description to be shown %@",[self.description description]);
            self.description = [self.slideDesctiptions objectAtIndex:self.marker];
            [self.webView loadHTMLString:self.description baseURL:nil];
        }
    }
    -(void)swipedLeft :(UISwipeGestureRecognizer *) gesture{
        if ((gesture.state == UIGestureRecognizerStateChanged)
            || (gesture.state == UIGestureRecognizerStateEnded)){
            NSLog(@"swipted left");
            self.marker --;
            self.description = [self.slideDesctiptions objectAtIndex:self.marker];
            [self.webView loadHTMLString:self.description baseURL:nil];
        }
    }

@end

输出:

2013-06-24 10:59:44.300 PlairTest[1667:c07] description <p>We are now roughly one month from the 2013 MLB All-Star Game, with two-and-a-half months of the 2013 season now in the books.</p><p>As the weeks pass, there is more and more separation between the contenders and the non-contenders, but this past week showed that things are still far from decided.&nbsp;</p><p>The San Diego Padres, Kansas City Royals and Toronto Blue Jays checked in at 18-20 in last week's power rankings and went a combined 16-3 last week. Meanwhile, the Rangers and Yankees stood at No. 5 and No. 6, and they went a combined 2-11.&nbsp;</p><p>Those were not the only surprises this past week, and needless to say, there was plenty of shakeup in the rankings as a result.</p><p>So here is a look at this week's updated <a href="http://bleacherreport.com/articles/1630131-updated-mlb-power-rankings">MLB power rankings</a>, with the focus this week being on who each team's five most valuable players are right now, according to WAR. Be sure to check back here each Monday morning for an updated look at where your favorite team stands.</p><p>&nbsp;</p><p><em>*Note: All WAR statistics courtesy of <a href="http://www.fangraphs.com/">FanGraphs.com</a> and current through June 15. All other statistics current through June 16.</em></p>
2013-06-24 10:59:44.301 PlairTest[1667:c07] count 31
2013-06-24 10:52:38.563 PlairTest[1646:c07] count 0
2013-06-24 10:52:38.564 PlairTest[1646:c07] swiped right
2013-06-24 10:52:38.564 PlairTest[1646:c07] count 0
2013-06-24 10:52:38.564 PlairTest[1646:c07] slide to be shown (null)
2013-06-24 10:52:38.564 PlairTest[1646:c07] description <p>We are now roughly one month from the 2013 MLB All-Star Game, with two-and-a-half months of the 2013 season now in the books.</p><p>As the weeks pass, there is more and more separation between the contenders and the non-contenders, but this past week showed that things are still far from decided.&nbsp;</p><p>The San Diego Padres, Kansas City Royals and Toronto Blue Jays checked in at 18-20 in last week's power rankings and went a combined 16-3 last week. Meanwhile, the Rangers and Yankees stood at No. 5 and No. 6, and they went a combined 2-11.&nbsp;</p><p>Those were not the only surprises this past week, and needless to say, there was plenty of shakeup in the rankings as a result.</p><p>So here is a look at this week's updated <a href="http://bleacherreport.com/articles/1630131-updated-mlb-power-rankings">MLB power rankings</a>, with the focus this week being on who each team's five most valuable players are right now, according to WAR. Be sure to check back here each Monday morning for an updated look at where your favorite team stands.</p><p>&nbsp;</p><p><em>*Note: All WAR statistics courtesy of <a href="http://www.fangraphs.com/">FanGraphs.com</a> and current through June 15. All other statistics current through June 16.</em></p>
4

1 回答 1

1

您的数组属性很弱:

@property (nonatomic, weak)NSArray * slideDesctiptions;

基本上,这意味着 ARC 将在您存储后立即释放(销毁)您的数组。您应该将其更改为strong

@property (nonatomic, strong) NSArray *slideDesctiptions;

快速注意,您的网络视图属性是正确的弱:

@property (weak, nonatomic) IBOutlet UIWebView *webView;

那是因为它是一个IBOutlet并且它被它的超级视图保留。你不想让它变强。

(所以很有可能testView应该更改weak为您目前拥有的strong

于 2013-06-24T18:10:18.790 回答