gdavis / FGallery-iPhone

Objective-C based gallery for iPhones
583 stars 144 forks source link

Question about using Segues #16

Closed JMattos closed 12 years ago

JMattos commented 12 years ago

I'm trying to use segues and storyboards with the FGalleryViewController and i think I'm missing something

I have my TableViewController (which implements and the methods)connected to a View Controller which I've changed to the type FGalleryViewController in the Story Board, and in the

(void)tableView:(UITableView )tableView didSelectRowAtIndexPath:(NSIndexPath )indexPath{ [self performSegueWithIdentifier:@"ShowGallery" sender:self]; }

Then, in my prepareForSegue, I have the following

if ([segue.identifier isEqualToString:@"ShowGallery"]) { FGalleryViewController controller = (FGalleryViewController ) segue.destinationViewController; controller.photoSource=sender; }

The problem is there's no way to do the "initWithPhotosouce" so I'm setting the photosource after the fact.

Everything seems to work, except that the photos arent showing up.. just black. I'm using the default network images in the example and I debugged so I know they exist and are being set right.

am I missing something?

JMattos commented 12 years ago

Also... sort of a similar question regarding StoryBoards and tabs. I dont see an easy way to control the instantiation of objects that act as the viewcontrollers in a set of tabs, so I'm not sure where I'd do the initWithDatasource.. how would I make FGallery occupy one of a set of tabs in the iOS5 world?

gdavis commented 12 years ago

Hi John,

I haven't tried to work with FGallery yet in a storyboards scenario, so I'm afraid you're mostly alone on this one. What you may need to do since you cannot call initWithPhotoSource is subclass the initWithCoder method and perform the same view setup operations performed in initWithPhotoSource. My guess is that those initial view layers are not being created since that is not being called. If you get it working in a branch, I'd be happy to merge those change back into the project. Good luck.

gdavis commented 12 years ago

Looks like I may be incorrect on adding the code from initWithPhotoSource into another subclassed method since it only assigns the datasource variable. Instead, you should look to see if initWithNibName... is called and all those setup operations perform properly with storyboards. I'd be curious to see what you find.

JMattos commented 12 years ago

Hey there

I got it, I think. Your response really helped, I think I'd been looking at it too long. Essentially, when the FGalleryViewController comes out of the story board, initWithCoder instead of initWithNib, so I just did the following..

  1. added in an initWithCoder as follows

(id)initWithCoder:(NSCoder *)aDecoder{ self=[super initWithCoder:aDecoder]; if (self) { [self initWithNibName:nil bundle:nil]; } return self; }

  1. Plopped a ViewController on my storyboard and changed its class to FGalleryViewController. I could also have subclassed it, I suppose and put the initWithCoder in the subclass (maybe called StoryBoardFGalleryViewController), I think that would have worked equally as well, making the ViewController on the StoryBoard a StoryBoardFGalleryViewController instead.

I think I'd only do that if I had an actual reason for subclassing other than not changing the original code, which might be compelling enough, come to think of it.

  1. Set up a segue from my table view cell called "ShowGallery" [self performSegueWithIdentifier:@"ShowGallery" sender:self];
  2. Set the photoSource in prepareForSegue as follows (using StoryBoardFGalleryViewController if I went with subclassing) (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{

if ([segue.identifier isEqualToString:@"ShowGallery"]) { FGalleryViewController controller = (FGalleryViewController ) segue.destinationViewController; controller.beginsInThumbnailView=YES; controller.photoSource=sender; }

Anyway, this turned into a bit of a rant ha ha ha, sorry about that. I think we've got it though. The only change I'd recommend is putting in initWithCoder so it'll work equally well with storyboards, and with nibs.

I'll look at the Tab version too to see if anything else is necessary, but I've got to go for a long run first. :)

JMattos commented 12 years ago

One downside I see to subclassing FGalleryViewController is that _barItems is a private (well as private as it can be in OBJC) property, so unless I make barItems a real property, I'm losing access to it.

What I did is make a @property called barItems on the FGalleryViewController.h and synthesize it as normal in the .m, then override the setBarItems so that it does the same thing that "initWithPhotoSource:barItems:" does, specifically

-(void) setBarItems:(NSArray*)items{ [_barItems addObjectsFromArray:items]; }

Then, my prepareForSegue looks like this

if ([segue.identifier isEqualToString:@"ShowGallery"]) {

    UIBarButtonItem *saveButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(savePhoto)];

    StoryBoardFGalleryViewController *controller = (StoryBoardFGalleryViewController *) segue.destinationViewController;
    controller.barItems=[NSArray arrayWithObjects:saveButton, nil];
    controller.photoSource=sender;
}

I can email you all this stuff if you like, too, if that will help you.

I didnt know how to fork the GIT Repo (I've never done that) but the changes are minor. I suppose that GitHub has some instructions on how to fork a repo?

Another slightly weird thing is that even if I dont change the _barItems. I'm not getting the left and right nav buttons automatically... do I need to create those manually? I dont see where they're being created in the FGallery "NetworkImages" page on the demo, but they're there...

J

JMattos commented 12 years ago

Okay, forget the thing about the navigation buttons. I'm an idiot... I forgot to import the images. Duh. ha ha ha.

BTW man great work on this. I totally love it, and I'm absolutely going to use it :)

JMattos commented 12 years ago

Alright, hell yeah. I got it working on a tab as well. Simple as can be.

The one thing I dont know how to do is keep the StoryBoardFGalleryViewController from taking over the full screen (and obscuring the tabs)... looking into that now.

  1. Create a Tab App in xcode
  2. Add the FGallery code (including the changes I mention above to make the _barItems a public property
  3. Drag out a navigation controller
  4. subclass FGalleryViewController, call it StoryBoardFGalleryviewController, make sure it's an FGallert
  5. Change the class of the navigation controller's root controller to "StoryBoardFGalleryViewController"
  6. Add the initWithCoder to StoryBoardFGalleryViewController as follows (id)initWithCoder:(NSCoder *)aDecoder{ NSLog(@"in initWithCoder of StoryBoardFGalleryViewController"); self=[super initWithCoder:aDecoder]; if (self) { self = [super initWithNibName:nil bundle:nil]; } self.dataModel = [[DataModel alloc]init]; // I have a DataModel class that holds my imageURLs and imageCaptions self.dataModel.delegate=self; // and the DataModel has a delegate self.photoSource=self; return self; }
  7. Add the FGalleryViewControllerDelegate Methods

I can zip up the project and email it to you if you like orI can post a zip to download.

gdavis commented 12 years ago

Glad to hear you got it working. If you want to put it in your own repo or try to get a fork going I'd prefer to take a look at your source code that way. I'd have to post my personal email on here for you to send me files, and I don't really want to do that :)

In regards to your comments about the private _barItems property, I'll have to take another look at that and see if its a good idea to make it public. My initial thoughts is that it'd be a nice addition so you don't have to rely on the init methods to define those items. Thanks for the feedback, and glad you're enjoying using it.

JMattos commented 12 years ago

Do you have any guidance on how to force the gallery to not take up the full screen for use with tabs? That's my only remaining problem :)

I'll try to figure out the whole forking thing, if now, I'll zip it up and put it on a public dropbox site you can grab. I agree about posting your email, I wasnt thinking.

pacosxm commented 12 years ago

@GladiatorApps I'm realy intresting by your code solution, can you me a link to download it please ?

kasperfn commented 12 years ago

For others with storyboards and segues:

This works for me