displaying ABUnknownPersonViewController with dismiss button - ios

The Application I am working on has dialogs/UI entirely done by hand/code.
Presently it only has Views/ViewControllers and no NavigationControllers yet.
"Navigation" is done by displaying controllers on top of another, and/or hiding views.
I am trying to display a ABUnknownPersonViewController.
I manage to do it, but I cannot dismiss it; I tried to add a button but it did not even showed up. I am using the following code, but I do not see any dismiss/cancel in my UIViewController, how should I modify it ?
- (void)AddCallerToAdressBook
{
ABUnknownPersonViewController *controller = [[ABUnknownPersonViewController alloc] init];
controller.allowsAddingToAddressBook = YES;
UINavigationController *newNavigationController = [[UINavigationController alloc] initWithRootViewController:controller ];
newNavigationController.title=#"AddCaller";
UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
target:self action:#selector(dismissed)];
[newNavigationController.navigationItem setLeftBarButtonItem:leftBarButton];
[self presentModalViewController:newNavigationController animated:YES];
}

Related

My Custom UINavigationController won't show right button

I have a "LoginViewController" which presents a new Controller which is a subclass of UINavigationcontroller when clicking a button:
MPNavigationViewController *controller = [[MPNavigationViewController alloc] initWithRootViewController:[[MPQuestionFirstViewController alloc] init]];
[self presentViewController: controller animated:YES completion:nil];
"MPNavigationViewController" subclass UINavigationController and uses "REMenu" to have a sliding-from-top menu ("Link") and on viewDidLoad I try to add a right button to open it:
- (void)viewDidLoad
{
[super viewDidLoad];
UIBarButtonItem *toggleMenuButton = [[UIBarButtonItem alloc] initWithTitle:#"Show" style:UIBarButtonItemStylePlain target:self action:#selector(toggleMenu:)];
self.navigationItem.rightBarButtonItem = toggleMenuButton;
[self initMenu];
}
It doesn't show any button on the navigation bar. Why could it be?
If I try to add the button from one of the "viewControllers" that will handle sections on the menu. It shows the button, but it doesn't paint it at all.
Thanks.
You are using subclass of UINavigationcontroller which is not actually view controller.
There is only one solution, You need to create your custom button and add it to UINavigationbar as a subview..
Use this hope it will help.
UIBarButtonItem *doneButton =[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(goToDoneButtonAction)];
self.navigationItem.rightBarButtonItem = doneButton;

Add a UISegmentedControl as the titleView in a UINavigationController

I'm trying to add a UISegmentedControl programmatically as the titleView in a UINavigationController. But it did not show up. Upon further investigation, I found out that the titleView property is ignored if the leftBarButtonItem is not set to nil according to the Apple docs. So I set it to nil but still the segmented control does not show up!
Below is my code.
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.leftBarButtonItem = nil;
UISegmentedControl *statFilter = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:#"Personnal", #"Department", #"Company", nil]];
[statFilter setSegmentedControlStyle:UISegmentedControlStyleBar];
self.navigationItem.titleView = statFilter;
}
What should I do? What am I missing here?
Thanks.
EDIT :
Thanks for all the responses. However, none of them worked for me so I took a deeper look. It must be the way I have laid out my application. I probably should have mentioned that earlier. Apologies.
What I'm creating is a Storyboard app. The main controller is a UITabBarController. When a certain tab is clicked, I want to show a UITableViewController. Since you cannot display a Navigation bar in a UITableViewController, I found out that I must add a UINavigationController and implement a UITableViewController inside it. This is how my app basically look like,
That Dashboard View Controller is connected to a class inheriting from UINavigationController
class. Maybe this is where the issue resides?
Because I just created a small test app, put a UIViewController, embedded in a UINavigationController and wrote the code I have posted in my original question above inside the UIViewController's ViewDidLoad and the segmented control shows up just fine. Now I don't know how to do the same in my main app because there's no other view controllers or anything connected to the navigation controller.
Sorry if I made things even worse. Please comment if anything is unclear.
I guess you have to make the UISegmentedControll a UIBarButtonItem:
UPDATE:
This Code makes the SegmentedControll centered:
- (void)viewDidLoad{
self.title = nil;
UISegmentedControl *statFilter = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:#"Personnal", #"Department", #"Company", nil]];
[statFilter setSegmentedControlStyle:UISegmentedControlStyleBar];
NSMutableArray *buttonArray = [[NSMutableArray alloc] init];
UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[buttonArray addObject:flexibleSpace];
[buttonArray addObject:[[UIBarButtonItem alloc] initWithCustomView:statFilter]];
[buttonArray addObject:flexibleSpace];
self.navigationItem.leftBarButtonItems = buttonArray;
[buttonArray release];
}
I hope this helps you out!
Use this i hope this will help you
- (void)viewDidLoad
{
[super viewDidLoad];
UIView * navview = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 160,80)];
self.navigationItem.leftBarButtonItem = nil;
UISegmentedControl *statFilter = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:#"Personnal", #"Department", #"Company", nil]];
[navview addSubview:statFilter];
self.navigationItem.titleView = navview;
}

barbuttonitem now showing up, presentmodalviewcontroller

