我正在开发一个IOS项目,我需要能够进入一个包含MWPhotoBrowser的ViewController。
目前,我已经创建了一个PhotoGalleryController并将其用作MWPhotoBrowser的容器,因为MWPhotoBrowser是UIViewController的子类。
然而,当我在模拟器中启动应用程序时,我得到一个空白的屏幕,中间有一个圆圈,表示已加载PhotoBrowser,但没有返回照片。
我继续记录了我所有的委托方法,发现正在调用numberOfPhotos但是实际上应该返回MWPhoto对象的函数甚至都没有被调用过!
我不太确定为什么即使我在容器ViewController中实现了所有正确的委托方法,照片对象的实际加载也永远不会完成。
我检查了API调用给出的响应输出,并打印出self.photoList。这些似乎很好,所以我不确定为什么它不会返回任何照片。
PhotoGalleryController.m
#import "PhotoGalleryController.h"
#import "MZApi.h"
#import "MZUser.h"
@implementation PhotoGalleryController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
//Make the HTTP request and setup the photo array in MZUser. Change from "usertext" later.
[MZApi loadOwnPhotoWithUserID:@"usertext" andCompletionHandler:^(NSMutableArray *photos, NSError *error) {
if(photos!=nil){
self.userPhotoList = photos;
[[MZUser getCurrentUser]setPhotoList:self.userPhotoList];
//NSLog(@" %@", self.userPhotoList);
//Should currently be null because CurrentUser is not created yet.
}
else{
NSLog(@"Failed to allocate photos!");
}
}];
MWPhotoBrowser *browser = [[MWPhotoBrowser alloc]initWithDelegate:self];
browser.displayActionButton = YES; // Show action button to allow sharing, copying, etc (defaults to YES)
browser.displayNavArrows = NO; // Whether to display left and right nav arrows on toolbar (defaults to NO)
browser.displaySelectionButtons = NO; // Whether selection buttons are shown on each image (defaults to NO)
browser.enableGrid = YES; // Whether to allow the viewing of all the photo thumbnails on a grid (defaults to YES)
browser.startOnGrid = YES; // Whether to start on the grid of thumbnails instead of the first photo (defaults to NO)
[self.view addSubview:browser.view];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(NSUInteger) numberOfPhotosInPhotoBrowser:(MWPhotoBrowser *)photoBrowser{
return [self.userPhotoList count];
}
- (id <MWPhoto>)photoBrowser:(MWPhotoBrowser *)photoBrowser photoAtIndex:(NSUInteger)index{
if(index<self.userPhotoList.count){
NSString *temp = [[self.userPhotoList objectAtIndex:index]file_url];
MWPhoto *photo = [MWPhoto photoWithURL:[NSURL URLWithString:temp]];
return photo;
}
return nil;
}
-(id <MWPhoto>) photoBrowser:(MWPhotoBrowser *)photoBrowser thumbPhotoAtIndex:(NSUInteger)index{
NSLog(@"Thumb Delegate Called!");
if(index<self.userPhotoList.count){
NSString *temp = [[self.userPhotoList objectAtIndex:index]file_url];
MWPhoto *photo = [MWPhoto photoWithURL:[NSURL URLWithString:temp]];
return photo;
}
return nil;
}
@end
PhotoGalleryController.h
#import <UIKit/UIKit.h>
#import "MWPhotoBrowser.h"
@interface PhotoGalleryController : UIViewController <MWPhotoBrowserDelegate>
@property(strong, nonatomic) NSMutableArray * userPhotoList;
- (NSUInteger)numberOfPhotosInPhotoBrowser:(MWPhotoBrowser *)photoBrowser;
- (id <MWPhoto>)photoBrowser:(MWPhotoBrowser *)photoBrowser photoAtIndex:(NSUInteger)index;
- (id <MWPhoto>)photoBrowser:(MWPhotoBrowser *)photoBrowser thumbPhotoAtIndex:(NSUInteger)index;
@end
服务器响应示例:
(
{
dislikes = 0;
"file_url" = "http://localhost:3000/static/photos/2.jpg";
likes = 0;
"photo_id" = 2;
"user_id" = kai1234;
},
{
dislikes = 4;
"file_url" = "http://localhost:3000/static/photos/1.jpg";
likes = 5;
"photo_id" = 1;
"user_id" = kai1234;
}
)
答案 0 :(得分:0)
当您使用块加载照片时,您正在添加浏览器进行查看,通常在使用块加载照片时会有延迟,因此浏览器将获得一个空对象。
您的代码应该看起来像
#import "PhotoGalleryController.h"
#import "MZApi.h"
#import "MZUser.h"
@implementation PhotoGalleryController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
//Make the HTTP request and setup the photo array in MZUser. Change from "usertext" later.
self.userPhotoList = [NSMutableArray array];
[MZApi loadOwnPhotoWithUserID:@"usertext" andCompletionHandler:^(NSMutableArray *photos, NSError *error) {
if(photos!=nil){
self.userPhotoList = photos;
[[MZUser getCurrentUser] setPhotoList:self.userPhotoList];
//NSLog(@" %@", self.userPhotoList);
//Should currently be null because CurrentUser is not created yet.
for (int i = 0; i < photos.count; i++)
{
[self.userPhotoList addObject:[MWPhoto photoWithURL:[NSURL URLWithString:[[photos objectAtIndex:i] file_url]]];
}
MWPhotoBrowser *browser = [[MWPhotoBrowser alloc]initWithDelegate:self];
browser.displayActionButton = YES; // Show action button to allow sharing, copying, etc (defaults to YES)
browser.displayNavArrows = NO; // Whether to display left and right nav arrows on toolbar (defaults to NO)
browser.displaySelectionButtons = NO; // Whether selection buttons are shown on each image (defaults to NO)
browser.enableGrid = YES; // Whether to allow the viewing of all the photo thumbnails on a grid (defaults to YES)
browser.startOnGrid = YES; // Whether to start on the grid of thumbnails instead of the first photo (defaults to NO)
[self.view addSubview:browser.view];
}
else{
NSLog(@"Failed to allocate photos!");
}
}];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(NSUInteger) numberOfPhotosInPhotoBrowser:(MWPhotoBrowser *)photoBrowser{
return [self.userPhotoList count];
}
- (id <MWPhoto>)photoBrowser:(MWPhotoBrowser *)photoBrowser photoAtIndex:(NSUInteger)index{
if(index<self.userPhotoList.count){
return [self.userPhotoList objectAtIndex:index];
}
return nil;
}
-(id <MWPhoto>) photoBrowser:(MWPhotoBrowser *)photoBrowser thumbPhotoAtIndex:(NSUInteger)index{
NSLog(@"Thumb Delegate Called!");
if(index<self.userPhotoList.count){
return [self.userPhotoList objectAtIndex:index];
}
return nil;
}
@end