passing data to secondViewController - iphone

Trying to send some data form my previous ViewController. To be more clear trying to send the selected date of the Si-Calendar to my secondView.
I'm adding the Calendar to my view in this way:
CalendarMonth *aCalendarView = [[CalendarMonth alloc] initWithFrame:CGRectMake(0, 0, 320, 324) logic:calendarLogic];
[aCalendarView selectButtonForDate:selectedDate];
[self.view addSubview:aCalendarView];
How do I use selected date and send it to my secondViewController (a UIView in which I'll display the selected date)?

Add a date property to your secondViewController, and set it appropriately when you create the instance...

It is a bit unclear what the first or second viewController is, but in general you could use a delegate for this purpose. For an example on how to do this, take a look at this answer

There are many ways to exchange data between view controllers, just take them as objects.
first controller hold a ref of the second controller, and transfer data using variables and methods
use delegate,
use notification
etc.

i can see that you are adding the CalenderMonth as the subview to another view. So if you dont release it (until u create the second view controller), you can set the selectedDate of the calender month obj to an iVar of second view controller. Else Create a delegate for CalenderMonth class. Set the second view controller as the delegate of the CalenderMonth and do the necessary.

Related

Is there a way to avoid viewDidLoad beeing called after every segue?

i initialize tables, data etc in my main ViewController. For more settings, i want to call another Viewcontroller with:
DateChangeController *theController = [self.storyboard instantiateViewControllerWithIdentifier:#"dateChangeController"];
[self presentViewController:theController animated:YES completion:^{NSLog(#"View controller presented.");}];
And some clicks later i return with a segue (custom:)
NSLog(#"Scroll to Ticket");
[self.sourceViewController presentModalViewController:self.destinationViewController animated:YES];
My Problem:
After returning to my main ViewController, viewDidLoad is called (everytime).I read, that the ARC releasing my mainView after "going" to the other ViewController and calling the viewDidUnload Method, but i want to keep all my data and tables i initialize at the beginning..
Any solution? Thank you very much!
The problem is that you are doing this:
main view controller ->
date change controller ->
a *different* main view controller
In other words, although in your verbal description you use the words "returning to my main ViewController", you are not in fact returning; you are moving forward to yet another instance of this main view controller every time, piling up all these view controllers on top of one another.
The way to return to an existing view controller is not to make a segue but to return! The return from presentViewController is dismissViewController. You do not use a segue for that; you just call dismissViewController. (Okay, in iOS 6 you can in fact use a segue, but it is a very special and rather complicated kind of segue called an Unwind or Exit segue.)
If you do that, you'll be back at your old view controller, which was sitting there all along waiting for your return, and viewDidLoad will not be called.
So, this was a good question for you to ask, because the double call of viewDidLoad was a sign to you that your architecture was all wrong.
I think you're taking the wrong approach - viewDidLoad is supposed to be called when it is called - it's a notification to you that the view is being refreshed or initially loaded. What you want to do is move that table initialization code somewhere else, or, at least, set a Boolean variable so that it is only called once. Would it work to create an object that has your table data when viewDidLoad is first called, then to check it to see if it's already been called?

How to modify UITextField in FirstViewController from SecondViewController?

So I am fairly new to objective C and iOS programming. I am learning as I program and have a question. I am trying to modify a UITextField in my FirstViewController with my Second. How do I do this?
I have a UITextField name in FirstViewController and would like to be able to do something like firstViewController.name.text = #"new name"; inside of SecondViewController with a button and have the value changed so that if I switch tabs back to the FirstViewController I see the change has been made.
Thanks in advance for any help.
1、Set the FirstViewController as the delegate of SecondViewController;
2、Use NSNotificationCenter to notify the change event.
Well to call
firstViewController.name.text = #"new name"
You just need a reference to your first view controller inside of your second view controller. There are a number of ways to pass a reference around. Could go through the app delegate.
Another option that doesn't require you to have a reference to the first view controller inside the second view controller is to use NSNotificationCenter
Then you can have your second view controller observe a notification named say #"namechange" which contains the new name string.
When the button is pressed, you post a notification to the NSNotificationCenter with the new name attached. All the info is in the link above, or just google NSNotificationCenter for more tutorials and info.

Passing data from a subview to a parent view with protocols and delegates

I have been using Protocols and Delegates for a couple of weeks now and have gotten used to passing data from a subview to its parent view no problem.
However I am now trying to pass some data with protocols and delegates from a sub subview to its parent view on the navigation stack.
i.e.
view 0 - to here, missing view 1
-- view 1
--- view 3 - pass from here
however the problem being is setting up that parent view as a delegate I cannot seem to find a way to do it. I have set it like this
SubResultViewController *subResultViewController = [[SubResultViewController alloc] init];
[subResultViewController setDelegate:self];
Then I set everything up in that subResultViewController I dont get any errors when I execute all the code how ever when I put a break point into the receiving method of the delegate in the main view where I am passing everything to it just never gets accessed..
I am hoping I can get some help with using protocols and delegates in this way (over multiple views on the navigational stack)
any help would be greatly appreciated.
The problem with passing delegates from views is that the viewController has already been loaded once, so doing this:
SubResultViewController *subResultViewController = [[SubResultViewController alloc] init];
[subResultViewController setDelegate:self];
is initializing a new SubResultViewController object, not the one already loaded(initialized).
You have to somehow pass the receiving viewController to the view that you want to send the message from.This is where NSNotification's come in handy. You can set your receiving view controller as a listener(observer) for the notification, then anytime a notification is posted with the same "name", any thing listening will get the message.Once I learned how to use the NotificationCenter It change how I was able to communicate between views/classes.
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSNotificationCenter_Class/Reference/Reference.html

Passing data from UIViewController to 1 back UIViewController

So I have this UINavigationController, I'm on the first moving to the next view, than I want to hit the 'back' button and to go back to the first view with the data that I saved into 'strAddress' on the second view. I want to present the data on the first view on 'lblShowStr.text'.
how can I manage to do that? I've searched all the web, found some people that wrote, but couldn't understand what they have been told there.
Thanks!
You can get a reference to the previous viewController in your navigation stack by saying:
NSArray *viewControllers = self.navigationController.viewControllers;
MyViewControllerClass *previousController = [viewControllers objectAtIndex:[viewControllers count] - 2];
You can then set a property on the 'previous' view controller to store your text, or even set the label outlet's text directly like this:
previousController.lblShowStr.text = self.strAddress;
It's not the best way to do it (the best way involves creating a custom delegate protocol or using NSNotificationCenter) but it's the easiest way.
In your first view controller you might have an NSString property called strAddress.
and you put that string into lblShowStr.text every time the view appears.
In your second view controller you might have a property pointing to an instance of view controller one. When you instantiate your second view controller you could assign the property on it to the first view controller.
secondViewController.firstViewController = self;
or
[secondViewController setFirstViewController:self];
Then when the user presses the back button viewDidAppear would get called for the first view and update the string.
I am assuming you don't want to store this data anywhere else e.g. in your model or nsuserdefaults etc.

Trying to update UILabel with contents of UITextField on different view controller

I have two view controllers, one is MainViewController, the other is SetupViewController. I want a UILabel on MainViewController to set the text to the contents of a UITextField from the SetupViewController when a button is pressed in the SetupViewController.
In SetupViewController, I have this in the IBAction:
- (IBAction)donePressed:(id)sender {
MainViewController *mvc = [[MainViewController alloc] init];
[mvc.testLabelOnMVC setText:testTextFieldOnSVC.text];
[release mvc];
}
testLabelOnMVC (and testTextFieldOnSCV, with respective terms) is
#property (nonatomic, retain) UILabel *testLabelOnMVC;
and is also synthesized.
Every time I try, it doesn't work. Nothing happens, nothing changes. I have no errors or warnings. Can anyone help me out?
The view of your MainViewController does not exist until you reference the MainViewController's view property (which forces viewDidLoad to execute). You must reference the view (or otherwise force the view to be constructed) before you attempt to modify any UI objects in the MainViewController.
You are allocating a new MainViewController when you press the button, then you are setting the text of the label on this new controller, not on the MainViewController that your app is showing.
To fix this, create either and IBOutlet or iVar that points to the original MainViewController and set the text on that instead.
Easiest way is to create a #property in the main view controller and write the text in there. Then just read it in the second MVC's viewDidLoad.
The only views that MainViewController should worry about are the ones that it owns; it shouldn't be trying to access the view hierarchy managed by SetupViewController. Likewise, SetupViewController should not directly modify views in MainViewController's view graph.
The right way to do what you're asking is for the two controllers to talk to each other, either directly or via the data model. For example, let's say that your MainViewController instantiates SetupViewController. If that's the case, it'd be natural for mvc to set itself as svc's delegate, so that svc sends it a messages like -setupController:didUpdateTestStringTo:. MainViewController's implementation of that method could then save the new test string somewhere and update it's testLabel field.
Another example: MainViewController instantiates SetupViewController. SetupViewController contains a field where the user can enter a new value for the test string. Before exiting, SetupViewController writes the contents of that field into NSUserDefaults or some other common data storage. When control returns to MainViewController, that object reads the shared data and updates itself as necessary, including setting the new value for testLabel.
There are other variations on the same theme, but the common thread here is that neither view controller has to directly access views that it doesn't own.
You can change the text of the label if the view is already loaded. Instead of initializing the viewcontroller, retrieve it from the view stack if you are using navigation controller.
I dont know if your viewController is already loaded or not.