Display slide-down (like in iPad split view) - objective-c

I'm curious about how to implement a drop down menu from a UIBarButtonItem like in the portrait orientation on iPad's split view. How is this done? I would have tried searching for it myself, but I don't really know exactly what I should be searching for.
Thanks in advance!

Make a new UIView class(this will be the view that will be shown in the popover)
Then in the .h of the class were you want to open this popover
#interface TheClassWerePopoverIsShown : UIViewController {
UIPopoverController *popover;
}
in the .m under the barbuttonitem click action add this
if ([popover isPopoverVisible]) {
[popover dismissPopoverAnimated:YES];
}
else {
THECLASSTOSHOW *popie = [[THECLASSTOSHOW alloc]init];
popover = [[UIPopoverController alloc]
initWithContentViewController:popie];
popover.popoverContentSize = CGSizeMake(300, 700);
[popie release];
[popover presentPopoverFromBarButtonItem:THEBARBUTTONITEM permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}

Related

Is it possible to Have a Searchbar in a UIViewController iOS 8?

I need to have a Searchbar inside a ViewController as the picture bellow. I don't wanna use UItableViewcontroller. Is it possible to have this searchbar that will help me to query in a NSMutableArray thanks to NSpredicate ?
My Goal
After Searching showing the results of the array in the View "white view".
Yes, It is possible.
You can create a search bar programmatically.
First,Add UISearchBarDelegate Protocol in .h (header) file.
#interface ViewController : UIViewController<UISearchBarDelegate>
Then, Create UISearchBar and add as subview in your view.
UISearchBar searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(30, 80, screen.size.width-60, 40)]; //give your frame x,y and width and height
searchBar.placeholder = #"Enter City Name";
searchBar.delegate = self;
//searchBar.hidden = NO;
[self.view addSubview:searchBar];
Now, Implement delegate methods
- (void)searchBarSearchButtonClicked:(UISearchBar *)aSearchBar{
//your code when search button clicked
}
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
//your code when you are entering text in search bar
}

Objective-C: How to switch the NSView by button click

