Go Back to home screen - iphone

I have created button for home like this:
UIBarButtonItem * addButton=[[UIBarButtonItem alloc] initWithTitle:#"Home" style:UIBarButtonItemStyleBordered target:self action:#selector(GoToHome:)];
[navItem setLeftBarButtonItem:addButton];
GoToHome function has only one line i.e:
[self.navigationController popToRootViewControllerAnimated:YES];
but when i click the Home button it shows no action (not working)

try this inside your GoToHome method
[self.parentViewController.parentViewController.parentViewController dismissModalViewControllerAnimated:YES];

have you connected the button code with the IB button? that can happen a lot and with everyone..!!

Try this,
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithTitle:#"Home" style:UIBarButtonItemStylePlain target:self action:#selector(GotoTopScreen)];
self.navigationItem.rightBarButtonItem = addButton;
-(void)GotoTopScreen{
[self.navigationController popToRootViewControllerAnimated:YES];
}

The best way to create a navigation controller is to create it in appDelegate and access it as appDelegate.navigationController. Then those nil object issues do not come.

Use this method:
UIBarButtonItem *flipButtons = [[UIBarButtonItem alloc] initWithTitle:#"Back"
style:UIBarButtonItemStyleBordered target:self
action:#selector(GotoTopScreen)];
self.navigationItem.leftBarButtonItem = flipButtons;
[flipButtons release];
-(void)GotoTopScreen
{
[self.navigationController popToRootViewControllerAnimated:YES];
}

Related

How to make navigation button with Login and Setting title

I want to add two buttons two navigation bar on right hand side one for settings and one for login but problem is that only one button i have searched comes on right that is edit is there any other way in which we can make two button and give them desired title.
UIBarButtonItem *addAttachButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(addAttachmentClicked:)];
UIBarButtonItem *sendButton = [[UIBarButtonItem alloc] initWithTitle:LS(#"Send") style:UIBarButtonItemStyleBordered target:self action:#selector(sendClicked:)];
self.navigationItem.rightBarButtonItems = #[addAttachButton,sendButton];
Here is the code I am using in My UIViewController's implementation file (.m file)
-(void)viewDidLoad{
[super viewDidLoad];
//back button
UIBarButtonItem *backButton = [[UIBarButtonItem alloc]initWithTitle:#" Back " style:UIBarButtonItemStyleBordered target:self action:#selector(backTo:)];
//Optional: if you want to add space between the back & login buttons
UIBarButtonItem *fixedSpaceBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
fixedSpaceBarButtonItem.width = 12;
//login button
UIBarButtonItem *signIn_BarButton = [[UIBarButtonItem alloc]initWithTitle:#" SIGN IN " style:UIBarButtonItemStyleBordered target:self action:#selector(signInUser)];
//add all buttons to right side
self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:backButton, fixedSpaceBarButtonItem,signIn_BarButton, nil];
}
now here are the metthods for both button clicks
-(IBAction)backTo:(id)sender{
[self.navigationController popViewControllerAnimated:YES];
}
-(void) signInUser{
//handle your sigin logic here
}
Please! let me if you need more help!

Adding UIButton top bar of a navigation controller that pushes to another viewController

I have a UIButton that pushes to another view controller using the following code. How do I go about putting this UIButton to the top bar of a navigation controller.
-(IBAction) nextButtonPressed {
TipsViewController *objYourViewController = [[TipsViewController alloc] initWithNibName:#"TipsViewController" bundle:nil];
[self.navigationController pushViewController:objYourViewController animated:YES];
[TipsViewController release];
}
I was thinking of adding something like: self.navigationItem.rightBarButtonItem somewhere, but can't figure out the syntax.
Here's something else I tried with the following code: I can get a "Next" button in the top bar of the Navigation Controller. How would I get it to push to another view controller?
UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithTitle:#"Next" style:UIBarButtonItemStylePlain target:self pushViewController:anotherButton animated:YES];
self.navigationItem.rightBarButtonItem = anotherButton;
[anotherButton release];
thanks for any help.
When you init your UIBarButtonItem, do you see a parameter called action: ? Mention a selector there:
UIBarButtonItem *bar = [[UIBarButtonItem alloc] initWithTitle:#"Next" style:UIBarButtonItemStyleBordered target:self action:#selector(Next:)];
The above item should be your rightBarButton as you want it to be.
And this would be your selector:
-(void)Next:(id)sender {
[self.navigationController pushViewController:nextViewController animated:YES];
}
UIBarButtonItem addButton=[[UIBarButtonItem alloc]initWithTitle:#"+" style:UIBarButtonItemStyleBordered target:self action:#selector(method)];

Problem over-riding the Back button in iOS

I have a UITableViewController A that is pushing UITableViewController B onto the stack.
In A i have the code:
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Trending"
style:UIBarButtonItemStylePlain
target:self
action:#selector(backButtonClicked:)];
backButtonClicked: is also implemented.
B has the title Trending, but when I click it, it doesn't ever reach backButtonClicked:
Why is this?
Try either setting the delegate:
[navigationController setDelegate:self];
Or using the left button. Sometimes the back button doesn't work with certain views and I have had to use the left button instead:
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Trending" style:UIBarButtonItemStylePlain target:self action:#selector(backButtonClicked:)];
Also, you can try setting B's button item instead of A:
viewControllerB.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Trending" style:UIBarButtonItemStylePlain target:self action:#selector(backButtonClicked:)];
In the Xcode documentation, it states that the backBarButtonItem target and action should be set to nil. So even if you do set it, it's probably a good bet that it will be ignored. You could check out the link below to add custom behaviour to the back button.
Custom Action on Back Button Item
Or you could just do the following in viewControllerB:
self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:#"Trending"
style:UIBarButtonItemStylePlain
target:self
action:#selector(backButtonClicked:)] autorelease];
Then also add this to viewControllerB
- (void)backButtonClicked:(id)sender {
[[[self.navigationController viewControllers] objectAtIndex:0] backButtonClicked:sender];
[self.navigationController popViewControllerAnimated:YES];
}
The above method will find the RootViewController and send it the backButtonClicked message. It will then pop the current view controller, which should allow you to emulate the backBarButtonItem. Also you can change which view controller you want to send the message by changing the value in the objectAtIndex method.
try not backBarButtonItem but leftBarButtonItem instead
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Trending" style:UIBarButtonItemStylePlain target:self action:#selector(backButtonClicked:)];
self.navigationItem.hidesBackButton=YES;
for me it works like a charm. And don't forget that you didn't release this button - and it can cause memory leak. So you can add autorelease when you assign you button or make it like this
UIBarButtonItem *myBackButton = [[UIBarButtonItem alloc] initWithTitle:#"Trending" style:UIBarButtonItemStylePlain target:self action:#selector(backButtonClicked:)];
self.navigationItem.leftBarButtonItem = myBackButton;
[myBackButton release];
self.navigationItem.hidesBackButton=YES;

how to set custom method with rightnavagation Button in NavBar

I want set my custom method "Home" with RightNavagation button how should I?
My code
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithTitle:#"DontWorryAboutThis" style:UIBarButtonItemStylePlain target:self action:#selector(home:)];
[barButton setImage:[UIImage imageNamed:#"home_btn.png"]];
[self.navigationItem setRightBarButtonItem:barButton];
Know I want to link this method with it.
-(IBAction)home{
MainViewController *main=[[MainViewController alloc]
initWithNibName:#"MainViewController" bundle:nil];
[self.navigationController pushViewController:main animated:YES];
}
how should I Do this please help me out.
Try to change
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithTitle:#"DontWorryAboutThis" style:UIBarButtonItemStylePlain target:self action:#selector(home:)];
in
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithTitle:#"DontWorryAboutThis" style:UIBarButtonItemStylePlain target:self action:#selector(home)];
compare your selector #selector(home:) with your action -(IBAction)home
See the missing colon in your action?
Change your action to
- (IBAction)home:(id)sender

trying to programmatically create rightBarButtonItem

This doesn't seem to be working. What am i doing wrong?
-(void)awakeFromNib{
UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(showNewEventViewController)];
self.navigationItem.rightBarButtonItem = rightBarButtonItem;
NSLog(#"awaked");
[rightBarButtonItem release];
}
my guess is, that you add the UIBarButtonItem to the wrong object!
you need to add it, to the rootViewController (instead to the UINavigationController, as you probably did)
YourRootViewController *theRootController = [[YourRootViewController alloc] init];
UINavigationController* navContainer = [[UINavigationController alloc] initWithRootViewController:theRootController];
UIBarButtonItem *btnCancel = [[UIBarButtonItem alloc] initWithTitle:#"Cancel" style:UIBarButtonItemStyleBordered target:self action:#selector(dismiss)];
theRootController.navigationItem.rightBarButtonItem = btnCancel
[navContainer setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[self presentModalViewController:navContainer animated:YES];
I would normally put this code in the viewDidLoad method rather than the awakeFromNib method; I'm not sure if that's where your problem lies. What does "not working" mean?
Try this instead:
- (void) initUI {
UIBarButtonItem *btnCancel = [[[UIBarButtonItem alloc] initWithTitle:#"Cancel"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(dismiss)]autorelease];
self.navigationItem.rightBarButtonItem = btnCancel;
//[btnCancel release]; no need to explicitly release the item
}