How do I populate a UIImage via button press on another view?
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:
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.
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.
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.
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.
Figured I post everything for anyone else who is killing themselves figuring out delegates:
main view controller .h
Code:
#import "ImagePickerPopoverController.h"
//add to interface
<ImagePickerPopoverControllerDelegate>
//add method
- (void)didClickButton:(id)sender;
main view controller .m
Code:
//delegate method fired on behalf of the popover vier controller
- (void)didClickButton:(NSString *) buttonName {
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:nil message:@"This totally works!!!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease];
[alert show];
}
//method to display the popover controller
- (IBAction)setImageButtonPressed:(id)sender {
if ([self.popoverController isPopoverVisible]) {
[self.popoverController dismissPopoverAnimated:YES];
} else {
ImagePickerPopoverController* ipPopover = [[ImagePickerPopoverController alloc] init];
popoverController = [[UIPopoverController alloc]
initWithContentViewController:ipPopover];
//this is one of the lines that I was missing before that was KILLING me!!!!!
ipPopover.delegate = self;
[ipPopover release];
self.contentSizeForViewInPopover = CGSizeMake(320.0, 485.0);
[popoverController presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
}
popover controller .h
Code:
@protocol ImagePickerPopoverControllerDelegate
- (void)didClickButton:(NSString *) buttonName;
//after @interface
id<ImagePickerPopoverControllerDelegate> delegate;
@property (nonatomic, assign) id<ImagePickerPopoverControllerDelegate> delegate;
//action on the popover controller that will fire the delegate method in the main view controller
- (IBAction)aButtonPressed:(id)sender;
popover controller .m
Code:
@synthesize delegate;
//IBAction on the popover controller,
- (IBAction)aButtonPressed:(id)sender {
//just getting the button's name and storing it fore now (Il'll need it later)
UIButton *temp = (UIButton *) sender;
NSString *buttonName = [temp titleForState:UIControlStateNormal];
if (delegate != nil) {
[delegate didClickButton:buttonName];
}
}
Would I be able to do some kind of FOR statement in my save method? Something like:
Code:
for ([myObject.name isEqual:@"buttonOne"]) {
[myObject setValue:@"test" forKey:@"imageChosen"];
}
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?
Would I be able to do some kind of FOR statement in my save method? Something like:
Code:
for ([myObject.name isEqual:@"buttonOne"]) {
[myObject setValue:@"test" forKey:@"imageChosen"];
}
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.
Figured I post everything for anyone else who is killing themselves figuring out delegates:
main view controller .h
Code:
#import "ImagePickerPopoverController.h"
//add to interface
<ImagePickerPopoverControllerDelegate>
//add method
- (void)didClickButton:(id)sender;
main view controller .m
Code:
//delegate method fired on behalf of the popover vier controller
- (void)didClickButton:(NSString *) buttonName {
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:nil message:@"This totally works!!!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease];
[alert show];
}
//method to display the popover controller
- (IBAction)setImageButtonPressed:(id)sender {
if ([self.popoverController isPopoverVisible]) {
[self.popoverController dismissPopoverAnimated:YES];
} else {
ImagePickerPopoverController* ipPopover = [[ImagePickerPopoverController alloc] init];
popoverController = [[UIPopoverController alloc]
initWithContentViewController:ipPopover];
//this is one of the lines that I was missing before that was KILLING me!!!!!
ipPopover.delegate = self;
[ipPopover release];
self.contentSizeForViewInPopover = CGSizeMake(320.0, 485.0);
[popoverController presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
}
popover controller .h
Code:
@protocol ImagePickerPopoverControllerDelegate
- (void)didClickButton:(NSString *) buttonName;
//after @interface
id<ImagePickerPopoverControllerDelegate> delegate;
@property (nonatomic, assign) id<ImagePickerPopoverControllerDelegate> delegate;
//action on the popover controller that will fire the delegate method in the main view controller
- (IBAction)aButtonPressed:(id)sender;
popover controller .m
Code:
@synthesize delegate;
//IBAction on the popover controller,
- (IBAction)aButtonPressed:(id)sender {
//just getting the button's name and storing it fore now (Il'll need it later)
UIButton *temp = (UIButton *) sender;
NSString *buttonName = [temp titleForState:UIControlStateNormal];
if (delegate != nil) {
[delegate didClickButton:buttonName];
}
}
Thanks again for all your help.
got it to work... the - (void)didClickButtonid)sender; in the .h mainview does not need to be there. you added to the .h in the popover view.
where you able to set different button to one method? - (void)didClickButtonNSString *) buttonName {
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
Quote:
Originally Posted by krye
Would I be able to do some kind of FOR statement in my save method? Something like:
Code:
for ([myObject.name isEqual:@"buttonOne"]) {
[myObject setValue:@"test" forKey:@"imageChosen"];
}
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?