I have different xib files with NSViewController attached to them. (Screenshot below)
One of xib file called StartMenuViewController which has a button. I want to click that button and change the view to DetectingUSBViewController.(Screenshot below)
The IBAction of that button is in StartMenuViewController.m file.
And I use AppController.m to control my main xib view.(NSWindow + NSView) (Screenshot below)
When the application runs, I try to initialize the StartMenuViewController fist by doing the following thing in my AppController.m file.
-(void)awakeFromNib{
[self initialize];
}
-(void) initialize
{
#autoreleasepool {
//mainViewController is a NSViewController and _mainView is a NSView which connect with Custom View in main xib
self.mainViewController = [[[StartMenuViewController alloc]initWithNibName:StartMenuView bundle:nil]autorelease];
[_mainView addSubview:[_mainViewController view]];
}
}
It works fine and it will show the StartMenuViewController.xib on the window at first, but I do not know how to change the view after clicking the button(FIND USB DRIVE). I want the current view changes to DetectingUSBViewController.xib.
Simplest way possible, assuming you have tied your USB button properly in, do the following :
- (IBAction)usbButton:(UIButton *)sender {
DetectingUSBViewController *second = [[DetectingUSBViewController alloc] initWithNibName:#"DetectingUSBView" bundle:nil];
[self presentViewController:second animated:YES completion:nil];
}
load the DetectingUSBViewController in startMenuViewController as DetectingUSBViewController* v1 = [[ViewCont1 alloc] initWithNibName:#"ViewCont1" bundle:nil]; now add or replace the view as [v1 view] in view where you want to add/replace.
You need to hook up your button to send an IBAction
You need a 'View for DetectingUSBViewController.xib'
=> one way (iOS like) is to use a ViewController. Subclass NSViewController and then alloc init a DetectingUSBViewController
Add the view. Don't present the VC (as there is no such thing in OSX)
//button click action
- (IBAction)usbButton:(UIButton *)sender {
//! Retain the VC
Self.detectingUSBViewController = [[DetectingUSBViewController alloc] initWithNibName:#"DetectingUSBView" bundle:nil];
//add the view
[_mainView addSubview:[_detectingUSBViewController view]];
}

iOS - Show UIPopoverController with TableView from Navigation Toolbarbutton

I would like to display a UIPopoverController in my iPad app when the user taps a button which is at the lower toolbar provided by my NavigationController. Is there a tutorial on how I can do this? All I found was ancient tutorials from 2010.
Thank you in advance.
-(IBAction)ButtunClickMethod:(id)sender{
//create the table view controller from nib
self.tblLisView = [[[tableListVC alloc]
initWithNibName:#"tableListVC"
bundle:[NSBundle mainBundle]] autorelease];
//set popover content size
tblLisView.contentSizeForViewInPopover = CGSizeMake(170, 170);
//create a popover controller
self.popoverController = [[[UIPopoverController alloc]
initWithContentViewController:tblLisView] autorelease];
//present the popover view non-modal with a
//refrence to the button pressed within the current view
[self.popoverController presentPopoverFromRect:popoverButton.frame
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionDown
animated:YES];
}

Need a really simple navigation controller with a table view inside a tab bar controller

I have an app with a tab bar controller (2 tabs). In one tab view controller, a button leads to an alert window. I want one button of the alert window to call a table view containing possible answers. I want that table view to have a done button and a title. I think that means a navigation controller has to be used. But most everything I can find on navigation controllers assumes a much more complicated situation. Here's part of the alert window logic:
-(void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 2) {
AnswersViewController *aVC = [[AnswersViewController alloc] init];
[self presentViewController:aVC
animated:YES
completion:NULL];
}
}
And AnswersViewController looks like this:
#interface AnswersViewController : UITableViewController
#end
#implementation AnswersViewController
- (id) init
{
self = [super initWithStyle:UITableViewStylePlain];
return self;
}
- (id) initWithStyle:(UITableViewStyle)style
{
return [self init];
}
- (void)viewDidLoad
{
[super viewDidLoad];
[[self view] setBackgroundColor:[UIColor redColor]];
}
#end
This code all works as expected (an empty red UITableView appears).
Two questions I guess: 1. Is there a simple modification to what I have that can give me a done button and title in my table view? 2. If I have to go to a navigation controller (probably), how can I make a bare-bones navigation controller with a done button and title and embed the table view within it? Oh, and I want to do this programatically. And I think I prefer the done button and title to be in the navigation bar, no tool bar desired. Thanks!
To get what you are looking for, you do need to use a UINavigationController. That will provide the UINavigationBar where you can display a title and also buttons.
To implement this with a UINavigationController, you want to do smoothing like this (assuming you are using ARC, so you don't need to worry about memory management):
-(void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 2) {
AnswersViewController *aVC = [[AnswersViewController alloc] init];
//Make our done button
//Target is this same class, tapping the button will call dismissAnswersViewController:
aVC.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(dismissAnswersViewController:)];
//Set the title of the view controller
aVC.title = #"Answers";
UINavigationController *aNavigationController = [[UINavigationController alloc] initWithRootViewController:aVC];
[self presentViewController:aNavigationController
animated:YES
completion:NULL];
}
}
Then you would also implement - (void)dismissAnswersViewController:(id)sender in the same class as the UIAlertView delegate method (based on the implementation I have here).
Hope this helps!

Resizing UINavigationController?

I want my navigationcontroller to only take half of the screen. Is this possible? In IB, when I drag, it forces me to fill up my entire screen, I can't resize it. If it's not possible, is there an alternative?
Thanks.
You can but you can only do it within iOS 5 because when I tried to do any type of direct view manipulation within a UINavigation controller than pushed or popped another view controller. The navigation controller would not display them.
Here's what you need to do.
-(void)viewDidLoad {
UINavigationController * navController = [[UINavigationController alloc] initWithRootViewController:[[UIViewController alloc] initWithNibName:nil bundle:nil]];
//Used to recieve callbacks (like shouldAutorotateToInterfaceOrientation:)
[self addChildViewController:navController];
//View manipulation
navController.view.frame = CGRectInset(navController.view.frame, 20, 20);
[self.view addSubview:navController.view];
//Calls all the standard methods (viewDidLoad,viewDidUnload,etc.)
[navController didMoveToParentViewController:self];
}
Also if your a registered developer there is a really good video on view controller containers, that might be helpful to you, from last years WWDC.
Based off what you have written I am assuming you have a #property in your header file. Something like:
#property (nonatomic, strong) IBOutlet UINavigationController *navController;
Then have this IBOutlet connected to your navigationController in your xib file. In that same xib file have a view that is connected to the file owner. Set this view's dimensions in interface builder so in your case to fill only half the screen. Then you will add the navController as this view's subview. If you also have a viewController already added in the xib file in the navController then in viewDidLoad do:
-(void)viewDidLoad {
[self addChildViewController:self.navController];
[self.view addSubview:self.navController.view];
[self.navController didMoveToParentViewController:self];
}
Otherwise if you are programmatically setting the rootviewcontroller do:
-(void)viewDidLoad {
self.navController = [[UINavigationController alloc] initWithRootViewController:yourViewController];
[self addChildViewController:self.navController];
[self.view addSubview:self.navController.view];
[self.navController didMoveToParentViewController:self];
}
You have to add self.navController as a childViewController so that it will take on the dimensions of the view it is being added into. Adding it as a subview is not enough to make the view resize. Hope this helps!