我需要在应用程序中直接从表视图中显示图片。我设法放置视图控制器并将其导入导航,当我打开该图片时,一切都很顺利。 问题是,当我尝试返回时,它首先在空视图上然后再次按回我转回到起始位置。 有人能告诉我这个问题。
以下是Picture View控制器的代码:
#import "PhotoGalleryVC.h"
@implementation PhotoGalleryVC
@synthesize images,tabBar,forward,backward,back,scrollView,imageWindow,titolo;
// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
// Custom initialization
index = 0;
images = [[NSMutableArray alloc] init];
}
return self;
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
// UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Back"
// style:UIBarButtonItemStyleBordered
// target:self
// action:nil]; //@sel
// self.navigationController.navigationItem.backBarButtonItem = backButton;
//
CGRect frame = CGRectMake(0, 0, 320, 480);
scrollView = [[UIScrollView alloc] initWithFrame:frame];
scrollView.pagingEnabled = NO;
//frame = CGRectMake(0, 44, 320, 416);
//imageWindow = [[UIImageView alloc] initWithFrame:frame];
scrollView.delegate = self;
scrollView.maximumZoomScale = 4.0;
scrollView.minimumZoomScale = 1.0;
//scrollView.zoomScale
[scrollView addSubview:imageWindow];
[scrollView addSubview:tabBar];
[scrollView addSubview:titolo];
scrollView.contentSize = CGSizeMake(320, 460);
[self.view addSubview:scrollView];
[backward setEnabled:NO];
[titolo setTextColor:[UIColor whiteColor]];
//[titolo setText:[NSString stringWithFormat:@"1/%i",[images count]]];
[titolo setText:[NSString stringWithFormat:@"%i/%i",index+1,[images count]]];
if ([images count] == 1) [forward setEnabled:NO];
if([images count] > 0) [self loadImage];
}
- (void)viewDidAppear:(BOOL)animated {
// [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:NO];
}
// Override to allow orientations other than the default portrait orientation.
/*
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait || UIInterfaceOrientationLandscapeRight);
}
*/
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
#pragma mark -
#pragma mark Metodi Delegate ScrollView
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(@"began");
}
//metodo delegate per zooming
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
// return some view that will be scaled
//[self.scrollView setNeedsDisplay];
return self.imageWindow;
}
/*
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
NSLog(@"didScroll");
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
NSLog(@"WillBeginDragging");
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
NSLog(@"willDecelerate");
}
- (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView {
NSLog(@"should");
return YES;
}
- (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView {
NSLog(@"didScrollToTop");
}
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView {
NSLog(@"willDecelerating");
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
NSLog(@"didEndDecelerating");
}
- (void)scrollViewDidZoom:(UIScrollView *)scrollView {
NSLog(@"didZoom");
}
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
NSLog(@"didEndScrollingAnimation");
}
*/
- (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(UIView *)view {
//NSLog(@"willBeginZoming");
if (tabBar.hidden == NO) {
[tabBar setHidden:YES];
[titolo setHidden:YES];
}
}
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale {
//NSLog(@"didEndZooming");
//NSLog(@"scala %f",scale);
if (scale == 1.0f) {
[tabBar setHidden:NO];
[titolo setHidden:NO];
}
}
#pragma mark -
#pragma mark Metodi Locali
- (void) addImageName:(NSString*)img {
if (img) {
[images addObject:[NSString stringWithString:img]];
}
}
- (IBAction)backAction {
//[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:NO];
[self dismissModalViewControllerAnimated:YES];
}
- (IBAction)backwardAction {
index--;
if (index == 0) [backward setEnabled:NO];
if (index < [images count]-1) [forward setEnabled:YES];
[self loadImage];
[titolo setText:[NSString stringWithFormat:@"%i/%i",index+1,[images count]]];
}
- (IBAction)forwardAction {
index++;
if ([images count]-1 == index) [forward setEnabled:NO];
if (index > 0) [backward setEnabled:YES];
[self loadImage];
[titolo setText:[NSString stringWithFormat:@"%i/%i",index+1,[images count]]];
}
- (void) loadImage {
imageWindow.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[images objectAtIndex:index] ofType:nil]];
}
#pragma mark -
@end
我在导航中将其称为:
- (void) performOnClickedItemAtIndex: (NSIndexPath *)indexPath onTableView: (UITableView *)tableView {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if([cell.textLabel.text isEqualToString:@"Mappa"]){
NSString *map = @"";
//central & east coast
if ([self.title isEqualToString:@"Centro Storico"]) {
map = @"mappacaorle.jpg";
}
if (![map isEqualToString:@""]) {
PhotoGalleryVC *gallery = [[PhotoGalleryVC alloc] initWithNibName:@"PhotoGalleryVC" bundle:nil];
[gallery addImageName:map];
gallery.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:gallery animated:YES];
}
}
Navigation *managedObject = (Navigation*) [fetchedResultsController objectAtIndexPath:indexPath];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"nav_parent_id.intValue == %d", [managedObject.nav_id intValue]];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Navigation" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
[fetchRequest setPredicate:predicate];
NSError *error;
NSArray *array = [managedObjectContext executeFetchRequest:fetchRequest error:&error];
if (array != nil) {
//NSLog(@"Sub items count=%d", [array count]);
if ([array count] > 0) {
NavTableView *navTable = [[NavTableView alloc] initWithStyle:UITableViewStylePlain];
navTable.title = managedObject.nav_title;
if ([managedObject.nav_title isEqualToString:@"Calendario Eventi"] == YES) {
WebViewController *wvc = [[WebViewController alloc] initWithNibName:@"WebViewController" bundle:nil urlStr:@"http://www.jesolo.it/iphone/?lang=ita#home"];
wvc.title = managedObject.nav_title;
[self.navigationController pushViewController:wvc animated:YES];
}else {
if ([managedObject.nav_title isEqualToString:@"Certified Italian Products"] == YES) navTable.title = @"C.I.P.";
navTable.managedObjectContext = self.managedObjectContext;
navTable.selectQuery = predicate;
navTable.parentID = indexPath.row; //[managedObject.nav_id intValue];
navTable.advert = [managedObject.nav_id intValue];
/*
navTable.navigationItem.leftBarButtonItem =[[[UIBarButtonItem alloc] initWithTitle:self.title
style:UIBarButtonItemStyleBordered
target:self
action:@selector(didBack:)]
autorelease];
*/
[self.navigationController pushViewController:navTable animated:YES];
[navTable addActiveMap:managedObject.nav_map_name];
}
} else {
// verify Restaurants, then
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *resEntity = [NSEntityDescription entityForName:@"Restaurant" inManagedObjectContext:managedObjectContext];
[request setEntity:resEntity];
// NSString *string1 = [NSString stringWithFormat:@"res_parent CONTAINS '[%d]'", [managedObject.nav_id intValue]];
NSString *string1 = nil;
if ([managedObject.nav_title isEqualToString:@"Around Me"])
string1 = [NSString stringWithFormat:@"res_tags = %d", self.id_tags_arroundme];
else
string1 = [NSString stringWithFormat:@"res_parent CONTAINS '[%d]'", [managedObject.nav_id intValue]];
//NSLog(@"Predicate=%@", string1);
NSPredicate *resPredicate = [NSPredicate predicateWithFormat:string1];
[request setPredicate:resPredicate];
NSArray *array2 = [managedObjectContext executeFetchRequest:request error:&error];
BOOL nothing = YES;
if (array2 != nil) {
//NSLog(@"Restaurants count=%d", [array2 count]);
if ([array2 count] > 0) {
if ([managedObject.nav_title isEqualToString:@"Around Me"]) {
//if section is function Around Me, then load them on mapview
if ([NetworkHelper connectedToNetwork] == YES) {
AddressController *mapController = [[AddressController alloc] initWithNibName:@"AddressController" bundle:nil withAroundMe:array2] ;
mapController.title = managedObject.nav_title;
[self.navigationController pushViewController:mapController animated:YES];
}
else{
//no network connection
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Caorle Official Mobile Guide"
message:@"No network connections available."
delegate:nil
cancelButtonTitle:@"Close"
otherButtonTitles:nil, nil];
[alert show];
return;
}
nothing = NO;
}
else{
ResTableView *resTable = [[ResTableView alloc] initWithStyle:UITableViewStylePlain];
resTable.title = managedObject.nav_title;
resTable.managedObjectContext = self.managedObjectContext;
resTable.selectQuery = resPredicate;
resTable.parentID = indexPath.row; //[managedObject.nav_id intValue];
[self.navigationController pushViewController:resTable animated:YES];
if (managedObject.nav_html != nil) [resTable addHtmlHeader:managedObject.nav_html];
nothing = NO;
}
}
}
if (nothing == YES) {
if (managedObject.nav_html != nil) {
NSLog(@">> HTML:%@",managedObject.nav_html);
if ([managedObject.nav_html hasPrefix:@"{ID}"]) {
int res_id = [[managedObject.nav_html substringFromIndex:4] intValue];
NSLog(@"DIRECT ID >> %d", res_id);
NSFetchRequest* reqForRestaurant = [[NSFetchRequest alloc] init];
NSEntityDescription *resEntity2 = [NSEntityDescription entityForName:@"Restaurant" inManagedObjectContext:managedObjectContext];
[reqForRestaurant setEntity:resEntity2];
NSString *stringRes = [NSString stringWithFormat:@"res_id.intValue == %d", res_id];
NSPredicate *resPredicate2 = [NSPredicate predicateWithFormat:stringRes];
[reqForRestaurant setPredicate:resPredicate2];
NSArray *array3 = [managedObjectContext executeFetchRequest:reqForRestaurant error:&error];
//NSLog(@">>>> Gallery:%d", [managedObject.res_gallery intValue]);
if (array3 != nil) {
//NSLog(@"Restaurants count=%d", [array2 count]);
if ([array3 count] == 1) {
Restaurant *managedObjectRes = (Restaurant*) [array3 objectAtIndex:0];
RestaurantDetails *details = [[RestaurantDetails alloc] initWithStyle:UITableViewStylePlain];
details.managedObjectContext = self.managedObjectContext;
details.parentID = [managedObject.nav_id intValue];
details.title = managedObjectRes.res_title;
[details initItemsWithData:managedObjectRes];
[self.navigationController pushViewController:details animated:YES];
}
}
} else if ([managedObject.nav_html hasPrefix:@"{view}"]) {
if ([[managedObject.nav_html substringFromIndex:6] isEqualToString:@"meteo"]) {
Meteo* meteo = [[Meteo alloc] initWithNibName:@"Meteo" bundle:nil];
meteo.title = @"Meteo";
[self.navigationController pushViewController:meteo animated:YES];
}
} else if ([managedObject.nav_html hasPrefix:@"{YouTube}"]) {
[self setUrlToOpen:[managedObject.nav_html substringFromIndex:9]];
[self requestUserToAccessNetwork];
[tableView deselectRowAtIndexPath:indexPath animated:NO];
} else if ([managedObject.nav_html hasPrefix:@"http://"]) {
[self requestUserToAccessNetwork];
urlToOpen = managedObject.nav_html;
currentTitle = managedObject.nav_title;
[tableView deselectRowAtIndexPath:indexPath animated:NO];
nothing = NO;
}
else {
NavTableView *navTable = [[NavTableView alloc] initWithStyle:UITableViewStylePlain];
navTable.title = managedObject.nav_title;
navTable.managedObjectContext = self.managedObjectContext;
navTable.selectQuery = [NSPredicate predicateWithFormat:@"nav_parent_id.intValue == %d", managedObject.nav_id]; // parent id == 0 is ROOT
[self.navigationController pushViewController:navTable animated:YES];
[navTable addHtmlHeader:managedObject.nav_html];
}
}
}
}
}
}
首发看起来像这样:
结果是:
当我试着回去时,它去了:
我想放弃这最后一步并直接回到第一张照片。
管理以找到解决方案。我无法回复8小时,所以编辑问题。
在performOnClickedItemAtIndex中:
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if([cell.textLabel.text isEqualToString:@"Mappa"]){
NSString *map = @"";
//central & east coast
if ([self.title isEqualToString:@"Centro Storico"]) {
map = @"mappacaorle.jpg";
}
if (![map isEqualToString:@""]) {
PhotoGalleryVC *gallery = [[PhotoGalleryVC alloc] initWithNibName:@"PhotoGalleryVC" bundle:nil];
[gallery addImageName:map];
gallery.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:gallery animated:YES];
}
}
如果像这样构造,我在main中添加了选项:
if (array != nil) {
//NSLog(@"Sub items count=%d", [array count]);
if ([managedObject.nav_title isEqualToString:@"Mappa"] == YES ||
[managedObject.nav_title isEqualToString:@"Centro Storico"] == YES) {
PhotoGalleryVC *pvc = [[PhotoGalleryVC alloc] initWithNibName:@"PhotoGalleryVC" bundle:nil];
[pvc addImageName:@"mappacaorle.jpg"];
pvc.modalTransitionStyle = UIModalTransitionStylePartialCurl;
[self presentModalViewController:pvc animated:YES];
//[self.navigationController pushViewController:pvc animated:YES];
return;
} else if ...