提问人:Alex Leu 提问时间:5/24/2014 最后编辑:user1118321Alex Leu 更新时间:5/24/2014 访问量:81
具有不同名称的 NSMutableArray 和 SetImage 的相同算法的工作方式不同
Same algorithms with NSMutableArray and SetImage with different names work differently
问:
所以我有一个问题。
我有:
NSMutableArray *photos_imagesArray;
NSMutableArray *myPhotos_imagesArray;
在 的 .h 文件中声明。@interface EXViewController
我所做的是:
对于一个视图 - - 我从这个视图上获取图像数据并使用 setImage。一切正常。photos_viewController
photos_imagesArray
UIImageView
现在,当我做同样的事情时,但使用 和 - setImage 不会启动并被清空。myPhotos_viewController
myPhotos_imagesArray
myPhotos_imagesArray
为什么会这样工作?我有两个非常相同的算法在两个以及相同的视图中工作,但我得到的结果不同。唯一不同的是几个名字。XCode 甚至没有向我显示任何错误。
更新 1(共享代码):
在 EXViewController.h 中,我声明可变数组的位置。
@interface EXViewController : UIViewController <UITextFieldDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate> {
UIImagePickerController *upload_imagePickerCamera;
UIImagePickerController *upload_imagePickerLibrary;
UIImage *upload_imageUploaded;
IBOutlet UIImageView *upload_imageViewUploadedImage;
// Photos Currently viewed
NSInteger _photoIndex;
NSMutableArray *photos_imagesArray;
NSMutableArray *myPhotos_imagesArray;
}
其功能包括:
对于有效的部分 -
// PHOTOS
-(void)goToPhotos {
[photos_imagesArray removeAllObjects];
_photoIndex = 0;
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UIViewController *viewControllerPhotos = [mainStoryboard instantiateViewControllerWithIdentifier:@"viewControllerPhotos"];
[self presentViewController:viewControllerPhotos animated:true completion:nil];
PFQuery *query_photos = [PFQuery queryWithClassName:@"Photo"];
[query_photos orderByDescending:@"createdAt"];
[query_photos findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
// Copy objects array to photos array
photos_imagesArray = [objects mutableCopy];
// Set Current photo
[self photos_setCurrentPhoto];
} else {
NSString *errorString = [error userInfo][@"error"];
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Error" message:errorString delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert show];
}
}];
}
-(void)photos_setCurrentPhoto{
if ([photos_imagesArray count] != 0) {
PFFile *userImageFile = photos_imagesArray[_photoIndex][@"imageFile"];
[userImageFile getDataInBackgroundWithBlock:^(NSData *imageData, NSError *error) {
if (!error) {
UIImage *image = [UIImage imageWithData:imageData];
[self.photos_imageViewCurrent setImage:image];
}
}];
}
}
而对于没有的部分——
// MY PHOTOS
-(void)myPhotos_goToMyPhotos{
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UIViewController *viewControllerPhotos = [mainStoryboard instantiateViewControllerWithIdentifier:@"viewControllerMyPhotos"];
[self presentViewController:viewControllerPhotos animated:true completion:^{
PFQuery *query_photos = [PFQuery queryWithClassName:@"Photo"];
[query_photos whereKey:@"userId" equalTo:[PFUser currentUser].objectId];
[query_photos orderByDescending:@"createdAt"];
[query_photos findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
_photoIndex = 0;
// Copy objects array to photos array
myPhotos_imagesArray = [objects mutableCopy];
NSLog(@"%i",[myPhotos_imagesArray count]);
// Set Current photo
[self myPhotos_setCurrentPhoto];
} else {
NSString *errorString = [error userInfo][@"error"];
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Error" message:errorString delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert show];
}
}];
}];
}
-(void)myPhotos_setCurrentPhoto{
if ([myPhotos_imagesArray count] != 0) {
// Set photo in image view
PFFile *userImageFile = myPhotos_imagesArray[_photoIndex][@"imageFile"];
[userImageFile getDataInBackgroundWithBlock:^(NSData *imageData, NSError *error) {
if (!error) {
UIImage *image = [UIImage imageWithData:imageData];
self.myPhotos_imageViewCurrentPhoto.image = image;
}
}];
}
}
PS 我在完成回调内部和外部尝试了该算法 - 相同的结果。
更新 2(添加了整个 EXViewController.h 文件):
#import <UIKit/UIKit.h>
#import <Parse/Parse.h>
@interface EXViewController : UIViewController <UITextFieldDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate> {
UIImagePickerController *upload_imagePickerCamera;
UIImagePickerController *upload_imagePickerLibrary;
UIImage *upload_imageUploaded;
IBOutlet UIImageView *upload_imageViewUploadedImage;
// Photo Currently viewed
NSInteger _photoIndex;
NSMutableArray *photos_imagesArray;
NSMutableArray *myPhotos_imagesArray;
}
// Login Form
@property (strong, nonatomic) IBOutlet UITextField *login_fieldEmail;
@property (strong, nonatomic) IBOutlet UITextField *login_fieldPassword;
- (IBAction)login_buttonSignInAction:(id)sender;
- (IBAction)login_buttonForgotAction:(id)sender;
// Sign Up Form
@property (strong, nonatomic) IBOutlet UITextField *signup_fieldEmail;
@property (strong, nonatomic) IBOutlet UITextField *signup_fieldPassword;
@property (strong, nonatomic) IBOutlet UITextField *signup_fieldCountry;
@property (strong, nonatomic) IBOutlet UISegmentedControl *signup_fieldSex;
@property (strong, nonatomic) IBOutlet UITextField *signup_fieldYear;
- (IBAction)buttonSignupAction:(id)sender;
// Photos
@property (weak, nonatomic) IBOutlet UIImageView *photos_imageViewCurrent;
- (IBAction)photos_buttonRatePlus:(id)sender;
- (IBAction)photos_buttonRateMinus:(id)sender;
// Menu
- (IBAction)menu_buttonSignOutAction:(id)sender;
- (IBAction)menu_buttonMyPhotos:(id)sender;
// Upload Photo
- (IBAction)upload_buttonTakePhoto:(id)sender;
- (IBAction)upload_buttonPhotoAlbum:(id)sender;
- (IBAction)upload_buttonUpload:(id)sender;
@property (strong, nonatomic) IBOutlet UIProgressView *upload_progressViewUpload;
@property (strong, nonatomic) IBOutlet UILabel *upload_labelImageUploaded;
@property (strong, nonatomic) IBOutlet UIActivityIndicatorView *upload_gravity;
// My Photos
@property (weak, nonatomic) IBOutlet UIImageView *myPhotos_imageViewCurrentPhoto;
@property (weak, nonatomic) IBOutlet UILabel *myPhotos_labelRating;
@property (weak, nonatomic) IBOutlet UILabel *myPhotos_labelVotes;
- (IBAction)myPhotos_buttonNext:(id)sender;
- (IBAction)myPhotos_buttonPrevious:(id)sender;
@end
答: 暂无答案
上一个:Swift 中的不可变/可变集合
下一个:了解副本和可变副本
评论