I have set up a nav controller, which works perfectly.
I've seen Lecture 7 of CS193P (Winter 2010), which details nav controllers. It shows how to pass data into a new view controller—I understand that part. I don't understand how to pass data back. The lecture talked about properties, but I'm not very comfortable or familiar with them. When I pass the data back, I want the table view embedded in the previous view (as in, there's a table view in previousViewController.view, but previousViewController itself is *not* a subclass of UITableViewcontroller) to reload itself. I'm not sure how to go about doing this.
In addition, I'd like to do the same thing with modal views, except to pass the data back when the modal view is dismissed.
I have set up a nav controller, which works perfectly.
I've seen Lecture 7 of CS193P (Winter 2010), which details nav controllers. It shows how to pass data into a new view controller—I understand that part. I don't understand how to pass data back. The lecture talked about properties, but I'm not very comfortable or familiar with them. When I pass the data back, I want the table view embedded in the previous view (as in, there's a table view in previousViewController.view, but previousViewController itself is *not* a subclass of UITableViewcontroller) to reload itself. I'm not sure how to go about doing this.
In addition, I'd like to do the same thing with modal views, except to pass the data back when the modal view is dismissed.
Until you become very familiar with properties, you are not going to be successful in Objective-C programming. Take the time now to learn about them. Brian Slick has a very nice tutorial on them. See his signature on any of his postings. As for reloading a table view, there is a method for that. It is called reloadData. Did you know that in XCODE you can place the cursor over any class name in your code and hold the ALT key and click on it to bring up the complete class reference for that class? It is very good documentation. Anyway, you just need to know when to use the reloadData method. Since the previous view is going to be coming back into view, maybe the viewWillAppear method for that view controller could be the place where you could do it.
I just did, but most of the relevant results weren't helpful. I know how to pass data forward, but not back. In addition, I don't know how to get the original view to reload itself based on the data that it receives back.
Basically, parentViewController contains a table view (but it's not a subclass of UITableViewController because I need to display other things). When a row is tapped, it loads detailViewController, which allows the user to change some things. I'm saving all of those changes into a single NSArray. I'd like to be able to pass the NSArray back to parentViewController just as detailViewController is dismissed (when the user taps the back button). I'd then like for parentViewController to reload its table view.
I am also doing this with an Add (+) button in the nav bar, which brings up a modal view controller—like the process of adding songs to the On-The-Go playlist in the Music app.
I would totally like to double High Five this question. Musicwind, I am having the exact same problem. Sofar, this is what I have come up with.. all unsuccessfully.
I am total noob so please take this all with a pleasantly wandering commentary feel.. none should be taken as rule...
As I see it, there are about a billion ways to do this..
1. Create a Singleton:
Plus: It is like a global variable, which is cool in its ease of getting to. You could then make it a pass through that you could get variables from when first level controller loads. Negatives: these oop dudes really dont like them unless you have to use them. Its all fung in the shwei of OOP, etc.etc.
2. Create an instance in your app delegate with pointers. That way The two controllers aren't talking to each other.
3. Another Idea I'm trying.. Try to figure out what the name of the instance of FirstLevelViewController is. that way you could message it directly. I think this would work. Its still out there in memory I think, and it has a name. Its just because rootviewcontroller sets it up automagically at startup I dont know what the instance id is so I can't figure out how to message it.
3. Finally, and I think what Im trying to go with, would be a bit of a purist OOP in my estimation.. Which would be creating a model class with one instance of said class that would hold the data. You could then have the view controllers reference instanced model class on viewdidappears. I think this is good because 1. It is keeping data out of the controller (which lets face it this data really is data not related to the view controller other than for its viewing purposes). So, that information out of the view controller and into a happy little data class of its own. This way you could name the instance on your own at some point, you would know its name for messaging purposes.
Other options I've seen thrown about involve using NSNotification center, but I think these people were diseased or high at the time. Crazy talk.
Please let me know if you figure this out. Others, feel free to smash/bash/trash all of this commentary. I'm really excited about figuring this out.
O.k. so I tried one of those ramblings and would like some help.
I made a Model named "Model.h"
@interface Model : NSObject {
NSString *date;
}
@property (nonatomic, retain) NSString *date;
@end
I then attempted to implement said Model - named "Model.m" (I know this is probably off)
#import "Model.h"
@implementation Model
@synthesize date;
@end
In my firstLevelViewController, on viewdidload, I believe I created an instance of this object.. confusingly named "model" just to screw myself.
Model *model = [[Model alloc]init];
model.date = @"Oh No You Didn't";
self.title = [model date]; <-- just to try and figure out what is stored in there.
Now, when they go to one of my second level view controllers. They use a datepicker to pick a date, I figure out how old they are in months, make a string saying "you're this old".. store it into a *message.
What I want to do now, I think, is to send a message to the instance of my data model, which is of class Model and instance model and update the date string to the message string... Any ideas how to do this? when I do this:
[Model model.date = message];
Warning: expected ']' before '.' token.
and
"Model may not respond to '+model'.
Any ideas how to clean this up? I would be forever indebted.
If you had an pointer to your model in that 2nd class, you would just need to do
Code:
model.date = message;
when I do that:
model.date = message;
it returns
error: 'model' undeclared (first use in this function).
I think you're spot on with this "pointer" business.. I've heard people talking about them. They sound important. Any ideas how to add some pointing action to that Model class model.date variable?
error: 'model' undeclared (first use in this function).
Hence my comment "If you had an pointer to your model in that 2nd class"
Since you want this to be sort of a global data management class, you really dont need to worry about the "pointer" business. However, it seems like you're lacking some fairly basic programming knowledge that has nothing to do with obj-c/iphone programming.
I'd google for some tips on how to make your Model class into a singleton.
"some fairly basic programming knowledge that has nothing to do with obj-c/iphone programming."
Probably a good *understatement. Get it? it was a pointer joke.
Though I have been toiling with this singleton idea.. I would really prefer struggling through this one instance of a data class idea, and learning how to make a pointer to it, as this is a mondo critical step in having classes talk together. I'm trying really hard to be as pure of an oop person from the get-go as it seems like a fun thing to laugh about at cocktail parties.
Most times when I read about pointers, they start getting into the "its a location, like a phone number" to a data location. If I'm not mistaken I did make a pointer to model when I created the instance of it in the firstviewcontroller. So, I think I just need to point my second class to that pointer?
Any help? speaking slowly in small words would be appreciated.
That depends totally on the structure of your application. Post some code. Just enough to show what you are attempting with class 1 and class 2.
I think I have 3 classes: view controllers named FirstLevelViewController, A secondlevelviewcontroller called "Birthday", and a Data Object NS object named Model.
I would like instances of firstlevelviewcontroller and birthday to be able to get and send messages from model.
Up at the top I posted my model's .h and .m.
FirstlevelViewController actually creates the Model instance "model"
Isn't there a specific formatting option for code? I'm used to seeing code the way Xcode displays it; my eyes glaze over the code when it's just typed out.
Anyway, I've been playing around with a new project, just to test this. In the RootViewController, I've defined a label and a button. In the SecondViewController, I've defined two buttons and a label. Here's the code: *
There seems to be an issue with the protocol, but as I said before, I'm not very good with protocols.
The interface is set up properly, and everything's connected. I've narrowed down the issue to the protocol. The app works perfectly, except that it crashes when I click the back button from SecondViewController.
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (void)viewDidUnload {
// Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
// For example: self.myOutlet = nil;
self.display = nil;
self.displayText = nil;
}
I think it looks fantastic! really good stuff. I have no idea what a protocol is or does, but boy it sure does look cool.
Again, I will ask... How to I set a pointer from one class object to a different class object? Its really making me bonkers. I feel like Im taking crazy pills here.
I think it looks fantastic! really good stuff. I have no idea what a protocol is or does, but boy it sure does look cool.
It is cool...in theory. In reality, I can't for the life of me get it to work.
Quote:
Originally Posted by justagruvn
Again, I will ask... How to I set a pointer from one class object to a different class object? Its really making me bonkers. I feel like Im taking crazy pills here.
Conceptually, I'm not sure, but I think you just need to create an instance of Model in the root's header file, like
@class Model
// ...Some code
Model *model
and then in the implementation of the root, have something like:
// RootViewController.m
#import "RootViewController.h"
// #import "SecondViewController.h"
@implementation RootViewController
@synthesize display;
#pragma mark -
#pragma mark Custom Methods
- (IBAction)pushViewController {
SecondViewController *vc = [[SecondViewController alloc] init];
[self.navigationController pushViewController:vc animated:YES];
}
- (void)updateLabelWithString:(NSString *)buttonPressed {
self.display.text = buttonPressed;
}
#pragma mark -
#pragma mark View lifecycle
- (void)viewDidLoad {
self.title = @"Root View";
[super viewDidLoad];
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
}
#pragma mark -
#pragma mark Memory management
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Relinquish ownership any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
// For example: self.myOutlet = nil;
self.display = nil;
}
- (void)dealloc {
[display release];
[super dealloc];
}
@end