2013-06-24 33 views
0

我有一個HTML字符串數組,並且我試圖讓用戶每次滑動時在UIWebView中出現下一個字符串。根據我的輸出,問題似乎是,當我在viewWillAppear中加載第一個HTML字符串時,數組全部在那裏,第一個HTML字符串出現在webview中,但是一旦我在swipedRight或swipedLeft中註冊了swipe,當我檢查陣列突然空了。我將該數組作爲該類中的一個屬性,所以我無法弄清楚爲什麼會發生這種情況。感覺就像它愚蠢的東西..iPhone在滑動時丟失了數組信息,無法顯示任何東西

我是新的問問題(和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> 

回答

1

你的數組屬性較弱:

@property (nonatomic, weak)NSArray * slideDesctiptions; 

基本上,這意味着ARC會回收(銷燬)數組您存放後直它。你應該改變,要strong代替:

@property (nonatomic, strong) NSArray *slideDesctiptions; 

快速注意到,您的Web視圖屬性是正確的要弱:

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

這是因爲它是一個IBOutlet而且它是由它的父保留。你不想讓它變得強大。

(所以有一個很好的機會,testView應改爲weak,你目前有作爲strong

+0

沒關係啊是有道理的。它現在有效。超級有用的非常感謝。 – ARomano