how to call a methode in the superview from the subview? - iphone

I had a view controller which is initialised in appdelegate.I was added that viewcontroller to the navigation controller with addsubview methode,inorder to show it above the navigation bar.Now i am adding another viewcontrollers view as the subview of this view.Now when i am pressing a button on the subview i need to perform some methodes in the superview.I tried this way`
appdelegate.viewcontroller =(ViewController*)self.view.superview;
}
if(appdelegate.viewcontroller==nil)
{
NSLog(#"appdelegate viewcontroller nil");
}
else
{
[appdelegate.viewcontroller setmessage:#""];
}
`But it is giving me error like
[UIView setmessage:]: unrecognized selector sent to instance.Can anybody point me in where i am going wrong?

for (UIView* next = [self.view superview]; next; next = next.superview)
{
UIResponder* nextResponder = [next nextResponder];
if ([nextResponder isKindOfClass:[UIViewController class]])
{
[(UIViewController*)nextResponder viewWillAppear:YES];
}
}

Related

isBeingDismissed not set in viewWillDisappear:

I have some code to clean up in my viewWillDisappear:, which I only want to use when the view is moving back to the parent view controller.
- (void)viewWillDisappear:(BOOL)animated
{
if ([self isMovingFromParentViewController] || [self isBeingDismissed]) {
NSLog(#"isMovingFromParentViewController or isBeingDismissed");
// clean up
}
[super viewWillDisappear:animated];
}
The view can be presented in two ways: it can be pushed by a navigation controller, or presented as a modal view controller (from the same navigation controller). If it's pushed, then popped (pressing the back button), my clean-up code runs. If it it presented as a modal view controller, then dismissed, the code doesn't run.
I dismiss like so:
[rootViewController dismissModalViewControllerAnimated:YES];
My question is: why isn't isBeingDismissed set when I dismiss my view controller?
If this is the first view controller in a modal navigation controller that's being dismissed, calling self.isBeingDimissed() from viewWillDisappear: returns false.
However, since the entire navigation controller is being dismissed, what actually works is self.navigationController?.isBeingDismissed(), which returns true.
As #Yuval Tal mentioned, this flag does not work when you're dismissing controller that is embeded inside navigation controller. Here's an extension that I use:
extension UIViewController
{
var isAboutToClose: Bool {
return self.isBeingDismissed ||
self.isMovingFromParent ||
self.navigationController?.isBeingDismissed ?? false
}
}
It can be easily extended when you find another case when standard .isBeingDismissed won't work. And if you find, let us, let me know in comments.
Your issue is how you are dismissing your modal view. How is rootViewController being defined?
When I call [self dismissModalViewControllerAnimated:YES] then [self isBeingDismissed] evaluates to true.
When I call [parentViewController dismissModalViewControllerAnimated:YES] then [self isBeingDismissed] evaluates to true, whereby parentViewController is the UIViewController that presented the modal view (note: not a UINavigationController).
If by some chance you came here trying to use isBeingDismissed on a non-modally presented view controller, you can always check the topViewController property of your navigationController, for instance:
if navigationController?.topViewController != self {
return
}
viewController.isBeingPresented == NO;
[rootVC presentViewController:viewController animated:NO completion:^{
viewController.isBeingPresented == NO;
viewController.isBeingDismissed == NO;
[viewController dismissViewControllerAnimated:NO completion:^{
viewController.isBeingDismissed == NO;
}];
viewController.isBeingDismissed == NO; // is not work
}];
viewController.isBeingPresented == YES; // is work
viewController.isBeingPresented == NO;
[rootVC presentViewController:viewController animated:NO completion:^{
viewController.isBeingPresented == NO;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
viewController.isBeingDismissed == NO;
[viewController dismissViewControllerAnimated:NO completion:^{
viewController.isBeingDismissed == NO;
}];
viewController.isBeingDismissed == YES; // is work
});
}];
viewController.isBeingPresented == YES; // is work

Handling UITapGestureRecognizer in custom view

