Transferring Data in View Controllers - iphone

I would like to transfer some data, for example an integer, from ViewController1 to ViewController2. How would I do that? In other words, how would I be able to access the information from ViewController1 in ViewController2? Please specify all the required code. Thanks!!

When you push to the second view controller you will use some code like this...
MyViewController *vc2 = [[MyViewController alloc] initWithNibName:#"MyViewController" bundle:nil];
[self.navigationController pushViewController:vc2];
in order to pass information to it you need to set up a property in MyViewController.h file like this...
#property NSString *someNameProperty;
Then when you push do this...
MyViewController *vc2 = [[MyViewController alloc] initWithNibName:#"MyViewController" bundle:nil];
vc2.someNameProperty = #"This is being passed to view controller 2";
[self.navigationController pushViewController:vc2];
Then in vc2 you can access the same property self.someNameProperty and it will contain the value you passed in from vc1.
Hope this helps.

Related

Get String From Parent ViewController?

I need to get an NSString from my parent view controller to its child view.
So I have 'ParentView' ----> 'ChildView'
And I need to get the string from ParentView to ChildView. I have tried adding a method which returns a string in my ParentView and calling it like so in the ChildView with no luck.
Doesn't work:
ViewController *vc = [[ViewController alloc] init];
startDateLbl.text = [vc string];
Any help as to how to achieve this would be appreciated, thanks.
The easiest way would be to set a property for your string in the child view.
#property (nonatomic, retain) NSString *childString;
Then pass your string from the parent view to the child view before or after you push the view onto the stack.
ViewController *vc = [[ViewController alloc] initWithNibName:#"ViewController"];
vc.childString = parentString;
[self.navigationController pushViewController:vc animated:YES]; // this assumes navController
This line ViewController *vc = [[ViewController alloc] init]; will create a new object.
Just giving you a simple code hope you get it
ChildViewController *viewController = [[ChildViewController alloc] initWithNibName:#"ChildViewController" bundle:nil];
[self.navigationController pushViewController:viewController animated:YES];
//here set your label, make sure startDateLbl has getter property
viewController.startDateLbl.text = [vc string];
Depending on the UIViewController type you may have access to the parent View Controller.
self.parentViewController.someString (see UIViewController documentation).

Is there a way to pass data from one view to modalviewcontroller

I have a view with a UIView and a button. Pressing the button brings up a ModalViewController which is UIView with UITextField in it. I need to pass the string value of NSObject in the UIView to the ModalViewController as a string. On button click I call this:
-(void) editNote{
TextViewController * vc = [(TextViewController *)[TextViewController alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
[self.navigationController presentModalViewController:nav animated:YES];
[vc release];
}
Normally, on row select we push the data using pushviewcontroller to the next view but after doing lot of google I cannot figure out how to pass the data using presentmodalviewcontroller.
You could add a property to TextViewController, then just use vc.object = myObject. Or you could make a new init method that takes some more information.

UINavigationController: push self?

I have a custom class which is a UITableViewController, this is inside of a UINavigationController. Normally, when I click a cell, I push a new class onto the stack and it is fine. This time, I would like to push self onto the stack (passing a different string onLoad so that I load different content), so that I can reuse my code, is this possible? Or do I always have to create a secondary class to push?
Rather than "pushing yourself", you can push a new instance of the same view controller yes. Simply create a new one like so:
MyViewController *viewController = [[MyViewController alloc] initWithNibName:"MyView"];
viewController.customString = #"Something else";
[self.navigationController pushViewController:viewController];
[viewController release];
I haven't tested this, and it's late so there might be bits wrong but you should be ok with that. Let me know if it works!
My solution:
MyViewController *viewController = [[MyViewController alloc] initWithNibName:"MyView"];
viewController.customString = #"Something else";
[self.navigationController pushViewController:viewController];

How do I pushViewController/etc. from a UIViewController subclass?

I've been attempting to figure this out for a while now, but I'm up to a point where I can't seem to solve my problem from reading other Q&As. I'm trying to get the active UIViewController in a UINavigationController to send popViewController/pushViewController messages to the UINavigationController, but I cannot figure it out. I'm probably doing something rather stupid that is causing it to break. The structure should be like this, but even then I'm not sure if I've done that right.
mainController
primaryNavigationController
firstViewController
secondViewController
both firstViewController and secondViewController are a subclass
mainController.m
firstViewController = [[FirstTestViewController alloc] init];
secondViewController = [[FirstTestViewController alloc] init];
primaryNavigationController = [[UINavigationController alloc]
initWithRootViewController:firstViewController];
[primaryNavigationController.view setFrame:CGRectMake(0,0,320i,409)];
[self.view addSubview:[primaryNavigationController view]];
[primaryNavigationController.navigationBar setFrame:CGRectMake(0,0,20,44)];
primaryNavigationController.navigationBar.tintColor = [UIColor blackColor];
How can I tell primaryNavigationController to push/pop a VC from within the firstTestViewController subclass?
You would allocate the second view controller within your first view controller (because you don't need it before):
secondViewController = [[FirstTestViewController alloc] init];
[self.navigationController pushViewController:secondViewController animated:YES];
[secondViewController release];
The SDK includes many sample projects that involve a navigation controller and show you how to do this.

How can i call the same ViewController from different TabBar items?

I've developed a ViewController that shows different data according to the input parameter. I would like to use a tabBar interface and call the same ViewController from different tabs by passing them to different parameters.
Can I do this? I get errors if I specify the ViewController's NIB in tabBar item.
Can you help me please?
Thanks in advance.
Create two different instances of your ViewController:
MyViewController *vc1 = [[MyViewController alloc] initWithNib:#"MyViewController" bundle:nil];
MyViewController *vc2 = [[MyViewController alloc] initWithNib:#"MyViewController" bundle:nil];
UITabBarController *tabs = [[UITabBarController alloc] init];
[tabs setViewControllers:[NSArray arrayWithObjects:vc1, vc2, nil] animated:NO];