I have a need to show PDF files modally, when my user clicks on some buttons I load the PDF into a class and present it modally, now I need to provide the user with ab utton on the modalVC to close or go back so I added RightBarButtonItem, the prioblem is that its not showing up? This is code the load
the PDF
[self.pdfViewController.view addSubview:pdfViewer];
UINavigationController *navController = [[UINavigationController alloc]
initWithRootViewController:pdfViewController];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(shutDown)];
[self presentModalViewController:navController animated:YES];
What am I doing wrong here?
You are attaching the bar button to the wrong viewController. It should belong to the viewController inside your presented navigationController. This is pdfViewController.
Additionally...
assuming self.pdfViewController is the same object as pdfViewController, it's best if you refer to it consistently
presentModalViewController is deprecated since at least ios5.0, you should be using presentViewController:animated:completion
So your code should look something like this
[self.pdfViewController.view addSubview:pdfViewer];
UINavigationController *navController =
[[UINavigationController alloc] initWithRootViewController:self.pdfViewController];
self.pdfViewController.navigationItem.rightBarButtonItem =
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self
action:#selector(shutDown)];
[self presentViewController:navController
animated:YES
completion:nil];
Your shutDown method will be sent to the presenting ViewController, not to your presented pdfViewController... so that's where you need to implement it.

Button not being added on navigation controller

- (void)viewDidLoad
{
[super viewDidLoad];
ac = [[AddContacts alloc]init];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:ac];
[self.view addSubview:navigationController.view];
UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithTitle:#"Show" style:UIBarButtonItemStylePlain target:self action:#selector(refreshPropertyList:)];
self.navigationController.navigationItem.rightBarButtonItem = anotherButton;
}
Why is my button not being added on the navigation controller?
The UIBarButtonItems are not controlled by the navigation controller, but by each of the view controllers it contains - each UIViewController can have different buttons. Try:
ac = [[AddContacts alloc]init];
UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithTitle:#"Show" style:UIBarButtonItemStylePlain target:self action:#selector(refreshPropertyList:)];
ac.navigationItem.rightBarButtonItem = anotherButton;
Then initialize the UINavigationController as you have been doing.
There are a few things here that could be causing issues.
Probably what the main issue is this line:
self.navigationController.navigationItem.rightBarButtonItem = anotherButton;
I am pretty sure that what you want to be setting is the right bar button item on the ac view controller's navigation item.
ac.navigationItem.rightBarButtonItem = anotherButton
A few other things though:
Don't have two letter variable names. "ac" is very ambiguous, "addContacts" would provide more information, "addContactsViewController" would provide even more
Are you implementing your own navigationController property in a UIViewController subclass? This is not recommended as it is overriding the navigationController property that UIViewController already has. Give it a different name.
Is the -viewDidLoad method on the parent view controller the place to be assigning the right bar button of your AddContacts object? Consider instead putting the code to set the bar button in the implementation of AddContacts instead.

Customising the titleView in a UIPopoverController

On an iPad application, I want to be able to have multiple buttons in the top bar of a popover. I'm launching it like this:
UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:vc];
pop = [[UIPopoverController alloc] initWithContentViewController:nc];
[pop presentPopoverFromRect:CGRectInset([tableView rectForRowAtIndexPath:indexPath], 10, 10)
inView:tableView
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
pop.delegate = self;
[nc release];
In the viewDidLoad I want to set the titleView to contain multiple UIBarButtonItems. This is ok on a normal UINavigationController but I need to be able to do this and keep the custom style of the navigation bar on the popover.
I have tried setting the rightBarButtonItem to have a toolbar that contains buttons, but they take the format of the toolbar, which itself will not take the format/style of the popover.
It's a bit hackish, but you can create a truly transparent UIToolbar by subclassing and overriding drawRect: with an empty method. This will still draw the UIBarButtonItems on the toolbar, but not the toolbar itself.
Then, as Nils describes, you can create a bar button item out of the custom toolbar instance and add it as the rightBarButtonItem.
Why you don't use a custom UIView with a UIToolbar or UINavigationBar instead of UIPopoverController?
The effect, is the same!
First you need to set a delegate to the UINavigationController and then implement the nabigationController:willShowViewController:animated: method. In this method you can adjust the left and right bar button items.
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
viewController.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc]
initWithTitle:#"Image Source"
style:UIBarButtonItemStylePlain
target:self
action:#selector(cancelPressed)] autorelease];
}
I had the same problem, and here is what I did :
UIBarButtonItem *uiBarRefresh = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:#selector(forcerefresh)];
UIBarButtonItem *uiBarCurl = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemPageCurl target:self action:#selector(showlist)];
UIToolbar_transparent* tools = [[UIToolbar_transparent alloc] initWithFrame:CGRectMake(0, 0, 133, 44.01)];
tools.barStyle = UIBarStyleBlackTranslucent;
tools.translucent = YES;
tools.backgroundColor = [UIColor clearColor];
tools.tintColor = self.navigationController.navigationBar.tintColor;
[tools setItems:[NSArray arrayWithObjects:flexItem,uiBarRefresh,uiBarCurl,nil] animated:NO];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:tools];
This way you avoid the dull grey background, the pesky white underline, and you can completely customize how you want them positioned, by using fixed or flex spacings.
Best of luck with your project :]

Resources