1

I have trouble understanding segues and how they work and pass objects. Basically I have a calculator and am trying to graph the objects stored in an array. So far I have an object called brain which is an instance of CalculatorBrain. Now brain has an NSArray property that I use as a stack to store variables. Let's say I add the values 3 and 5 to the array and then want to segue. I have my segue selected to a button called "Graph" so when I click the button it segues. How would I pass brain to the new view controller that I am segueing to? I have a property called setGraphingPoint which is defined in the new view controller which I think should accept the passed object. Also, if I pass brain through a segue will the values 3 and 5 be passed along with it or will it create a new object of CalculatorBrain? Here is what I have so far.

This is defined in the new view controller

@property (nonatomic, strong) CalculatorBrain *graphingPoint;
@synthesize graphingPoint = _graphingPoint;

-(void) setGraphingPoint:(CalculatorBrain*) graphingPoint{

_graphingPoint = graphingPoint;
[self.graphingView setNeedsDisplay];

}

This is called from the old view controller which will have button to segue

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{

if([segue.identifier isEqualToString:@"Graph"])
    [segue.destinationViewController setGraphingPoint:[self.brain program]];
Terrel Gibson
  • 481
  • 1
  • 6
  • 21
  • What's going wrong? Can you be more specific about what's not working? – Nosrettap Jul 19 '12 at 21:18
  • I just dont fully understand how to pass and object and what exactly is passed – Terrel Gibson Jul 19 '12 at 21:22
  • 1
    In this example, you are passing a pointer to the object `property`. This means that it is a reference to the original object being passed and NOT a copy. Imagine if I was using my finger to point at a dog. Suddenly you walk up next to me. I can then tell you to also use your finger to point at the dog. We are both referring to the EXACT same object (but note that the dog doesn't actually move) – Nosrettap Jul 19 '12 at 21:27
  • I thought that you are supposed to keep multiple MVC's disclosed from each other so they dont know each others methods, would a pointer be a bad thing? Also, how would I call a method that is in another controller without importing the file and using segues – Terrel Gibson Jul 19 '12 at 21:38
  • You are supposed to keep MVCs as separated as possible; however, you still need some ability to communicate between them. If you're worried about classes knowing too much about each other, use protocols. For instance, create a `GraphPointUsing` protocol which defines the `setGraphingPoint` method. This way you only have to import the protocol and not the entire new view controller. See my answer below. – Nosrettap Jul 19 '12 at 21:41
  • What if I dont use protocols and instead try to pass a class instance using segues, would the new passed object have access to the methods and properties in its old view controller? – Terrel Gibson Jul 19 '12 at 21:44
  • It would have access to the methods and properties declared in the `.h` file...(This is not entirely true, but for your purposes it's close enough to the truth). – Nosrettap Jul 19 '12 at 21:47
  • how would I then call the method? I tried this [self.graphingPoint "METHODNAME"]; but im not sure if that works since that would be a setter? – Terrel Gibson Jul 19 '12 at 21:49

1 Answers1

2

You can use protocols. For instance, you can have your prepareForSegue look like this:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    id destination = segue.destinationViewController;
    if([destination conformsToProtocol:@protocol(GraphPointUsing)])
        [destination setGraphingPoint:[self.brain program]];
}

Then you just have to make sure the the ViewController that you are segueing to conformist to GraphPointUsing.

If you do not want to use protocols, but you still want to call methods on GraphPoint you can do this:

//In new ViewController suppose we want to call the method `foo` on `GraphPoint`
[self.graphingPoint foo];

//Or if we want to call a setter we can do
[self.graphingPoint setFoo:5];
Nosrettap
  • 10,940
  • 23
  • 85
  • 140