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 02-22-2011, 03:30 PM   #1 (permalink)
Registered Member
 
Join Date: Jan 2011
Posts: 24
ChrisW is on a distinguished road
Question Passing a property between different classes

When I press a button in Class1, I open Class2. Once done in Class2 I take a screenshot, set it to a UIImage, and go back to Class1. I would like to take this UIImage from Class2 and pass it to a Class1 image, but I do not know how to do this.

This is what I have so far...
Code:
//Class2.m

CGRect screenRect = [[UIScreen mainScreen] bounds];
UIGraphicsBeginImageContext(screenRect.size);
    
CGContextRef ctx = UIGraphicsGetCurrentContext();
[[UIColor blackColor] set];
CGContextFillRect(ctx, screenRect);
    
[self.view.layer renderInContext:ctx];
 
UIImage *mapImage = UIGraphicsGetImageFromCurrentImageContext();

//
//This is where I want to set the Class1 UIImage to the mapImage
//

UIGraphicsEndImageContext();
ChrisW is offline   Reply With Quote
Old 02-22-2011, 03:41 PM   #2 (permalink)
Registered Member
 
simplyDusty's Avatar
 
Join Date: Jan 2011
Posts: 158
simplyDusty is on a distinguished road
Default

It depends on the relationship between Class1 and Class2 and what each of them are

Code:
//In Class1 you can do this if Class2 doesn't really do anything except make an image
Class2 *c2 = //init your class2 object
self.Image = [c2 GetScreenshotAsUIImage]; //Just return the UIImage you created in the posted method

//Or if there is an actual view that is shown for Class2 this should work
((Class1*)parentViewController).Image = mapImage;
If Class1 and Class2 are unrelated you can use a property in your app delegate

Last edited by simplyDusty; 02-22-2011 at 03:50 PM.
simplyDusty is offline   Reply With Quote
Old 02-22-2011, 03:45 PM   #3 (permalink)
Registered Member
 
Join Date: Jan 2011
Posts: 24
ChrisW is on a distinguished road
Default

How do I know if Class 1 owns Class2?
ChrisW is offline   Reply With Quote
Old 02-22-2011, 03:52 PM   #4 (permalink)
Registered Member
 
simplyDusty's Avatar
 
Join Date: Jan 2011
Posts: 158
simplyDusty is on a distinguished road
Default

Quote:
Originally Posted by ChrisW View Post
How do I know if Class 1 owns Class2?
Where is Class2 created? What type of Classes are these? NSObject, UIViewControllers, etc...
simplyDusty is offline   Reply With Quote
Old 02-22-2011, 04:08 PM   #5 (permalink)
Registered Member
 
Join Date: Jan 2011
Posts: 24
ChrisW is on a distinguished road
Default

Quote:
Originally Posted by simplyDusty View Post
Where is Class2 created? What type of Classes are these? NSObject, UIViewControllers, etc...
Thanks for the quick response!

I added Class2 as a UIViewController subclass with a XIB file. Class2 has a MKMapView and a gesture recognizer. I ultimately want to take this screenshot and set a Class1 button image.

I was trying to implement
Code:
((Class1*)parentViewController).Image = mapImage;
but parentViewController was undeclared. This was very similar to what I was trying...
Code:
((Class1*)[[UIApplication sharedApplication] delegate]).myImage = UIGraphicsGetImageFromCurrentImageContext;
ChrisW is offline   Reply With Quote
Old 02-22-2011, 04:31 PM   #6 (permalink)
Registered Member
 
simplyDusty's Avatar
 
Join Date: Jan 2011
Posts: 158
simplyDusty is on a distinguished road
Default

Yea, i wasn't sure if UIViewControllers had a parentViewController or not. You're trying cast your delegate as Class1 which is incorrect. Cast it to your actual AppDelegate. You'll need to add an image property to your AppDelegate class. Then in Class1 you can get your delegate the same way as in Class2 and grab the Image property from there
simplyDusty is offline   Reply With Quote
Old 02-22-2011, 07:59 PM   #7 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by ChrisW View Post
When I press a button in Class1, I open Class2. Once done in Class2 I take a screenshot, set it to a UIImage, and go back to Class1. I would like to take this UIImage from Class2 and pass it to a Class1 image, but I do not know how to do this.

This is what I have so far...
Code:
//Class2.m

CGRect screenRect = [[UIScreen mainScreen] bounds];
UIGraphicsBeginImageContext(screenRect.size);
    
CGContextRef ctx = UIGraphicsGetCurrentContext();
[[UIColor blackColor] set];
CGContextFillRect(ctx, screenRect);
    
[self.view.layer renderInContext:ctx];
 
UIImage *mapImage = UIGraphicsGetImageFromCurrentImageContext();

//
//This is where I want to set the Class1 UIImage to the mapImage
//

UIGraphicsEndImageContext();
Better still, use a singleton data container object. I wrote a post on the technique just yesterday:

Post on using a data container singleton object
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Reply

Bookmarks

Tags
delegate, property, screenshot, uiapplication, variable

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: 379
18 members and 361 guests
Absentia, akphyo, apatsufas, BinHex, cpsclicker, dre, Error404, Gaz, gmarro, jeroenkeij, Kirkout, mottdog, Music Man, PavelMik, teebee74, whitey99, Wikiboo
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,666
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, cpsclicker
Powered by vBadvanced CMPS v3.1.0

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