In my iPad app, I have a main view. Via a button press, a detail view is displayed (modal view). On that detail view I have an image view. Pressing a tab bar button displays a popover with a a custom view of images to choose from. (They are really just buttons).
I would like to populate the UIImageView on the Detalview with image "x" based on the button press from the popover.
So my question is, how do I tell the detailview to populate the UIImageView from a button press from the popover view?
I though I could do something like this in the popoverviewcontroller.m:
I have an action wired to each of the buttons in the popover view:
- (IBAction)buttonOnePressed {
DetailViewController *detailView = [[DetailViewController alloc] init];
detailView.myImageView.image = [UIImage imageNamed:@"imageOne.png"];
}
- (IBAction)buttonTwoPressed {
DetailViewController *detailView = [[DetailViewController alloc] init];
detailView.myImageView.image = [UIImage imageNamed:@"imageTwo.png"];
}
But it doesn't load the image.
Sorry about the crude mockup, but maybe a picture would better explain it....
Thanks
<a href="
http://ineedcoffeetocode.com" target="_blank">iNeedCoffeeToCode.com</a> <a href="
http://rescuemyclassicmac.com" target="_blank">RescueMyClassicMac.com</a> <a href="
http://ryemac3.net" target="_blank">RyeMAC3.net</a>
Replies
In my detail view, added methods:
Now just need to figure out howe to call that method from the popoverviewcontroller.
I would use protocols and delegation. Your popoverviewcontroller should have a pointer to the detailViewcontroller called delegate. Then you can call methods on the detailViewcontroller. You use a protocol to make sure that your detailViewcontroller implements the needed methods to update the imageview. Another thing is that you don't need a method for every image. Why not just have one method and then pass a index or something.
You can read about delegation here:
https://developer.apple.com/library/ios/#documentation/General/Conceptual/DevPedia-CocoaCore/Delegation.html
And about protocols here:
https://developer.apple.com/library/ios/#documentation/General/Conceptual/DevPedia-CocoaCore/Protocol.html%23//apple_ref/doc/uid/TP40008195-CH45-SW1
So in the header of your PopoverViewController you shold have something like this
And have the instance variable delegate:
That you should declare as a property also:
And of course synthesize:
In your detailViewController when you create the popOverviewController, you should set the delegate to self. So you would have something like this:
Then you should implement the didSelectImageAtIndex method in the detailViewController:
The last thing you should do is to call the method when the user taps on a image in your popoverViewController. So just do something like this:
Notice that I check to see if is has a delegate and if the delegate implements the method before calling it.
I hope you get the idea.
This line will ALWAYS create a new instance of DetailViewController not access an existing one - which is want you want.
You could pass in the instance of DetailViewController to your popover - but IMHO that is bad design.
You should make your DetailViewController the delegate of your PopOverController and then implement
in your DetailViewController.
Thanks.
Also, the part about:
wouldn't work since the popover view is just a xib with some buttons on it. It's not a table view with indexes.
I've been messing with is code and searching online for over 3 hours now and still, I'm totally lost as to how this is supposed to work. Who is the delegate supposed to be? The one with the method who's called, or the one who does the calling?
Dang! I'm at my wits end with this! This should be so simple I know, but I've spent the past 5-6 hours trying to figure out how to make a delegate method. Must be missing something. I just don't get it. I've looked at a few tutorials, and it looks like I'm following the rules, but it just doesn't work!
Maybe the whole thing is backwards.
In my case, wouldn't the delegate be the detailview since it's the one that's going to populate the UIImageView? The popovercontroller, when a button is pressed, should send a method to the detail view as to what to do.
My code compiles without error. I think I got it right, but still nothing. My "load the imageview" method is not called by the popover viewcontroller.
The delegate should be the detailview yes! The what I'm doing when I set the delegate property of popoverviewcontroller to self.
Hard to say what you need to do in order to get it working. Should work if you follow my description.
Thanks, I''ll give it another go.
Figured I post everything for anyone else who is killing themselves figuring out delegates:
main view controller .h
main view controller .m
popover controller .h
popover controller .m
Thanks again for all your help.
I basically want to say "Hey, for the row in my database table that has "buttonOne" saved under the 'name' attribute, go ahead and save the string"test" under the 'imagesChosen' attribute.
I'm just finding it hard to save data to a row in the database table without using a table view and table view cells in the app.
So when saving data, writing to attributes, etc, how do you target a specific row in the database table?
been reading you code and trying to follow. i've been stuck on a similar problem... would you happen to have to source code for download. i can't seem to get yours. thanks..... i'm sort of stuck.
got it to work... the - (void)didClickButton:(id)sender; in the .h mainview does not need to be there. you added to the .h in the popover view.
i tried fooling around with if else if statement and UIButton tags. i can't seem to get it to work, as NSString does not have this property....
any thoughts???
s
imageView.userInteractionEnabled = true
//now you need a tap gesture recognizer
//note that target and action point to what happens when the action is recognized.
let tapRecognizer = UITapGestureRecognizer(target: self, action: Selector("imageTapped:"))
//Add the recognizer to your view.
imageView.addGestureRecognizer(tapRecognizer)
Now you still need the function, in this case, imageTapped:, which is where the action happens when the gesture is recognized. Mindmajix provides IOS training which makes you learn everything on the ios development and also rectify errors generated while developing.The gesture that was recognized will be sent as an argument, and you can find out which imageView was tapped from they gestures view property.
func imageTapped(gestureRecognizer: UITapGestureRecognizer) {
//tappedImageView will be the image view that was tapped.
//dismiss it, animate it off screen, whatever.
let tappedImageView = gestureRecognizer.view!
}