2013-10-15 73 views
0

我想從Myfile推送數據到SDWebImageDataSource並推送SDWebImageRootViewController(視圖)。我有一個問題,我無法傳遞數據。我已經使用setImageLink方法,但我在NSInteger方法中獲得imageLink值(null),爲什麼會發生這種情況?我應該用不同的方式嗎?傳遞數據到另一個文件沒有pushViewController

Myfile.h

@property (nonatomic, retain) NSString *image; 

Myfile.m

- (void)showSDWebImageSample 
{ 
SDWebImageDataSource *imageController = [[SDWebImageDataSource alloc] init]; 
[imageController setImageLink:image]; 
[imageController release]; 

NSLog(@"imagelink are: %@", imageController.imageLink); 

SDWebImageRootViewController *newController = [[SDWebImageRootViewController alloc] init]; 
[[self navigationController] pushViewController:newController animated:YES]; 
[newController release]; 
} 

SDWebImageDataSource.m

-(void)setImageLink:(NSString *)imageLinkStr 
{ 
    imageLink = imageLinkStr; 

//// I can see the value here 
NSLog(@"111 %@ 111",imageLinkStr); 
NSLog(@"222 %@ 222",imageLink); 
} 

- (NSInteger)numberOfPhotos { 

////imageLink string is (null) Problem here 
NSLog(@"222 %@ 222",imageLink); 

images_ = [[NSArray alloc] initWithObjects: 
      [NSArray arrayWithObjects:@"http://farm3.static.flickr.com/2756/4464013736_524526b2b2_z.jpg", @"http://farm3.static.flickr.com/2756/4464013736_524526b2b2_s.jpg", nil], [NSArray arrayWithObjects:imageLink, imageLink, nil], nil]; 

NSInteger count = [images_ count]; 
return count; 
} 

- (void)imageAtIndex:(NSInteger)index photoView:(KTPhotoView *)photoView { 
NSArray *imageUrls = [images_ objectAtIndex:index]; 
NSString *url = [imageUrls objectAtIndex:FULL_SIZE_INDEX]; 
[photoView setImageWithURL:[NSURL URLWithString:url] placeholderImage:[UIImage imageNamed:@"photoDefault.png"]]; 
} 

回答

1

也許不是最好的解決辦法,但你可以試試這個方法:

-(void)setImageLink:(NSString *)imageLinkStr { 
     imageLink = imageLinkStr; 

     //// I can see the value here 
     NSLog(@"111 %@ 111",imageLinkStr); 
     NSLog(@"222 %@ 222",imageLink); 

    [[NSUserDefaults standardUserDefaults] setObject:imageLink forKey:@"image"]; 
    [[NSUserDefaults standardUserDefaults]synchronize]; 
    } 

    - (NSInteger)numberOfPhotos { 

     NSString *importImage = [[NSUserDefaults standardUserDefaults] objectForKey:@"image"]; 
    ////imageLink string is (null) Problem here 
    NSLog(@"222 %@ 222",importImage); 

    images_ = [[NSArray alloc] initWithObjects: 
     [NSArray  arrayWithObjects:@"http://farm3.static.flickr.com/2756/4464013736_524526b2b2_z.jpg", @"http://farm3.static.flickr.com/2756/4464013736_524526b2b2_s.jpg", nil], [NSArray arrayWithObjects:importImage, importImage, nil], nil]; 

     NSInteger count = [images_ count]; 
     return count; 
    } 
+0

您忘了[[NSUserDefaults standardUserDefaults] synchronize];在setImageLink – Yanchi

+0

@Yanchi你是對的,我使用弧,所以沒有必要同步,但在這種情況下,你必須添加 – Ilario

+0

@Ilario它的工作原理...接受和升級。 –

相關問題