I have a custom view class. Inside my view controller I add a Tap gesture recognizer on this view object. Now, in the handler of tap gesture I am setting up a property on my view object which I am trying to fetch in the drawRect of my view class. Now, surprisingly when I print view objects in my "handleGesture" & "drawRect", I get two different objects. Because of this my if condition inside drawRect do not get execute. What could be the reason?
It do not come in state UIGestureRecognizerStateBegan. It always coming inside UIGestureRecognizerStateEnded.
// Adding Gesture in my view
MyCustomView *customView= [[[MyCustomView alloc] init] autorelease];
UIGestureRecognizer *GestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleGesture:)];
[customView addGestureRecognizer:GestureRecognizer];
[GestureRecognizer release];
// Handling tap on my view
- (void)handleGesture:(UIGestureRecognizer *)GestureRecognizer; {
MyCustomView *aView= (MyCustomView *)GestureRecognizer.view;
switch (iGestureRecognizer.state) {
case UIGestureRecognizerStateBegan:
NSLog(#"Began");
[aView setNeedsDisplay];
aView.touchDown = YES;
break;
case UIGestureRecognizerStateEnded:
NSLog(#"Ended");
aView.touchDown = NO;
[aView setNeedsDisplay];
break;
default:
break;
}
}
// Inside my view class
- (void)drawRect:(CGRect)iRect {
if (self.touchDown) {
// Do something here
}
}
There is nothing calling the drawRect method. You don't want to do this directly, but in your handleGesture method, you could put in a call to [aView setNeedsDisplay] and your view's drawRect will get called in the next drawing cycle.

Activity Indicator not starting animation straight away

I initialize an activity indicator and in a button press action I start it animating and call the next view to display.
-(IBAction) downloadButtonPressed:(id)sender {
NSLog(#"Download Button Pressed");
indicator.hidden = NO;
[indicator startAnimating];
if (addviewcontroller == nil)
addviewcontroller = [[AddViewController alloc]init];
[self.view addSubview:addviewcontroller.view];
[addviewcontroller setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[self presentModalViewController:addviewcontroller animated:YES];
}
When I press the button, the activity indicator doesn't start immediately. It starts when the other view is called. The indicator is displayed for a second, but when the button is pressed it takes some time to load the other view.
I dont know why the indicator shows for a second without starting.
Try this :
-(IBAction) downloadButtonPressed:(id)sender
{
NSLog(#"Download Button Pressed");
indicator.hidden = NO;
[indicator startAnimating];
[self performSelector:#selector(showController) withObject:nil afterDelay:0.1f];
}
- (void)showController {
if (addviewcontroller == nil)
addviewcontroller = [[AddViewController alloc]init];
[self.view addSubview:addviewcontroller.view];
[addviewcontroller setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[self presentModalViewController:addviewcontroller animated:YES];
}
That should do the trick ;-)
EDIT
I just noticed that there is a problem in your code, you are adding your addviewcontroller twice. One by adding it as a subview of the actual view controller, and one by modally presenting another view controller. You should remove one of the statements from this function.

Remove Unused ViewControllers

When I click in the TTTabBar, the tabs elected sends me to one the the correct ViewController, then I click another TTTabBar it will send me to the other one.....etc, but how can I remove the last ViewController, so they don't just keep stacking on top of each other.
- (void)tabBar:(TTTabBar*)tabBar tabSelected:(NSInteger)selectedIndex
{
if(selectedIndex == 0){
UIViewController* viewController = (UIViewController*)[[TTNavigator navigator] viewControllerForURL:#"tt://Forum"];
[self.view addSubview:viewController.view];
[self.view addSubview:_tabBar];
}else if(selectedIndex == 1) {
UIViewController* viewController = (UIViewController*)[[TTNavigator navigator] viewControllerForURL:#"tt://Profile"];
[self.view addSubview:viewController.view];
[self.view addSubview:_tabBar];
}else if(selectedIndex == 2) {
UIViewController* viewController = (UIViewController*)[[TTNavigator navigator] viewControllerForURL:#"tt://PMs"];
[self.view addSubview:viewController.view];
[self.view addSubview:_tabBar];
}else if(selectedIndex == 3) {
UIViewController* viewController = (UIViewController*)[[TTNavigator navigator] viewControllerForURL:#"tt://Friends"];
[self.view addSubview:viewController.view];
[self.view addSubview:_tabBar];
}
}
I got this to work
if ([viewController isKindOfClass:[UIViewController class]]) {
[viewController.view removeFromSuperview];
} else {
}
when i added it above
if(selectedIndex == 0){
Use:
- (void)removeFromSuperview
To do that in your scenario you'll have to keep a reference to the current view around or create a way of getting a reference to it using a tag.
http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIView_Class/UIView/UIView.html
Is TTNavigator a class you wrote? It looks like a singleton manager class to me... Is viewControllerForUrl returning an autoreleased instance of UIViewController? You could implement a stack in the TTNavigator where you would hold the references to all your view controllers and then popping them as you see fit.

Hiding UITableView searchBar leaves a blank space

I have a UIViewController with a standard UITableView and Search bar with Search delegate added. The view has a segmented control in the navigation bar, when the user changes the segmented control I would like to hide the searchBar.
The code I am using is:
- (void)segChange {
if ([segmentedControl selectedSegmentIndex] == 0) {
[[[self searchDisplayController] searchBar] setHidden:YES];
// This does not work:
[[[self searchDisplayController] searchResultsTableView] setContentOffset:CGPointZero animated:NO];
}
else {
[[[self searchDisplayController] searchBar] setHidden:NO];
}
}
The code hides the searchBar fine, but it also leaves a nasty white space at the top of the table view.... any ideas on how to get rid of it???
Thanks
This code solved the problem:
- (void)segChange {
if ([segmentedControl selectedSegmentIndex] == 0) {
[self.myTableView setTableHeaderView:nil];
}
else {
[self.myTableView setTableHeaderView:[[self searchDisplayController] searchBar]];
}
}
Rather than hiding the segmented control try setting its frame to CGRectZero