Reputation: 21
I am fairly new to programming in general, and have been following the CS193p videos on iTunesU. I am currently doing assignment 3, and am having trouble getting a bit of information from the View sent to the View Controller.
I believe I have set up the whole delegation thing correctly, so the question really is to how to get my View Controller to see a bit of information (such as self.bounds.size.width
), which is a property that only the View has. Would this involve using self.dataSource
? And if so, through what means could I pass this bit of information? My end goal is to have the View Controller perform some transformation to the View's properties, and send it back to the View's drawRect
so that it could be drawn.
Thanks!!
** Edit, as requested, I have posted parts of my drawRect code below
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGFloat scale = 32;
CGPoint midPoint;
midPoint.x = self.bounds.origin.x + self.bounds.size.width/2;
midPoint.y = self.bounds.origin.y + self.bounds.size.height/2;
// ---- finding the y starting point
float totalXsInWidth;
totalXsInWidth = self.bounds.size.width / scale;
float leftMostX = totalXsInWidth / -2;
float graphResultY = sin(leftMostX); // ** in theory, I want "leftMostX" to be modifed by the equation entered (in the CONTROLLER)
NSLog(@"The leftMostX is %f", leftMostX);
[self.dataSource passingVariable:leftMostX]; //** Here I pass the variable from drawRect to get modifyed in the CONTROLLER
float graphResultY1;
graphResultY1 = 5; //this is a test, I want to see if the controller actually effect a change
graphResultY1 = [self.dataSource calcResult:self]; //this should now be a different number than 5 or 0 (the init value)
NSLog(@"From graphingview, the result is %f", graphResultY1); //** unfortunately = 0... :(
Upvotes: 2
Views: 197
Reputation:
Having had a read of CS193p Assignment 3 (for anyone interested, it is available here in PDF format), it looks like you are being asked to create a protocol for your UIView subclass, and have the delegate of that protocol (the view's managing view controller) provide the data used for drawing.
If you have set up your protocol correctly, the view's drawRect
method should be asking the view controller for data through the protocol's method, something like:
DataObject *data = [self.delegate getData];
// It might be [self.dataSource getData]; in your case
That should call the getData
delegate method that you should have written into the view controller (I have made up a method signature for this, adapt to the one used by your protocol. Also, this code doesn't consider relevant memory management, if required):
- (DataObject *)getData
{
// Get data from the model, and return
DataObject *dataObjectToReturn = [Model getRelevantData];
return dataObjectToReturn;
}
The view's drawRect
should now have the relevant instance of DataObject
and can go about using that data to draw what it needs to draw.
Below is my original answer, which is not relevant to the specific problem above, but does show another method of view/view controller interaction. (This method isn't applicable to the above problem because the data needed for drawing shouldn't be owned by the view itself.)
To access the view's properties from that view's controller you would use self.view
. Thus, to get the view's width, like in your example, you would use:
CGFloat viewWidth = self.view.bounds.size.width;
You can also set the controller's view, though note that for your example you have to supply an entire CGRect for the frame, as manipulating the width directly is not allowed:
// This gets a reference to the view's frame, then uses values from it to create a new
// CGRect that is 10.0 points wider, and sets the frame of the view to the new frame
CGRect viewFrame = self.view.frame;
self.view.frame = CGRectMake(viewFrame.origin.x, viewFrame.origin.y, viewFrame.size.width + 10.0f, viewFrame.size.height);
Upvotes: 1
Reputation: 2556
Im not famaliar with the assignment but if you are using a view within a view controller you can access that views property like so:
SomeView *someView = [SomeView new];
CGFloat width = someView.bounds.size.width;
Upvotes: 0