Get String From Parent ViewController? - iphone

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).

Related

Set Title with Modal View doesn´t work

I initiate the following Modal Controller:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UIViewController *modal = [storyboard instantiateViewControllerWithIdentifier:#"modalController"];
modal.title = #"Example Title";
[self presentModalViewController:modal animated:YES];
I set the title with:
modal.title = #"Example Title";
but this doesn´t work, can anyone help me?
Edit:
I have wrapped my ModalView with a UINavigationController like this
You are going about this in a slightly convoluted way... but to stay with your paradigm, you need to present the navigationController, not the contained viewController: trying to do it the latter way will instantiate the viewController, but this action will not pull the containing navController along with it out of the storyboard. You are setting the viewController's title property ok, but you have no (automated) way to display the title. Whereas if you instantiate the navController, it's contained viewController does get unarchived along with it as it's topViewController.
//give the navigation controller a storyboard id eg "navVC"
UINavigationController* modalNav = [self.storyboard instantiateViewControllerWithIdentifier:#"navVC"];
[modalNav topViewController].title = #"Example Title";
//[self presentModalViewController:modalNav animated:YES];
//deprecated method, use this instead:
[self presentViewController:modalNavController
animated:YES
completion:nil];
You have to add a IBOutlet property for UINavigationItem (the title) in your model controller with a classic drag & drop method.
#property (weak, nonatomic) IBOutlet UINavigationItem *navTitle;
Then set the title in viewDidLoad function.
- (void)viewDidLoad
{
[super viewDidLoad];
// title view
[self.navTitle setTitle: #"atitle"];
}

Transferring Data in View Controllers

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.

UITabBarController to load subviews

I am kind of stuck and would appreciate any ideas on what I did wrong.
I created programmatically a NSObject which holds a UITabBarController to which three ViewControllers are added:
UITabBarController tabBarController = [[UITabBarController alloc] init];
ControllerOne = [[OneViewController alloc] initWithNibName:#"OneView" bundle:nil];
ControllerTwo = [[TwoViewController alloc]initWithNibName:#"TwoView" bundle:nil];
ControllerThree = [[ThreeViewController alloc] initWithNibName:#"ThreeView" bundle:nil];
NSMutableArray *viewControllers = [[NSMutableArray alloc] initWithCapacity:3];
[viewControllers addObject:ControllerOne];
[viewControllers addObject:ControllerTwo];
[viewControllers addObject:ControllerThree];
[tabBarController setViewControllers:viewControllers];'
I now display the tabBarController's view
viewController.modalTransitionStyle = transitionStyle;
[self presentModalViewController:viewController animated:YES];
with viewController being the just created tabBarController.
The view changes fine, displaying the tabbar correctly (Icons and titles) but fails to show e.g. OneViewController's view. I assume that the view is not loaded since the - (void)viewDidLoad is not being called for any of the subview controllers.
I would appreciate any suggestions.
Thanks,
equi
Are you sure your nib names are how you have typed them? It's not called OneViewController.xib?, for example?
I sorted out the issue.
The above code actually worked, reason for the view not appearing was that I accidentally synthesised 'view' in the UIViewController derivative.

how do I pass data to a child UINavigationController which is presented modally (i.e. via initWithRootViewController)

How do I pass data to a child UINavigationController which is presented modally via "[[UINavigationController alloc] initWithRootViewController:newItemController];"?
That is the way with this method of creating the child controller (i.e. newItemController in this case), it is initialised via the UINavigationController initWithRootViewController method, hence there doesn't seem to be the ability to call a custom newItemController init method here? Nor have access to the newItemController instance itself to call a custom "setMyData" type method?
NewItemController *newItemController = [NewItemController alloc];
newItemController.delegate = self;
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:newItemController];
[self.navigationController presentModalViewController:navController animated:YES];
Code in your question is missing the init called to NewItemController.
For example:
NewItemController *newItemController = [[NewItemController alloc] init];
Now, when you created NewItemController you can create your own init:
-(id)initWithStuff:(NSString *)example {
self = [super init];
if (self) {
// do something with the example data
}
return self;
}
or you can add property to the NewItemController class
// header file
#property (nonatomic, copy) NSString *example;
// .m file
#synthesize example;
// when you create the object
NewItemController *item = [[NewItemController alloc] init];
item.example = #"example string data";
The key is that you don't pass the data to the navigation controller, you pass it to the navigation controller's root view controller.

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.