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
Related
I am new at iOS Development.I Have UINavigationController as rootviewcontroller for UIWindow.In the subclass i added UITabbarController programatically.I given default Tabbarcontroller selection as first controller. Here i am trying to add 3 buttons to the navigationbar .
Can any send me the corrent code.
Thanks In advance
To add more than one button on the left or the right side you have to call one of the following methods:
- (void)setRightBarButtonItems:(NSArray *)items animated:(BOOL)animated
- (void)setLeftBarButtonItems:(NSArray *)items animated:(BOOL)animated
The array items contains instances of the UIBarButtonItem class. You can instantiate them like this
UIBarButtonItem *FirstButton = [[UIBarButtonItem alloc]
initWithTitle:#"Title"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(buttonTapped:)];
Then you have to implement the selector buttonTapped: which is called when the button has been tapped.
-(void)buttonTapped:(UIBarButtonItem *)button
{
// do the things that should happen when the button is pressed
}
If you don't want to get them on the left or right side it is also possible to create a UIView by yourself containing the buttons and set the view to be the titleView of the UINavigationItem. This effect is similar to the buttons in the Facebook App
[self.navigationItem setTitleView:yourView];
Here is simple code for add UIButton on UINavigationBar
In Following code you can add Button With FlexibleSpace
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
[barItems addObject:flexSpace];
[flexSpace release];
UIBarButtonItem *btnFirst = [[UIBarButtonItem alloc] initWithTitle:#"First" style:UIBarButtonItemStyleBordered target:self action:#selector(FirstTapped:)];
[barItems addObject: btnFirst];
UIBarButtonItem *btnSecond = [[UIBarButtonItem alloc] initWithTitle:#"Second" style:UIBarButtonItemStyleBordered target:self action:#selector(SecondTapped:)];
[barItems addObject:btnSecond];
UIBarButtonItem *btnThird = [[UIBarButtonItem alloc] initWithTitle:#"Third" style:UIBarButtonItemStyleBordered target:self action:#selector(ThirdTapped:)];
[barItems addObject: btnThird];
self.navigationItem.rightBarButtonItems = barItems;
Button Related Methods
-(void) FirstTapped:(UIBarButtonItem *)sender{
//perform your action
}
-(void) SecondTapped:(UIBarButtonItem *)sender{
//perform your action
}
-(void) ThirdTapped:(UIBarButtonItem *)sender{
//perform your action
}
NOTE : self.navigationItem.rightBarButtonItems Works only in iOS 5 or Latter.
UIButton *customButton = [UIButton buttonWithType:UIButtonTypeCustom];
// here is custom button setup //
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:customButton];
[self.navigationItem setLeftBarButtonItem:item animated:YES];
I have created navigation controller via storyboard interface and I have added 4 buttons programmatically on Navigationbar but I don't know how should I justify it would you please help me!
here is the picture:
here is my code for buttons I know That I used rightBarButtonItems but I don't know what should I write instead!
- (void)viewDidLoad
{
[super viewDidLoad];
UIBarButtonItem *menuButton= [[UIBarButtonItem alloc] initWithTitle:#"Menu" style:UIBarButtonItemStyleDone
target:self action:#selector(menu:)];
UIBarButtonItem *yearButton= [[UIBarButtonItem alloc] initWithTitle:#"Year" style:UIBarButtonItemStyleDone
target:self action:#selector(year:)];
UIBarButtonItem *weekButton= [[UIBarButtonItem alloc] initWithTitle:#"Week" style:UIBarButtonItemStyleDone
target:self action:#selector(week:)];
UIBarButtonItem *reportButton= [[UIBarButtonItem alloc] initWithTitle:#"Report" style:UIBarButtonItemStyleDone
target:self action:#selector(report:)];
NSArray *buttons = [NSArray arrayWithObjects:menuButton,yearButton,weekButton,reportButton,nil];
self.navigationItem.rightBarButtonItems = buttons;
}
Add flexible spacers between each of the buttons:
UIBarButtonItem *flexibleSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
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];
}
I want to show next previous buttons above the keyboard. I call function "makeToolBars" from viewDidLoad to create it. The application does display the buttons correctly but when I click on them I get error it does not go to "goToNextField" function.
Can anyone point how to correct it ?
-(void)goToNextField:(id)sender
{
[txtPassword resignFirstResponder];
[txtUserName becomeFirstResponder];
}
-(void) makeToolBars
{
UIToolbar *toolbar1 = [[[UIToolbar alloc] init] autorelease];
[toolbar1 setBarStyle:UIBarStyleBlackTranslucent];
[toolbar1 sizeToFit];
UIBarButtonItem *nextButton = [[UIBarButtonItem alloc]
initWithTitle:#"Next"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(goToNextField)];
UIBarButtonItem *flexButton1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
NSArray *itemsArray1 = [NSArray arrayWithObjects: nextButton,flexButton1, nil];
[flexButton1 release];
[nextButton release];
[toolbar1 setItems:itemsArray1];
[txtUserName setInputAccessoryView:toolbar1];
}
The below statement is wrong.
UIBarButtonItem *nextButton = [[UIBarButtonItem alloc]
initWithTitle:#"Next"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(goToNextField)];
use the below modified correct code .
UIBarButtonItem *nextButton = [[UIBarButtonItem alloc]
initWithTitle:#"Next"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(goToNextField:)];
Because you might forget to append colon in goToNextField (Correct goToNextField:).
And in your function implementation for goToNextField you were considering the colon and put :(id) sender in your function.
You've written action:#selector(goToNextField)];. It should be action:#selector(goToNextField:)];
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
}