how to access superviewcontroller from its subviewcontroller in iphone - iphone

I have a viewcontroller and i am adding a subviewcontroller in that. Now i want to set value for a string from subviewcontroller which is in its superviewcontroller.
Please Suggest some idea
Thank You

if you want to access superview use the statement said by Jhaliya,
If you want to access superviewcontroller property frm subviewcontrller property ,
use
id mainViewController = [self.view.superview nextResponder];
here mainViewController is the superviewcontroller reference.
There a parentViewController property in UIViewController class reference,
This property is used for navigation, tab bar, and modal view controller hierarchies.
The above syntax is used for when you are setting a view as subview to a parentviewcontroller

You can access the super view controller using the parentViewController property on every UIViewController.
UIViewController *parent = self.parentViewController;

You can use superView method of UIView to access its parrent.
UIView *mySuperView = [MySubView superView];

use superview property of UIView.
UIMyView* mySuperView = (UIMyView*) myView.superview ;
mySuperView.mytextString = #"Assign string to super view";
EDITED:
You could get the list of all your UIViewController by using methods of UINaigationController .
Also current visible controller and top view controller,
Read the documentation for UINavigationController .

Related

How UIActivityController is presented?

I am trying to present a custom size UIViewController on top of UITableViewController without dismissing the UITableViewController but I could not. All I could do was to add the view of the UIViewController as subview.
The UIActivityController seems to be working differently when it is presented. The presenting view controller is not set to nil.
How can I present a custom size UIViewController -as the UIActivityController- without dismissing the presenting view controller ?
Thank you
Here's something you can do:
ActivityViewController *activity = [[ActivityViewController alloc]init];
[self addChildViewController:activity];
[self.view addSubview:activity.view];
Here, ActivityViewController is your custom UIViewController and inside its viewDidLoad or so, you can set its frame to the height you like. Or you can have a method which can be used to vary the height according to the number of buttons you have inside that UIView

Calling method from one Viewcontroller inside another

Noob question fellas, but I cant get it.
I've got a View Controller that loads in a separate View Controller. I would like to be able on a button press to call a method inside the parent View controller. So here is what i've got
parent VC:
.h
-(void)callParentMethod;
.m
-(void)viewDidLoad{
self.childVC.parentVC = self;
}
-(void)callParentMethod{
NSLog(#"Hello?");
}
child VC:
.h
#import "TheParentViewController.h"
#property (nonatomic, weak) TheParentViewController *parentVC;
.m
-(void)addThis{
[self.parentVC callParentMethod];
}
I get no errors, the child VC method addThis seems to call the method, but the NSLog is never called. Any thoughts what i'm doing wrong?
I think parentVC releases because of weak reference. Try to use this method.
-(void)addThis{
NSLog(#"%#", self.parentVC);
[self.parentVC callParentMethod];
}
YourParentVC *parent = (YourParentVC*)[self presentingViewController];
parent.whatever = blah;
I am not quite sure what your application is, but you should not have to keep a reference to the parent view controller. A UIViewController already has a property called parentViewController which you can use in the following way:
[self.parentViewController methodToCall];
This will call a method on the parent view controller. You may need to cast the object in order to call custom methods
[(TheParentController *)self.parentViewController methodToCall];
This of course assumes that the child view controller's view is a subview of the parent view controller view. Hope this helps
Check whether your parent view controller is allocated.
parentnvc = [ParentViewController alloc]initWithNibName:#"somenib" bundle:nil];
if parentvc is allocated and initialised and call the method
You can create a property in child view controller say,
#class ParentVC;
#property(weak,nonatomic)ParentVC *parentVCreference;
and get the reference for the Parent view controller as follows
self.parentVCreference=(ParentVC*)self.parentViewController;
then you can call any exposed methods from ParentVC as follows
[self.parentVCreference parentVCMethod];
Note: You need to import header of ParentVC in ChildVC implementation file.

Accessing a uinavigationbar from subclass?

I have a subclass based on UIView and i want to change the tint of the uinavigationbar but various methods I've tried haven't worked. How do i do this?
As long as the view is in a viewController that's part of the navigation stack you can do:
viewController.navigationController.navigationBar.tintColor = [UIColor blackColor];
To access the viewController, you can declare an id as an ivar in your View class:
id myParentViewController;
Declare this id as a property so it can be accessed from outside the View.
When creating the view from the viewController, you could pass a reference to the viewController into the view by doing:
view.myParentViewController = self;
Where self would be the reference to your viewController on the navigation stack.
Now that you're in the view, you can access the navigation bar by doing this:
if([myParentViewController isKindOfClass:[UIViewController class]])
{
UIViewController *theParentViewController = (UIViewController*)myParentViewController;
theParentViewController.navigationController.navigationBar.tintColor = [UIColor blackColor];
}
You can access UINavigationBar from UINavigationController. One method is pass the UINavigationController or UINavigationBar as a parameter to the UIView. Or the UIView can use delegate to notify the outer UIViewController to do works.

Calling method in parent UIViewController, after adding by addSubview

I have a UIViewController that is creating another view controller, and adding its view as a subview:
In the parent UIViewController:
SlateMoreView* subView = [[SlateMoreView alloc] initWithNibName:#"SlateMoreView" bundle:nil];
[self.view addSubview:subView.view];
I then need to call a method from the subview, in the parent view.
I have seen how to do this when I am adding the sub UIViewController using [self.navigationController pushViewController: subView animated: YES], because I can find the parent using this kind of code:
In the sub view UIViewController:
NSArray* viewControllerArray = [self.navigationController viewControllers]
int parentViewControllerIndex = [viewControllerArray count] - 2;
SlateView* slateView = [viewControllerArray objectAtIndex:parentViewControllerIndex];
...and then I can send messages to it. But since I added the sub view manually by using addSubView, I can't do this.
Can anyone think of how I can talk to my parent UIViewController?
Thanks!
UIViews have a superview property which seems to be what you are looking for.
In addition you probably don't want to nest UIViewController's view like that unless you are very deliberately building a custom contain view controller. See http://blog.carbonfive.com/2011/03/09/abusing-uiviewcontrollers/
You might want to consider if your problem can be solved by using NSNotifications. You could post a notification from your subview when an event happens that interested listeners (your superview) need to know about . When the superview receives the notification, it can run whatever code you wish. All the while the subview never needs to know about the superview.
This is one way to make your classes less dependant on each other.
You could also use delegation as another option.
When you add your view as a subview to a view hierarchy, you put it in the responder chain. You can go up the responder chain to reach the view controller as a UIView controlled by a UIViewController has the UIViewController as its nextResponder.
id object = theSubview;
do {
object = [object nextResponder];
} while ( ![object isMemberOfClass:[YourViewController class]] );
// object has the view controller you need.

Presenting Modal TableView

I've a tableviewController in a class which I need to present modally from another class. In the modaltableview class, I'm creating tableview in viewDidLoad and apart from this there are tableView delegate methods in this class.
My question is how do I present this class object to show this tableViewController modally?
I've presented the viewController from same class as:
UIViewController *vw = [[UIViewController alloc]init];
[self presentModalViewController:vw];
But what to write for a viewController from another class? Shall I call presentModalViewController from this class or the modaltableViewControler class?
P.S. I'm having a modalViewController already on the current viewController where I've to present the new modalTableView.
Thanx in advance.
In the visible view controller, create and initialise an instance of your modal tableViewController. Then, still in the visible view controller, call [self presentModalViewController:<THE TABLE VIEW CONTROLLER YOU JUST INITIALISED> animated:YES];
Hope this helps!
-jrtc27