Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 01-01-2011, 12:40 PM   #1 (permalink)
Registered Member
 
krye's Avatar
 
Join Date: Jan 2009
Location: NY, USA
Posts: 359
krye is on a distinguished road
Default 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:

Code:
- (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
krye is offline   Reply With Quote
Old 01-01-2011, 01:08 PM   #2 (permalink)
Registered Member
 
krye's Avatar
 
Join Date: Jan 2009
Location: NY, USA
Posts: 359
krye is on a distinguished road
Default

Think I may be onto something. Shouldn't populate one view's images from another controller. Bad design. Instead, call a method:

In my detail view, added methods:
Code:
- (void)loadButtonOneImage {
	
	UIImage *stockImage = [UIImage imageNamed: @"imageOne.png"];
	[obverseImage setImage:stockImage];	
	
}

- (void)loadButtonTwoImage {} //etc, etc
- (void)loadButtonThreeImage {} //etc, etc
Now just need to figure out howe to call that method from the popoverviewcontroller.

Code:
- (IBAction)buttonOnePressed {
		
	DetailViewcontroller *detailView = [[DetailViewcontroller alloc] init];
	
	[detailView loadButtonOneImage];   //does not work???????
	
}

Last edited by krye; 01-01-2011 at 01:10 PM.
krye is offline   Reply With Quote
Old 01-01-2011, 01:46 PM   #3 (permalink)
Registered Member
 
Join Date: Dec 2009
Posts: 13
bizty is on a distinguished road
Default

Quote:
Originally Posted by krye View Post
Think I may be onto something. Shouldn't populate one view's images from another controller. Bad design. Instead, call a method:

In my detail view, added methods:
Code:
- (void)loadButtonOneImage {
	
	UIImage *stockImage = [UIImage imageNamed: @"imageOne.png"];
	[obverseImage setImage:stockImage];	
	
}

- (void)loadButtonTwoImage {} //etc, etc
- (void)loadButtonThreeImage {} //etc, etc
Now just need to figure out howe to call that method from the popoverviewcontroller.

Code:
- (IBAction)buttonOnePressed {
		
	DetailViewcontroller *detailView = [[DetailViewcontroller alloc] init];
	
	[detailView loadButtonOneImage];   //does not work???????
	
}

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/...elegation.html
And about protocols here:
https://developer.apple.com/library/...08195-CH45-SW1

So in the header of your PopoverViewController you shold have something like this

Code:
@protocol PopoverViewControllerDelegate <NSObject>
- (void) popoverViewController: (PopoverViewController *) popController didSelectImageAtIndex:(int) index;
@end
And have the instance variable delegate:
Code:
id <PopoverViewControllerDelegate> delegate;
That you should declare as a property also:
Code:
@property (nonatomic, assign) id<PopoverViewControllerDelegate> delegate;
And of course synthesize:
Code:
@synthesize delegate;
In your detailViewController when you create the popOverviewController, you should set the delegate to self. So you would have something like this:
Code:
PopoverViewController *popController = [[PopoverViewController alloc] init];
[popController setDelegate:self];
Then you should implement the didSelectImageAtIndex method in the detailViewController:
Code:
- (void) popoverViewController: (PopoverViewController *) popController didSelectImageAtIndex:(int) index {
	switch (index) {
		case 0:
			UIImage *stockImage = [UIImage imageNamed: @"imageOne.png"];
			[obverseImage setImage:stockImag];
			break;
		case 1:
			UIImage *stockImage = [UIImage imageNamed: @"imageTwo.png"];
			[obverseImage setImage:stockImag];
			break;
		case 2:
			UIImage *stockImage = [UIImage imageNamed: @"imageThree.png"];
			[obverseImage setImage:stockImag];
			 break;
		default:
			break;
	}
}
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:
Code:
- (IBAction)buttonOnePressed {
	if (delegate && [delegate respondsToSelector:@selector(popoverViewController:didSelectImageAtIndex:)])
		[delegate popoverViewController:self didSelectImageAtIndex:0];
}
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.

Last edited by bizty; 01-01-2011 at 01:48 PM.
bizty is offline   Reply With Quote
Old 01-01-2011, 01:49 PM   #4 (permalink)
Use [code] tags please
 
Join Date: Jun 2009
Location: Jacksonville, FL
Posts: 410
timle8n1 is on a distinguished road
Default

Quote:
Originally Posted by krye View Post
Code:
DetailViewcontroller *detailView = [[DetailViewcontroller alloc] init];
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

Code:
- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController
in your DetailViewController.
timle8n1 is offline   Reply With Quote
Old 01-01-2011, 02:55 PM   #5 (permalink)
Registered Member
 
krye's Avatar
 
Join Date: Jan 2009
Location: NY, USA
Posts: 359
krye is on a distinguished road
Default

Thanks for the replies, still a little lost on delegation. I'll have to read up on it a bit more. Can't seem to get it to work.

Thanks.
krye is offline   Reply With Quote
Old 01-01-2011, 03:58 PM   #6 (permalink)
Registered Member
 
krye's Avatar
 
Join Date: Jan 2009
Location: NY, USA
Posts: 359
krye is on a distinguished road
Default

OK, now I'm officially confused. I thought the DetailViewController would be the delegate.

Also, the part about:
Code:
- (void) popoverViewController: (PopoverViewController *) popController didSelectImageAtIndex:(int) index {
	switch (index) {
		case 0:
			UIImage *stockImage = [UIImage imageNamed: @"imageOne.png"];
			[obverseImage setImage:stockImag];
			break;
		case 1:
			UIImage *stockImage = [UIImage imageNamed: @"imageTwo.png"];
			[obverseImage setImage:stockImag];
			break;
		case 2:
			UIImage *stockImage = [UIImage imageNamed: @"imageThree.png"];
			[obverseImage setImage:stockImag];
			 break;
		default:
			break;
	}
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?

Last edited by krye; 01-01-2011 at 04:01 PM.
krye is offline   Reply With Quote
Old 01-01-2011, 05:42 PM   #7 (permalink)
Registered Member
 
krye's Avatar
 
Join Date: Jan 2009
Location: NY, USA
Posts: 359
krye is on a distinguished road
Default

Quote:
Originally Posted by bizty View Post
....
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.
krye is offline   Reply With Quote
Old 01-01-2011, 06:45 PM   #8 (permalink)
Registered Member
 
krye's Avatar
 
Join Date: Jan 2009
Location: NY, USA
Posts: 359
krye is on a distinguished road
Default

I found a useful guide HERE.

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.
krye is offline   Reply With Quote
Old 01-02-2011, 08:11 AM   #9 (permalink)
Registered Member
 
Join Date: Dec 2009
Posts: 13
bizty is on a distinguished road
Default

Quote:
Originally Posted by krye View Post
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.
bizty is offline   Reply With Quote
Old 01-02-2011, 12:51 PM   #10 (permalink)
Registered Member
 
krye's Avatar
 
Join Date: Jan 2009
Location: NY, USA
Posts: 359
krye is on a distinguished road
Default

Quote:
Originally Posted by bizty View Post
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.
krye is offline   Reply With Quote
Old 01-04-2011, 09:38 PM   #11 (permalink)
Registered Member
 
krye's Avatar
 
Join Date: Jan 2009
Location: NY, USA
Posts: 359
krye is on a distinguished road
Default

Got it, after many hours & days of reading up on delegates, etc. Also, Chapter 4 of this $25 course was a big help: iOS SDK Tutorials | iPhone SDK Developing iPad Applications from Lynda.com

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.
krye is offline   Reply With Quote
Old 01-07-2011, 03:29 PM   #12 (permalink)
Registered Member
 
krye's Avatar
 
Join Date: Jan 2009
Location: NY, USA
Posts: 359
krye is on a distinguished road
Default

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?
krye is offline   Reply With Quote
Old 09-23-2011, 12:57 PM   #13 (permalink)
Registered Member
 
Join Date: Sep 2009
Posts: 6
mart0593 is on a distinguished road
Default

Quote:
Originally Posted by krye View Post
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.
mart0593 is offline   Reply With Quote
Old 09-23-2011, 02:08 PM   #14 (permalink)
Registered Member
 
Join Date: Sep 2009
Posts: 6
mart0593 is on a distinguished road
Default

Quote:
Originally Posted by krye View Post
Got it, after many hours & days of reading up on delegates, etc. Also, Chapter 4 of this $25 course was a big help: iOS SDK Tutorials | iPhone SDK Developing iPad Applications from Lynda.com

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.
mart0593 is offline   Reply With Quote
Old 09-23-2011, 04:05 PM   #15 (permalink)
Registered Member
 
Join Date: Sep 2009
Posts: 6
mart0593 is on a distinguished road
Default

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 View Post
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?
mart0593 is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 360
8 members and 352 guests
blueorb, fredidf, iAppDeveloper, iGamesDev, MarkC, mottdog, sacha1996, Touchmint
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,667
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, host number one
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 03:30 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0