Add barButtons to navigationbar with modalView in iPhone - iphone

In my application, I'm presenting a modalViewController with a navigationbar on the top of it, but I'm not able to add barbuttons to the navigatiobar. I'm using following code:
displayController = [[UIViewController alloc]initWithNibName:nil bundle:nil];
UINavigationController *cntrol = [[UINavigationController alloc] initWithRootViewController:displayController];
[displayController setTitle:#"TEST"];
UIBarButtonItem *submit = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self
action:#selector(displayViewForPosts)];
displayController.navigationItem.rightBarButtonItem = submit;
[submit release];
UIBarButtonItem *newsfeed = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self
action:#selector(displayViewForPosts)];
displayController.navigationItem.leftBarButtonItem = newsfeed;
[newsfeed release];
[self presentModalViewController:cntrol animated:YES];
[cntrol release];
[displayController release];

UIViewController *detailViewController = [[UIViewController alloc] initWithNibName:nil bundle:nil];
UINavigationController *cntrol = [[UINavigationController alloc] initWithRootViewController:detailViewController];
[detailViewController setTitle:#"TEST"];
UIBarButtonItem *submit = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self
action:#selector(displayViewForPosts)];
detailViewController.navigationItem.rightBarButtonItem = submit;
[submit release];
[self.navigationController presentModalViewController:cntrol animated:YES];
[cntrol release];

Related

Adding back button to a view controller

I have created 3 UIViewControllers for my 3 UIToolBar items but i am not able to create a button on each controller which takes me back to the home page or main view controller. Kindly help me as i am new to objective c.
Here is my code....
"ViewController.h"
#import "ViewController.h"
#import "ContactInfoViewController.h"
#import "DateViewController.h"
#import "MessageViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
UIToolbar *toolbar = [[UIToolbar alloc] init];
toolbar.frame = CGRectMake(0, 417, self.view.frame.size.width, 44);
UIBarButtonItem *contact = [[UIBarButtonItem alloc] initWithTitle:#"Contact" style:UIBarButtonItemStyleDone target:self action:#selector(ContactButton)];
UIBarButtonItem *flexiableItem1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
UIBarButtonItem *date = [[UIBarButtonItem alloc] initWithTitle:#"Date" style:UIBarButtonItemStyleDone target:self action:#selector(DateButton)];
UIBarButtonItem *flexiableItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
UIBarButtonItem *message = [[UIBarButtonItem alloc] initWithTitle:#"Message" style:UIBarButtonItemStyleDone target:self action:#selector(MessageButton)];
NSMutableArray *items = [[NSMutableArray alloc] initWithObjects:contact,flexiableItem1,date,flexiableItem,message, nil];
[toolbar setItems:items animated:YES];
[items release];
[self.view addSubview:toolbar];
[toolbar release];
}
-(void)ContactButton
{
ContactInfoViewController *secondViewcont = [[ContactInfoViewController alloc] init];
UINavigationController *navigCont = [[UINavigationController alloc] initWithRootViewController:secondViewcont];
[self presentViewController:navigCont animated:YES completion:nil];
}
-(void)DateButton
{
DateViewController *secondViewcont = [[DateViewController alloc] init];
UINavigationController *navigCont = [[UINavigationController alloc] initWithRootViewController:secondViewcont];
[self presentViewController:navigCont animated:YES completion:nil];
}
-(void)MessageButton
{
MessageViewController *secondViewcont = [[MessageViewController alloc] init];
UINavigationController *navigCont = [[UINavigationController alloc] initWithRootViewController:secondViewcont];
[self presentViewController:navigCont animated:YES completion:nil];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
#end
Add custom button to your second view controller
In viewDidLoad you can do like this
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(doneButtonPressed:)]autorelease];
- (IBAction)doneButtonPressed:(id)sender
{
[self dismissViewControllerAnimated:YES completion:nil];
}

UIBarButtonItem not displayed

I can't make my UIBarButtonItem to display. I simplified it as much as I could, but even this does not display the bar :
- (void)viewDidLoad {
UIBarButtonItem *supprBouton =[[UIBarButtonItem alloc]
initWithTitle:#"Supprimer"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(supprimerDate:)];
self.navigationItem.rightBarButtonItem = supprBouton;
UIBarButtonItem *enregBouton = [[UIBarButtonItem alloc]
initWithTitle:#"Enregistrer"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(enregFichier:)];
self.navigationItem.leftBarButtonItem = enregBouton;
[enregBouton release];
[supprBouton release];
}
One right button, one left, this should be displayed at least, no ? What can be wrong ?
Without more context, my best guess is that you have not yet set up a UINavigationController.
Try to add a uinavigationcontroller to your view:
UIViewController *vc = [[UIViewController alloc] initWithNibName:nil bundle:nil];
UINavigationController *navCntrl1 = [[UINavigationController alloc] initWithRootViewController:vc];

?How to add custom button to UIPopoverController

How to add button to UIPopoverController ?
I am attaching a picture for able to understand very clear.
UIViewController* popoverContent = [[UIViewController alloc] init];
UIView *popoverView = [[UIView alloc] init];
popoverView.backgroundColor = [UIColor blackColor];
UIToolbar *toolbar=[[UIToolbar alloc] initWithFrame:CGRectMake(0, 0,400 ,44)];
toolbar.barStyle =UIBarStyleBlackTranslucent;
NSMutableArray *ButtonArray=[[NSMutableArray alloc ]init];
Save=[[UIBarButtonItem alloc ]initWithTitle:#"Done" style:UIBarButtonItemStyleBordered target:self action:#selector(save_pressed)];
UIBarButtonItem *space=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *cancel=[[UIBarButtonItem alloc ]initWithTitle:#"Cancel" style:UIBarButtonItemStyleBordered target:self action:#selector(cancel_pressed)];
[ButtonArray addObject:cancel];
[cancel release];
[ButtonArray addObject:space];
[space release];
[ButtonArray addObject:Save];
[Save release];
[toolbar setItems:ButtonArray];
[ButtonArray release];
[popoverView addSubview:toolbar];
[toolbar release];
picker = [[UIPickerView alloc] init];
picker.frame = CGRectMake(0, 44, 400, 340);
picker.delegate = self;
picker.dataSource = self;
picker.showsSelectionIndicator = YES;
[popoverView addSubview:picker];
popoverContent.view = popoverView;
popoverController = [[UIPopoverController alloc] initWithContentViewController:popoverContent];
[popoverController setPopoverContentSize:CGSizeMake(400, 264) animated:NO];
[popoverController presentPopoverFromRect:btn.frame inView:scr_AddLocation permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
[popoverView release];
[popoverContent release];
here I m adding Picker To Popover When We Pressed Button .
The cancel button (if that is what you are referring to) is the rightBarButtonItem of the UINavigationController's navigation bar. You can set it to be a standard UIBarButtonItem (as is done here) or a custom one (whose view you can create).

Adding a Right "done" Button (UIBarButtonItem) to a UINavigationController

I see that a similar question was asked here: How to add a right button to a UINavigationController? (among others) but its not quite what I'm looking to do and they arent solving my problem.
Essentially, I've created a UIViewController called WebViewViewController with a UIWebView on it which will be shown using presentModalViewController. Essentially its a mini web browser to display a web page while keeping the user in the app rather than launching Safari.
The viewController does the following to get it to show... and the "done" button is meant to provide a place to close the browser.
-(IBAction)visitFacebook {
WebViewViewController *rootController = [[WebViewViewController alloc] init];
rootController.webURL = #"http://www.facebook.com/";
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:rootController];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc ] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(done:)];
[navigationController.navigationItem setRightBarButtonItem:doneButton animated:YES];
[navigationController.navigationItem setTitle:#"Facebook"];
if (rootController) {
[self presentModalViewController:navigationController animated:YES];
}
[doneButton release];
[rootController release];
}
Unfortunately the "done" button isnt showing.. any ideas where im going wrong?
Try with below
-(IBAction)visitFacebook{
WebViewViewController *rootController = [[WebViewViewController alloc] init];
rootController.webURL = #"http://www.facebook.com/";
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:rootController];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc ] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(done:)];
rootController.navigationItem.rightBarButtonItem = anotherButton;
[navigationController.navigationItem setTitle:#"Facebook"];
if (rootController) {
[self presentModalViewController:navigationController animated:YES];
}
[doneButton release];
[rootController release];
}
Perhaps you're looking for something more like this:
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:#"Done"
style:UIBarButtonItemStyleDone target:self
action:#selector(dismissModalViewControllerAnimated:)];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Done"
style:UIBarButtonItemStylePlain target:self action:#selector(done:)];
Just this one line code displayed done button for me.

UIToolBar items added during loadView not appearing

I have a UIViewController which is created programmatically. I include the following code in loadView. The toolbar is shown, but not the button I added. Any help would be appreciated please.
[self.navigationController setToolbarHidden:NO animated:NO];
UIBarButtonItem *actionB = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:#selector(actionBTapped:)];
[self.navigationController.toolbar setItems:[NSArray arrayWithObject:actionB] animated:NO];
[actionB release];
You want to set the items on the UIViewController itself, not its navigation controller:
self.toolbarItems = [NSArray arrayWithObject:actionB];
Try this:
NSMutableArray *items = [[[NSMutableArray alloc] init] autorelease];
UIBarButtonItem *labelButton = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:nil action:nil] autorelease];
[items addObject:labelButton];
[items addObject:[[[UIBarButtonItem alloc] initWithTitle:#"YoutTitle" style:UIBarButtonItemStyleDone target:self action:#selector(actionBTapped:)] autorelease]];
[self.navigationController.toolbar setItems:items animated:NO];