UINavigationController not being animated as wanted - iphone

Well, I have a tab based app with a navigationController for each tab. When I am at the root of each navigationController and I push a view, the animation runs perfect, but when I am on a pushed view and I want to pop it the navigationController gets animated but not the view. This is what I use to pop it:
[self.navigationController popViewControllerAnimated:YES];
and to push it:
[self.navigationController pushViewController:activityController animated:YES];
any suggestion?
EDIT:
I have a tableView in the root controller. Every time I select a row I run this code
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
ActivityViewController *activityController = [[ActivityViewController alloc] initWithNibName:#"ActivityViewController" bundle:nil];
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[backButton setBackgroundImage:[UIImage imageNamed:#"BackButonItem"] forState:UIControlStateNormal];
[backButton setBackgroundImage:[UIImage imageNamed:#"BackButonItem_Pressed"] forState:UIControlStateHighlighted];
[backButton addTarget:self action:#selector(popBack) forControlEvents:UIControlEventTouchUpInside];
[backButton setFrame:CGRectMake(15, 10, 55, 30)];
UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
activityController.navigationItem.leftBarButtonItem = backButtonItem;
activityController.navigationItem.hidesBackButton = YES;
[self.navigationController pushViewController:activityController animated:YES];
}
and this is my popBack method:
- (void) popBack
{
[self.navigationController popViewControllerAnimated:YES];
}

Did you try with
[self.navigationController popToViewController:self animated:YES];
It seems to have solved the problem for a lot of people.

Related

ios - Not adding backbutton to NavigationBar on ViewController

I can not add on NavigationBar backbutton. I want to perform this operation on a ViewController, I have tried different ways, but could not reach a solution. Do you need to use Backbutton NavigationController to use, or you can perform on a ViewController?
Waiting for your help.
If your ViewController is within a navigation controller, you should be able to do this:
[self.navigationController popViewControllerAnimated:YES];
Hide the default back button
[self.navigationItem setHidesBackButton:YES animated:YES];
UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *backBtnImage = [UIImage imageNamed:#"back_arrow.png"] ;
[backBtn setBackgroundImage:backBtnImage forState:UIControlStateNormal];
[backBtn addTarget:self action:#selector(goback) forControlEvents:UIControlEventTouchUpInside];
backBtn.frame = CGRectMake(0, 0, 35, 23);
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithCustomView:backBtn] ;
self.navigationItem.leftBarButtonItem = backButton;
- (void)goback
{
[self.navigationController popViewControllerAnimated:YES];
}

Setting Back Button to a custom UIImage

Currently I'm using my own custom images in UIBarButtonItems with the following code:
UIButton *profileBarButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 35.0f, 35.0f)];
[profileBarButton setImage:[UIImage imageNamed:#"profile-barbutton.png"] forState:UIControlStateNormal];
[profileBarButton addTarget:self.navigationController action:#selector(toggleMenu) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:profileBarButton];
This works perfectly when I have a defined action to call, such as presenting a modalviewcontroller and turning on edit mode. However, I'm confused as to how I'd set the action for going back from one view to another, not modally? Is there a specific method I could call programmatically? Usually the navigationcontroller takes care of this...
for going back from one view to another, not modally you may write like this:
- (void) toggleMenu
{
if (self.navigationController.visibleViewController == self)
{
[self.navigationController popViewControllerAnimated: YES];
}
}
Use this code:
[self dismissViewControllerAnimated:YES completion:nil];
Check with this code...
MyViewController *vc = [[MyViewController alloc] initWithNibName:#"MyNib" bundle:nil];
UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:vc];
[self presentModalViewController:nc animated:YES];
[vc release];
[nc release];
Then normally push code like...
OtherViewController *vc = [[OtherViewController alloc] initWithNibName:#"MyOtherNib" bundle:nil];
[self.navigationController pushViewController:vc animated:YES];
[vc release];
:)
Try this code:
UIImage *backButtonImage = [UIImage imageNamed:#"backButton.png"];
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[backButton setImage:backButtonImage forState:UIControlStateNormal];
backButton.frame = CGRectMake(0, 0, backButtonImage.size.width, backButtonImage.size.height);
[backButton addTarget:self action:#selector(back) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *customBackBarItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
self.navigationItem.leftBarButtonItem = customBackBarItem;
In the #selector(back), "back" is the method that triggers the popup method of navigationcontroller. Like this:
-(void)back {
[self.navigationController popViewControllerAnimated:YES];
}

adding buttons to a bottom toolbar

I have the following code:
self.shareButton=[[UIButton alloc] initWithFrame:CGRectMake(0.0, 0.0, 35, 35)];
[shareButton setBackgroundColor:[UIColor blueColor]];
[shareButton setBackgroundImage:[UIImage imageNamed:#"share_button"] forState:UIControlStateNormal];
[shareButton setBackgroundImage:[UIImage imageNamed:#"share_button_pressed"] forState:UIControlStateHighlighted];
[shareButton addTarget:self action:#selector(shareButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.navigationController setToolbarItems:[NSArray arrayWithObject:shareButton] animated:YES];
Why I cannot see toolbar buttons? What's the problem?
What meggar says is probably the right answer. Try doing it this way.
--UPDATE--
Since the problem still exists I have updated the example to move the set call to the view controller instead of the navigation controller.
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:shareButton];
[self setToolbarItems:[NSArray arrayWithObject:barButtonItem] animated:YES];
[barButtonItem release];

iPhone: Add button on MPMoviePlayerViewController's navigation bar

I am trying to add button on MPMoviePlayerViewController's navigationcontroller.view. I have wrote following so far. It doesn't give any error but button isn't appearing on view! Could anyone please tell me what am I doing wrong? Thanks.
MPMoviePlayerViewController *videoController = [[[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:filePath]] autorelease];
UIButton *favButton = [UIButton buttonWithType:UIButtonTypeCustom];
favButton.frame = CGRectMake(280, 25, 30, 30);
[favButton addTarget:self action:#selector(favouriteButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
[favButton setBackgroundImage:[UIImage imageNamed:#"unselected.png"] forState:UIControlStateNormal];
[videoController.navigationController.view addSubview:favButton];
[self presentMoviePlayerViewControllerAnimated:videoController];
- (void)favouriteButtonClicked:(id)sender
{
NSLog(#"Inside favourite button clicked");
}
Also try adding using following code but no luck!
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(favouriteButtonClicked:)];
videoController.navigationController.navigationItem.rightBarButtonItem=button;
[button release];
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(favouriteButtonClicked:)];
videoController.navigationItem.rightBarButtonItem = button;
[button release];
If you use presentModalViewController, you can not see the button you add. You have to [self.navigationController pushViewController:moviePlayer animated:YES] to see it.
Try this..it will work
[[[UIApplication sharedApplication] keyWindow] addSubview:favButton];

How to edit a TableView in iPhone?

In my application i need an edit button for my tableview that can delete a row or can change its position.
it is really easy when i am using a default navigation bar but now in my case i am using a custom bar that is infect an imageview & now i need a button that can edit a tableview. i am not using the default navigation bar.
so plz help me
You just need to set the editing property of the UITableView, in your UITableViewController implement something like;
- (void) editingButtonPressed:(id)sender {
if([self isEditing]) {
[sender setText:#"Edit" forState:UIControlStateNormal];
[self setEditing:NO animated:YES];
} else {
[sender setText:#"Done" forState:UIControlStateNormal];
[self setEditing:YES animated:YES];
}
}
And hook that up to your button or image and replace the setText with setImage if you have no text.
Here is my init method for a UITableViewController using a custom UIToolbar which adds two buttons to the navigation bar in place of the right navigation bar button.
- (id) init {
[super initWithStyle:UITableViewStyleGrouped];
UIBarButtonItem *email = [[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"Email.png"] style:UIBarButtonItemStyleBordered target:self action:#selector(composeEmail:)] autorelease];
UIBarButtonItem *bookmark = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(addBookmark:)] autorelease];
[bookmark setStyle:UIBarButtonItemStyleBordered];
CustomToolbar *buttonToolbar = [[CustomToolbar alloc] initWithFrame:CGRectMake(0, 0, 93, 45)];
[buttonToolbar setBarStyle:UIBarStyleBlackTranslucent];
[buttonToolbar setItems:[NSArray arrayWithObjects:email, bookmark, nil] animated:NO];
[[self navigationItem] setTitle:#"Table with Custom Toolbar"];
[[self navigationItem] setRightBarButtonItem:[[[UIBarButtonItem alloc] initWithCustomView:buttonToolbar] autorelease]];
[buttonToolbar release];
return self;
}
When creating the buttons I use action:#selector(customMethodName) when creating the buttons to hook up my methods to the button actions in this case composeEmail and addBookmark which load the new views for those tasks.
you can add a button on your imageview and on that button click set your tableviewediting to yes.
here's the code how to add a imageview and button:-
UIImageView *imageView2=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
[imageView2 setImage:[UIImage imageNamed:#"bottom bar_gda.png"]];
[self.view addSubview:imageView2];
[self.view bringSubviewToFront:imageView2];
UIButton *deleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
[deleteButton setFrame:CGRectMake(280, 3, 26, 36)];
deleteButton.contentMode = UIViewContentModeScaleAspectFill;
UIImage *newImage12 = [UIImage imageNamed:#"check.png"];
[deleteButton setBackgroundImage:newImage12 forState:UIControlStateNormal];
[deleteButton setBackgroundImage:newImage12 forState:UIControlStateHighlighted];
[deleteButton addTarget:self action:#selector(editmethod:) forControlEvents:UIControlEventTouchUpInside];
[imageView2 addSubview:deleteButton];
You have to add UIButton instance to that imageview and implement a method that will execute when an event occurs on that UIButton.
Code for the method will look like,
if(tableView.editing)
[tableView setEditing:NO animated:YES]
else
[tableView setEditing:YES animated:YES]
Read UITableView Documentation for more info.
YOU CAN USE SWIPE TO DELETE FEATURE.
THIS CAN BE DOEN IN FOLLOWING WAY.
you need to implement following 3 delegate method of tableView.
tableView:commitEditingStyle:forRowAtIndexPath:
tableView:canEditRowAtIndexPath:
tableView:editingStyleForRowAtIndexPath:
In method
tableView:editingStyleForRowAtIndexPath:
RETURN TYPE SHOULD BE
UITableViewCellEditingStyleDelete.