1

Is it possible to communicate with a viewcontroller after a segue switched to another viewcontroller? I want to know, because I need data from a viewcontroller passed on to the next viewcontroller,

but I just can't figure out how to get data from one viewcontroller to another, as they have no unique names or something which I can use to communicate with.

So can someone tell me: If it is possible to communicate between viewcontrollers, and if possible, how?

laarsk
  • 862
  • 6
  • 17
  • 36

4 Answers4

2

Usually you do it the other way around, pushing values from the source to the destination. If you implement prepareForSegue in the view controller that is going to be segue'd out, you can use

[segue destinationViewController]

to get a reference to the destination view controller. Then you can set any values in that controller that are needed using properties on that controller before it segues in.

Joachim Isaksson
  • 176,943
  • 25
  • 281
  • 294
  • Hmmm, that sounds good. But there is no way to communicate with the previous viewcontroller? (I've actually already used that prepareForSegue method before, so i know that works :) ) – laarsk Jan 29 '12 at 15:38
  • You could always put a UIViewController* property on the target view controller and push a reference to the old controller into it using the technique above. Then you can use that property to reach back to the previous UIViewController. Problem is, when using ARC you may end up with a circular reference, so probably good to make it weak. – Joachim Isaksson Jan 29 '12 at 15:42
  • Ehm, I'm not sure how to do that, could you give an example? Also, when I try this: "[[segue destinationViewController] setUserName]" I get the error: No known instance method for selector setUserName, what does that mean? – laarsk Jan 29 '12 at 15:48
  • @laarsk Means you don't have a setUserName method on the destination controller that does not take a parameter (like the actual user name you want to set?) – Joachim Isaksson Jan 29 '12 at 15:52
  • Well, there is actually... The destination viewcontroller is class SubViewController, and I've written a method in it: `-(void)setUserName:(NSString*)newUserName { welcomeLabel.text = newUserName; NSLog(@"Username set: %@.", newUserName); }` So it should exist, right? – laarsk Jan 29 '12 at 15:57
  • @laarsk I'm guessing you mean [[segue destinationViewController] setUserName:@"My UserName"]; though? – Joachim Isaksson Jan 29 '12 at 16:14
  • How do you mean? I call the function like you just typed, and have defined the function like I typed... – laarsk Jan 29 '12 at 16:19
1

try the views presentingViewController property.

mica
  • 3,898
  • 4
  • 34
  • 62
  • I actually tried that, but for some reason I cant access any functions/methods from that viewcontroller? The presenting viewcontroller is class ViewController with some custom functions, but when I try to access those functions with '[self.presentingViewController customMethod]' I get the error that method can not be found? – laarsk Jan 29 '12 at 15:44
  • I think you have to cast to yout class, to use your custom method – mica Jan 29 '12 at 15:47
  • I'm not sure what you mean, here, could you explain it a bit more? – laarsk Jan 29 '12 at 15:48
  • presentingViewController returns an object of class ViewController, which doesnot understand your custommethod. Try: [((yourclass) self.presentingViewController) customMethod] – mica Jan 29 '12 at 15:53
  • When I try ` [((SubViewController) self.presentingViewController) setUserName:nameField.text]` I get the error: `Used type SubViewController where arithmetic or pointer type is required` – laarsk Jan 29 '12 at 16:00
  • New error: `Receiver type 'SubViewController' for instance message does not declare a method with selector 'setUserName'` But it IS declared: `-(void)setUserName:(NSString*)newUserName { welcomeLabel.text = newUserName; NSLog(@"Username set: %@.", newUserName); }` – laarsk Jan 29 '12 at 16:23
  • And when I then add this to the @interface: `setUserName:(NSString*)newUserName;` that error disappears, but I get 10 in return... – laarsk Jan 29 '12 at 16:28
  • I fixed that error by declaring the function underneath the @interface: `-(void)setUserName:(NSString*)newUserName` but for some reason the app now crashes...: `*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key activeUser.' ` ***What does that mean?*** – laarsk Jan 29 '12 at 16:34
1

A typical OOP pattern is to create yet another object, a Model object (MVC paradigm), connect all the view controllers that need to communicate with this Model object, and pass any shared state or variables by setting and getting properties in this Model object. A common shortcut for very small apps is to use the App Delegate as a Model object, as any other controller can get a reference to the app delegate. But this shortcut is not very extensible to larger or reusable code.

hotpaw2
  • 70,107
  • 14
  • 90
  • 153
  • I do not understand a lot of what you're saying, but I do understand it is about some of the principles of MVC coding, so is there something you can refer me to (or just explain me) to learn more about this? I'm not familiar with the App Delegate (to be honest I don't even know what it is or what it's for), neither do I know what you mean by creating a model object. I have searched for tutorials about making a MVC based iPhone app, but without success... – laarsk Jan 29 '12 at 16:54
1

I have searched for tutorials about making a MVC based iPhone app, but without success

Goto ITunesU and look for Paul Hegartys "iPad and iPhone Application DEvelopment" from stanford university

very good!

mica
  • 3,898
  • 4
  • 34
  • 62