Why is viewWillDisapper not called when I push back button in navigation bar of myViewController?
TheController *theController = [[TheController alloc] initWithStyle:UITableViewStylePlain];
UINavigationController *aNavigationController = [[UINavigationController alloc] initWithRootViewController:theController];
self.naviController = aNavigationController;
[aNavigationController release];
[theController release];
[self.view addSubview:naviController.view];
// This is TheController.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
MyViewController *myViewController = [[MyViewController alloc] init];
[self.navigationController pushViewController:myViewController animated:YES];
[myViewController release];
}
// This is MyViewController.m.
MyViewConroller is subclass of UIViewController.
- (void)viewWillDisappear:(BOOL)animated {
// I want to override back button behavior. But, this is not called.
}
viewWillAppear, viewDidAppear, viewDidDisappear, viewDidUnLoad are not called either.
If you just want to override the left navigation bar button behavior, you can simply replace the leftBarButtonItem with a button with a custom action:
// Place this in you viewDidLoad method
UIBarButtonItem *customBack = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonSystemItemCancel target:self action:#selector(customAction)];
self.navigationItem.leftBarButtonItem = customBack;
[customBack release];
You can also provide a custom view for the UIBarButtonItem if you prefer.
You should call viewWillAppear in your parent view controller.
http://bit.ly/q9vQiu
Related
I'm having some strange difficulties with setting a title and adding a bar button to my navigation bar.
My app is a Tabbed application. The problem is within my first tab. The first tab consists of a UINavigationController, that contains a UITableViewController. From my AppDelegate:
UIViewController *firstViewController = [[MyTableViewController alloc] init];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:firstViewController];
_tabBarController = [[TFTabBarController alloc] init];
_tabBarController.delegate = self;
_tabBarController.viewControllers = [NSArray arrayWithObjects:navigationController, nil];
On selecting a row in firstViewController, I push a UIWebViewController on the navigation stack:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UIViewController *secondViewController = [[MyWebViewController alloc] init];
[self.navigationController pushViewController:secondViewController animated:YES];
}
On intercepting the onclick of a hyperlink in the WebView, I push another UITableView on the navigation stack:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSURL *url = [request URL];
if (navigationType == UIWebViewNavigationTypeLinkClicked)
{
UIViewController *thirdViewController = [[MyTableViewController alloc] init];
[self.navigationController pushViewController:thirdViewController animated:YES];
return NO;
}
return YES;
}
As thirdViewController opens, its navigation bar only contains the back button with the title of the previous view to navigate back. Its title and the right bar button are not shown...
This is how I try to display them:
self.title = #"My Title";
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Push me" style:UIBarButtonItemStylePlain target:self action:#selector(push)];
I also tried setting the button through the navigationController, but still no luck:
self.navigationController.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Push me" style:UIBarButtonItemStylePlain target:self action:#selector(push)];
What could I be doing wrong here?
Check that self.navigationItem and self.navigationController are not nil when accessed.
From the documentation:
The first time the property is accessed, the UINavigationItem object is created. Therefore, you shouldn’t access this property if you are not using a navigation controller to display the view controller.
A good place to access these members is under -viewDidLoad and not upon initialization.
I'd like to add a UINavigationController to my app info view (NOT to my main view). I've watched/read a number of tutorials showing how to add it to the main window through the AppDelegate using IB. In my case, I only want it to appear when a user presses the info button and is brought to the infoView. Here is how I switch to the infoView within my MainViewController:
- (IBAction)infoButtonPress:(id)sender
{
// Create pointer to instance of InfoViewController
InfoViewController *infoView = [[InfoViewController alloc] initWithNibName:#"InfoViewController" bundle:nil];
// Add view switching animation
infoView.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
// Change view using animation
[self presentModalViewController:infoView animated:YES];
}
At this point the infoView is displayed and I would like THIS to be the RootView of the UINavigationController. I have tried adding the line:
UINavigationController *infoNavController = [[UINavigationController alloc]
initWithRootViewController:infoView];
after creating an instance of InfoViewController, but the app crashes. Is it possible to add UINavigationController to views other than the main view?
Thanks.
You are very close with your implementation. Try it in this order.
InfoViewController *infoView = [[InfoViewController alloc] initWithNibName:#"InfoViewController" bundle:nil];
UINavigationController *infoNavController = [[UINavigationController alloc]
initWithRootViewController:infoView];
[infoView release]; // skip this if using ARC
infoView.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:infoNavController animated:YES];
UPDATE
To kill this modal, you will have to add your buttons to the main modal view.
InfoViewController.m
-(void)cancel:(id)sender {
[self dismissModalViewControllerAnimated:YES];
}
-(void)viewDidLoad {
[super viewDidLoad];
UIBarButtonItem *cancelButton =
[[UIBarButtonItem alloc] initWithTitle: #"Cancel"
style: UIBarButtonItemStylePlain
target: self
action: #selector(cancel:)];
self.navigationItem.leftBarButtonItem = cancelButton;
[cancelButton release];
}
I've got a TabBar application and 2 views.
One view is a MKMapView with annotations and callouts etc. All works well. Clicking on the 'UIButtonTypeDetailDisclosure' button in the callout my 'showDetails' function fires (NSLOG..).
Now I want to push a DetailView in 'showDetails'. I'd have to either use pushViewController or presentModalViewController
#pragma mark - MKMap methods
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation{
if([annotation isKindOfClass:[CustomPlacemark class]])
{
MKPinAnnotationView *newAnnotation = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:[annotation title]];
newAnnotation.pinColor = MKPinAnnotationColorGreen;
newAnnotation.animatesDrop = YES;
newAnnotation.canShowCallout = YES;
newAnnotation.enabled = YES;
//newAnnotation.pinColor=MKPinAnnotationColorPurple;
NSLog(#"Created annotation at: %f %f", ((CustomPlacemark*)annotation).coordinate.latitude, ((CustomPlacemark*)annotation).coordinate.longitude);
[newAnnotation addObserver:self
forKeyPath:#"selected"
options:NSKeyValueObservingOptionNew
context:#"GMAP_ANNOTATION_SELECTED"];
UIImageView *leftIconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"leftIconView.png"]];
newAnnotation.leftCalloutAccessoryView = leftIconView;
[leftIconView release];
UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton setTitle:annotation.title forState:UIControlStateNormal];
[rightButton addTarget:self
action:#selector(showDetails:)
forControlEvents:UIControlEventTouchUpInside];
newAnnotation.rightCalloutAccessoryView = rightButton;
[newAnnotation autorelease];
return newAnnotation;
}
return nil;
}
- (void) showDetails:(id)sender {
NSLog(#"Annotation Click");
[self.navigationController pushViewController:self.detailViewController animated:YES];
// I want to push a new View here...
}
Unfortunately this doesn't do anything (No error / no action). I reckon it's because I don't have a navigationController to use pushViewController.
- (void) showDetails:(id)sender {
NSLog(#"Annotation Click");
[self.navigationController pushViewController:self.detailViewController animated:YES];
// I want to push a new View here...
}
i am trying to create and push a DetailViewController in the navigationController when my 'UIButtonTypeDetailDisclosure' button in notification is clicked. but navigationController is nil
Any idea what to add to my 'showDetails' function?
Much appreciated
You need to have a navigation controller before pushing views on it:
DetailViewController *detailController = [[DetailViewController alloc] initWithNibName:#"DetailViewController" bundle:[NSBundle mainBundle]];
// this line calls the viewDidLoad method of detailController
detailController.view.hidden = NO;
UINavigationController *navigationController = [[UINavigationController alloc] detailController];
navigationController.navigationBarHidden = YES;
[navigationController setView:detailController.view];
[self.view addSubview:navigationController.view];
[detailController release];
[navigationController release];
Be careful this will initialize navigationController again and again if you do this in your showDetails:. So you need to initialize your navigation controller in eg. viewDidLoad and then you will be able to push your detailController onto it
If you want to push a view controller with a navigation controller, then your initial view must be within the navigation controller to begin with.
In your case, your tab bar controller should probably be put in the navigation controller if it isn't already. Then you can call your showDetails method the way you have done above. If it is in the nav controller, maybe you can post the code where you do that and I can try and offer more assistance.
Here is how you might do something like this in your applicationDidFinishLaunchingWithOptions: method
FirstViewController *fvc = [[FirstViewController alloc] init];
SecondViewController *svc = [[SecondViewController alloc] init];
UITabBarController *tabcon = [[UITabBarController alloc] init];
tabcon.viewControllers = [NSArray arrayWithObjects:fvc,svc,nil];
UINavigationController *navcon = [[UINavigationController alloc] initWithRootViewController:tabcon];
[self.window addSubview:navcon.view];
Then when you want to push a detail view controller, you just call:
DetailViewController *dvc = [[DetailViewController alloc] init];
[self.navigationController pushViewController:dvc];
If you set your view controllers as properties, you can call self.viewControllerName, just make sure you are allocating memory for them as well. Embedding your tab bar controller in a navigation controller like this gives you all the window dressing (back buttons, title bar, etc.) for free and it will adapt to your different views as they come on screen.
Check this, im pushing a modal view inside of another modal view. But, im trying to put a button inside of this modal view, but without luck.
What im doing wrong?
Thanks!
CadastroViewController *addController = [[CadastroViewController alloc] initWithNibName:#"CadastroViewController" bundle:nil];
// This is where you wrap the view up nicely in a navigation controller
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:addController];
// You can even set the style of stuff before you show it
navigationController.navigationBar.barStyle = UIBarStyleBlackOpaque;
navigationController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"OK" style:UIBarButtonItemStyleBordered target:self action:#selector(buy)];
// And now you want to present the view in a modal fashion all nice and animated
[self presentModalViewController:navigationController animated:YES];
// make sure you release your stuff
[navigationController release];
[addController release];
You'll have to add a new UINavigationItem to the navigationbar of the actual viewcontroller - NOT the navigation controller.
addController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"OK" style:UIBarButtonItemStyleBordered target:self action:#selector(buy)];
You should add your button in a
-(void) viewDidLoad of your CadastroViewController controller class
This will look like this:
- (void) viewDidLoad
{
[super viewDidLoad];
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithTitle:#"OK" style:UIBarButtonItemStyleBordered target:self action:#selector(buy)];
self.navigationController. leftBarButtonItem = button;
[button release];
}
[self presentModalViewController: navigationController animated:YES]; is ok in your example, just all other initializations you should do in viewDidLoad
It seems to me, that the problem is here:
[self presentModalViewController: navigationController animated:YES];
Instead try do this:
[self presentModalViewController: addController animated:YES];
I have a UIViewController that is presented modally. The UIViewController is inside of a UINavigationController.
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
saveButton = [[UIBarButtonItem alloc] initWithTitle:#"Save"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(saveButtonClicked:)];
self.navigationItem.rightBarButtonItem = saveButton;
self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:(102.0/255.0) green:(20.0/255.0) blue:(11.0/255.0) alpha:1];
self.title = #"Login";
//toolbar.tintColor = [UIColor colorWithRed:(102.0/255.0) green:(20.0/255.0) blue:(11.0/255.0) alpha:1];
}
Why isn't my navbar appearing with the text Login and a save button to the right?
It sounds like you may be presenting the view controller instead of the navigation controller. Your code to present the view controller you've described should look something like this:
MyViewController *viewController = [[MyViewController alloc] init];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewController];
[self presentModalViewController:navController animated:YES];
[viewController release];
[navController release];
You set the properties after the nib is loaded, but you probably need to do it after/while it is pushed.