Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

ethana

macrumors 6502a
Original poster
Jul 17, 2008
836
0
Seattle, WA
How do I pass data from MyViewController to MyView?

I need an 'int' in MyViewController.m passed to MyView.m so I can use the variable within my drawRect: function.

How do I do this?

Thanks!
 
This thread discusses a similar topic:
passing Data from AppDelegate to UITabBarController

Basically, there are at least four different approaches to this issue of "scoping data":
1) Create a property in the "receiver" (MyView) and have the "sender" (MyViewController) set it (recommended if this property clearly belongs to the receiver and nothing else)
2) Create a property in the app delegate that both objects can access
3) Create a singleton that both objects can access
4) Set the value in NSUserDefaults in the "sender" and have the "receiver" retrieve it
 
Here is the code I have:

Code:
// in UntitledViewController.m

- (IBAction)hit {
	UntitledView *untitledView = [[UntitledView alloc] init];
	untitledView.y = 2;	
	self.view = untitledView;
	[untitledView release];
	[self.view setNeedsDisplay];
}

When I do this, the y property is now getting set, which is awesome, but the screen turns black. My button disappears and everything is gone.

How do I fix this?
 
What is the y property supposed to be used for? And more details on what UntitledView is supposed to be / show would help too. Maybe even include the code for UntitledView.
 
y is just for x/y cooridnates that will be used in the drawRect function in UntitledView. The code I have is just an example to try and make it work. y is an int property.

Ultimately what I am trying to do is draw a line using CoreGraphics when a button is pushed in my UIViewController (UntitledViewController).

UntitledView is completely default if you were to add a new UIView class to your project. I have added pretty much no code to it besides the y property.
 
ethana, thanks for the clarification. If your UntitledView doesn't do anything special from a regular UIView right now, I would say the reason for the black screen is that it doesn't have anything in the view to draw. So, just a "blank space" appears due to this line:
Code:
self.view = untitledView;
 
Ethan, this is not the way to learn iPhone development. This question, and the one you posted at the Apple forum on how to draw a line are among the simplest, most fundamental, ideas of iPhone development.

Read the sticky at the top of this forum on how to learn iPhone development. Buy a book or three on iPhone development and work through those books. You will learn the answers to these simple questions and hundreds more in a short time.
 
ethana, thanks for the clarification. If your UntitledView doesn't do anything special from a regular UIView right now, I would say the reason for the black screen is that it doesn't have anything in the view to draw. So, just a "blank space" appears due to this line:
Code:
self.view = untitledView;

I guess here's my problem... sorry for not explaining it enough...

I am trying to write a program that when you push a button it will draw a line ON TOP of what is already on the screen. So for example: the button will still be there on the screen, but now a blue line will appear above it. It's my understanding that I can do this if I just get drawRect: to be called within my UntitledView, which will let me draw lines.

The problem I'm having is that I need to pass coordinates of where the line is to be drawn when the button is pressed. So therefore, I created the int y property. The 'y' int never gets set until self.view = untitledView; is called. But when that happens, everything goes black because as you say, UntitledView is empty.

But it's not empty... because in Interface Builder I put a button on the view, hooked it up to UntitledViewController, etc. So what is going on? How do I get the FULL view to redraw WITH the new line I create on top?

How do I get it so that drawRect draws ON TOP of what is on the screen, instead of loading a whole new blank view.

Ethan
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.