viewDidDisapper won't call - iphone

I have uinavigation controller with a view controller named VC1,now i have a button in VC1 that when i click on him i go to view controller VC2,this is how i bring VC2 to screen :
VC2 *tmp = [[VC2 alloc]init];
[[self navigationController] pushViewController:tmp animated:YES];
[tmp release];
now when i click in VC2 on the back button of the navigation to return to VC1 its work but i put in VC2 viewDidDisappear and viewWillDisappear methods,and when i click on the back button this function won't called, any one have any idea?

You may call the view[..] methods manually from the UINavigationControllerDelegate callbacks, however the easiest way to ensure that the methods are called by the super implementation of the UINavigationController is just to call them manually once when the UINavigationController is allocated.
See my answer here: iPhone viewWillAppear not firing
So immediately after you have called navController = [[UINavigationController alloc] init.., make sure to call
[navController viewWillAppear:NO];
[navController viewDidAppear:NO];
[navController viewWillDisappear:NO];
[navController viewDidDisappear:NO];
This should ensure events are correctly forwarded to each view controller in the future.

If you want to call the viewWillDisappear/viewDidDisappear methods, your view controller has to do that manually before popping itself off the nav stack. Have a look at this
,you might get an idea how to do it.

Related

How can I hide a button in a different view controller?

I am using this code to hide a button in a different view controller, but the button does not get hidden when the button is pressed to hide the button in the other view controller.
This is the code I am using to hide the button in the other view controller:
[self dismissModalViewControllerAnimated:YES];
NSLog(#"Exited");
ViewController *vc = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
[vc.mainbutton1 setHidden:YES];
Why is this not working?
Thanks!
take a BOOL variable in ViewController controller and make the property and synthesize also.
and do this.
ViewController *vc = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
vc.check = YES;
in the view controller viewdidload
write this
if(self.check)
[mainbutton1 set hidden:YES];
The other answers should work unless...
Judging by your code I am going to guess that you are trying to hide a button on the viewController that presented the modal view?
If this is correct then what you are doing will not work as you are creating a new instance of ViewController which is not the already existing viewController you want to use.
Although the docs say that it is fine to call [self dismissModalViewControllerAnimated:YES]; from the presented modal view I tend to set up a delegate to handle the dismissal like in Apple's utitliy app template.
The reason this isn't working is because even though you have alloc'd and init'd the ViewController properly, the actual elements of that vc ViewController (including mainbutton1) have not been loaded yet.
Hitman has the right idea (and I'm voting his idea up).
Either put in a BOOL property for setting mainButton1 to hidden when the view appears, or call your [mainButton1 setHidden: YES] right after you explicitly display the view (via animation or adding subviews or whatever).
From your question it sounds like you want to hide the button in an existing view controller, whereas in your code you are creating a new one
ViewController *vc = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
[vc.mainbutton1 setHidden:YES];
Either the view controller which you observe is not the one you expect or the mainbutton1 outlet is not connected properly. You can check if the memory controller is the one you expect by logging its memory address.
NSLog(#"Hid button for view controller %p", vc);
And doing the same in the viewDidAppear callback of ViewController
NSLog(#"In viewDidAppear for view controller %p", self);
It seems you want a certain button to be hidden if something has been happening somewhere else.
You COULD, somewhat as a hack (but I don't mind that very much) control this with a variable on your AppDelegate for instance.
When the "something" is happening "somewhere else", do this:
MyAppDelegate *appDelegate = [[(MyAppDelegate *)UIApplication sharedApplication] delegate];
appDelegate.shouldHideThatOtherButtonLater = YES;
Then, when you create your new ViewController later on you could use this value to determine if your button should be visible or not like this:
ViewController *vc = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
MyAppDelegate *appDelegate = [[(MyAppDelegate *)UIApplication sharedApplication] delegate];
[vc.mainbutton1 setHidden: appDelegate.shouldHideThatOtherButtonLater ];
You will in this case have to prepare your AppDelegate for this by creating and synthesizing that shouldHideThatOtherButtonLater-property.

don't understand how to use navigation controller in iphone

I'm extremly new to iphone and I have the following misunderstanding.
All over internet the tutorials about how to use NavigationController programatically it says:
NavigationController must be declared in applicationDidFinishLaunching and must be init with a root.After that you can add views to it.
I have this:
A UIViewController class meaning(AdiViewController.h, AdiViewController.m and AdiViewController.xib) and no Delegate file meaning no applicationDidFinishLaunching method.
What I wanna do is from my class-AdiViewController when pressing a button to go to another view.
I understand that I need a NavigationController which should retain my views having the root AdiViewController.
But my problem is where should I initializate that NavigationController in viewDidAppear??...cause I don't have the Delegate files.
If you could provide a minimal example with this small issue of mine it would be great.I'm sure that for those how are senior this is nothing but still I don't get it.Thanks
NavigationController must be declared in applicationDidFinishLaunching -> this is not true.
In your AdiViewController if you have button when you push that button you want to load navigation Controller right ?
// Hook this IBAction to your button in AdiViewController
- (IBAction)pushNavController
{
AnotherViewController* rootView = [[AnotherViewController alloc] initWithNibName:#"Anotherview" bundle:nil];
UINavigationController* navController = [[UINavigationController alloc] initWithRootViewController:rootView];
[rootView release];
[self presentModalViewController:navController animated:YES];
[navController release];
}
If you are in AnotherViewController i.e., you are in root view controller of Navigation controller. You need to push and pop view controllers from there. For example if you have a button in AnotherViewController:
// push next view controller onto navigation controller's stack
- (IBAction)pushNextViewController
{
NextViewController* nextView = [[NextViewController alloc] initWithNibName:#"NextView" bundle:nil];
[self.navigationController pushViewController:nextView animated:YES];
[nextView release];
}
// Similarly if you want to go back to AnotherViewController from NextViewController you just pop that from navigation controller's stack
- (IBAction)pushNextViewController
{
[self.navigationController popViewControllerAnimated:YES];
}

modalViewController delegate callbacks - HowTo?

I have some view controller, and when I click some button I make modalView (wrapped with UINavigationController) and present it.
DscViewController *enterDescription = [[[DscViewController alloc] init] autorelease];
UINavigationController* navController = [[UINavigationController alloc] initWithRootViewController:enterDescription];
[self presentModalViewController:navController animated:YES];
[navController release];
The question is, how to make my (parent)view controller to be delegate, and when I click on some button at modalViewController (Done for example) to call some method in my parent viewController to dismiss modal and do some savings from modal input?
I don't think that it's good practice to make one controller blame its parent controller to do work that it should handel. You can always use self.parentViewController to dismiss the modal you are currently in.
If for some reason you only handle storage in the parentViewController then you can point your done button action to a method in your current viewController and then use self.parentViewController to trigger the parent method for storage.
or just set your buttons delegate directly to self.parentViewController
[button addTarget:self.parentViewController action:#selector(someMethodInParentViewController) forControlEvents:UIControlEventTouchUpInside];

How to chain modal views from a controller launched from UITabBarController

How would I go about chaining several modal controllers from a UITabBarController's view? The View Programming Guide from Apple says this is feasible but when I attempt such a task, I get the error,
"*Assertion failure in -[UIWindowController transition:fromViewController:toViewController:target:didEndSelector:], /SourceCache/UIKit_Sim/UIKit-1447.6.4/UIWindowController.m:186
The Class hierarchy is something like this:
UITabBarController -> 1 child is a UIViewController inherited class named, Tab1Controller.
Tab1Controller -> orchestrates each of the 2 controllers that need to be presented modally.
Launches 1 modal UIViewController and when this finishes up (get called via a callback), dismisses it and then initiates another modal UIViewController.
It's as if there's not enough time between the two modal controllers ending and starting.
Is there any sample code that shows how to have one modal controller after another can be chained?
See my answer to this SO question:
Correct way of showing consecutive modalViews
It's as if there's not enough time between the two modal controllers ending and starting.
I think you've hit the nail on the head there. You cannot present a new modal view controller until the previous one has finished disappearing and the viewDidAppear: method is called on the view controller that had been being covered by the old modal view.
Another option would be to present the second modal view on top of the first, e.g.
[firstModalViewController presentModalViewController:secondModalViewController animated:YES]
You can then call [firstModalViewController dismissModalViewControllerAnimated:YES] to dismiss the second (returning to the first), or [self dismissModalViewControllerAnimated:YES] to dismiss both at once.
// present modal view inside another presented modal view
FirstViewController *firstVC = [[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController: firstVC];
// Note: you can use your viewcontroller instead self.window.rootViewController
[self.window.rootViewController presentViewController:navController animated:YES completion:^{
//code...
SecondViewController *secondVC = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
[navController presentViewController: secondVC animated:YES completion:nil];
}
}];

presentmodalviewcontroller navigationcontroller

I am creating a Navigation based iPhone application.
In that I have called a UiViewController using presentModalViewController. After that, the ViewController becomes visible. From that ViewController I need to call another ViewController using the sample presentModalViewController. Is this possible or not?
What do you mean by "call another uiviewcontroller"? (It really helps if you can be more detailed in your question.) If you mean, "slide in another view controller", then:
MyNewViewController *myNewViewController = [[MyNewViewController alloc] initWithNibName:#"MyNewViewController" bundle:nil];
[navigationController pushViewController:myNewViewController animated:YES];
[myNewViewController release];
...where:
MyNewViewController is the new view controller class that you want to slide in (the above code assumes you have an XIB file for the view controller class).
navigationController points to the current navigation controller. You'll have to replace it with something like [self navigationController], depending where you are in the view hierarchy.
U might be using following line to present a view controller.
//assume name of viewController which u want to present is "myViewController"
[self.navigationController presentModalViewController:myViewController animated:YES]
If you want to push an other ViewController or present an other ViewController then u will need to replace above line with following lines.
//[self.navigationController presentModalViewController:myViewController animated:YES];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:myViewController];
navigationController.navigationBarHidden = YES; //if u want to show navigation bar then remove this line
[self presentModalViewController:navigationController animated:YES];
After using above code you can present or push other view controllers within presented view controller.
Hope it will solve your problem :)