how to show UIDatePicker in iPad? - iphone

I implemented the UIDatePicker in iPhone and it's working correctly.
The same code I tried with iPad but it is not showing me UIDatePicker.
I read somewhere that I need to show it with UIPopOverController and find this
link but it is not showing the proper code.
I can't make a separate XIB for that. I have to implement it by code itself.
Mine is IOS5.
I use the following code:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if (textField.tag == 1)
{
[self showDatePickerIniPad];
}
}
-(void)showDatePickerIniPad
{
UIViewController *popoverContent = [[UIViewController alloc] init];
UIView *popoverView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
popoverView.backgroundColor = [UIColor whiteColor];
datePicker.frame = CGRectMake(0, 0, 320, 300);
[popoverView addSubview:datePicker];
popoverContent.view = popoverView;
popoverContent.contentSizeForViewInPopover = CGSizeMake(320, 244);
UIPopoverController *popoverController = [[UIPopoverController alloc] initWithContentViewController:popoverContent];
[popoverController presentPopoverFromRect:CGRectZero inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
txt_FromDate.inputView = datePicker;
}

Since your question is more on the lines of learning rather than a specific doubt or question, i am posting tutorial links. These 2 tutorials should solve your problem(s) -
UIPickerView Basics
UIDateView Components
UPDATE: Also check these highly relevant links from stackoverlfow itself -
UIDatePicker in UIActionSheet on iPad
https://stackoverflow.com/questions/5830585/uidatepicker-inside-uiactionsheet-ipad
blank UIDatePicker on iPad in mode UIDatePickerModeDateAndTime

If you want the popover to show when you click on the textfield you are doing in the right method, - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField.The problem is this is a Delegate Method for the textField so you should include the UITextFieldDelegate in you interface in the .h file
#interface myViewController : UIViewController<UITextFieldDelegate>{
and set the delegate of you text field like this
self.yourTextField.delegate = self;
That should solve your problem.

Related

How to design an iOS program with custom inputViews

Note, this is a DESIGN question, NOT a functionality question. I already know how to implement the following, I'm just trying to figure out the best way to design it.
I have an iOS app where a few UIViewControllers throughout the app have UITextFields with UIDatePicker input views. The code for this is below:
- (void) viewDidLoad
{
self.dateField.inputView = [self createDatePicker];
}
- (UIView *) createDatePicker
{
UIView *pickerView = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height, self.view.frame.size.width, TOOLBAR_HEIGHT + KEYBOARD_HEIGHT)];
UIDatePicker *picker = [[UIDatePicker alloc] init];
[picker sizeToFit];
picker.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
picker.datePickerMode = UIDatePickerModeDate;
[picker addTarget:self action:#selector(updateDateField:) forControlEvents:UIControlEventValueChanged];
[pickerView addSubview:picker];
// Create done button
UIToolbar* toolBar = [[UIToolbar alloc] init];
toolBar.barStyle = UIBarStyleBlackTranslucent;
toolBar.translucent = YES;
toolBar.tintColor = nil;
[toolBar sizeToFit];
UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem* doneButton = [[UIBarButtonItem alloc] initWithTitle:#"Done"
style:UIBarButtonItemStyleDone target:self
action:#selector(doneUsingPicker)];
[toolBar setItems:[NSArray arrayWithObjects:flexibleSpace, doneButton, nil]];
[pickerView addSubview:toolBar];
picker.frame = CGRectMake(0, toolBar.frame.size.height, self.view.frame.size.width, pickerView.frame.size.height - TOOLBAR_HEIGHT);
toolBar.frame = CGRectMake(0, 0, self.view.frame.size.width, TOOLBAR_HEIGHT);
return pickerView;
}
- (void) doneUsingPicker
{
[self.dateField resignFirstResponder];
}
- (void) updateDateField: (UIDatePicker *) datePicker
{
self.dateField.text = [self.formatter stringFromDate:datePicker.date];
}
The problem is, I keep on having to paste this code throughout the app in different classes that have UITextFields with UIDatePicker inputviews. What would be the best way to design this so as to minimize duplicated code. I've thought about having a UIDatePickerableViewController superclass that contains this code, but this doesn't seem extensible. For instance, what if I soon have other types of input views that could be attached to text fields. How should I design this?
You can refactor code/methods shared between the classes in a common superclass, and inherit subclasses inside which you only modify the parts that are needed to be different.
Or, if you approach the problem from a different point of view: create a custom InputWiewWithDatePicker class and move the (self-)configuration and -initialization code inside the - init method of that class. This way you don't have to paste all this everywhere, and only a single line will be duplicated:
customControl = [[InputViewWithDatePicker alloc] init];
My first thought would be to create a new UIView subclass that contains a date picker and text field with the layout you desire. This can be done with a nib or in code. Anyplace you want to add this new kind of view, it's either a one-liner in viewDidLoad, or paint a UIView into a nib and change it's class to your new view class.
Subclass your desired layout, then when you allocate it, it will come with all the options you have defined.

how to show the UIPopoverController when button clicked in iPhone

I have a problem with UIPopoverController. On button press I need to display the UIPopoverController. This is the code.
-(IBAction)hintButton:(id)sender{
CGRect contentRect = CGRectMake(0, 0, 200, 40);
UIViewController* popoverContent = [[UIViewController alloc] init];
popoverContent.view = hintB;
popoverContent.contentSizeForViewInPopover = contentRect.size;
if(popoverController == nil){ //make sure popover isn't displayed more than once in the view
popoverController = [[UIPopoverController alloc]initWithContentViewController:popoverContent];
[popoverController presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
popoverController.delegate = self;
}
}
When I press the button application crashes.
UIPopoverController cannot be used for iPhone applications. It's only for iPad apps.
However, you can have this functionality by creating custom UIPopoverController. You can find a custom UIPopoverController sample here.
Let me know if you have any similar questions.
Thanks,
MinuMaster

UISearchview responding to actions - TableSearch sample iphone

I would like to modify the tableSearch sample code, so that there is no nib for the tableview. As it is already a tableviewcontroller subclass, I can call the tableview like this:
MainViewController *mainViewController = [[MainViewController alloc] initWithStyle:UITableViewStylePlain];//NibName:#"MainView" bundle:nil];
Without the nib, I would like to add the search bar manually. I tried below:
UISearchBar* searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0,10, 320, 41)];
[searchBar setFrame:CGRectMake(0,10, 320, 41)];
searchBar.delegate = self;
searchBar.tintColor = [UIColor redColor];
[searchBar sizeToFit];
self.tableView.tableHeaderView = searchBar;
The searchbar does appear ok. Now I would like the search action of this searchbar to point to the methods that I already have in my code, i.e searchDisplayController delegate methods.
Is there anyway I can point the self.searchDisplayController's Search bar property to my custom searchbar, so they do the same actions? Thanks in advance...
You have to use the initWithSearchBar:contentsViewController method of UISearchDisplayController. This way, your custom search bar will be associated with the controller. Also you have to set the contentsViewController in this method.
Hm.. this is code from one of my VC. Also set UISearchBarDelegate,UISearchDisplayDelegate
_listContainer = [[UITableView alloc] init];
_listContainer.delegate = self;
_listContainer.dataSource = self;
_listContainer.separatorStyle = UITableViewCellSelectionStyleNone;
_data = [[NSArray alloc] init];
And then
_searchBar = [[NL_CustomSearchBar alloc] initWithFrame:CGRectMake(4.0f, 0, 150, 46)];
_searchBar.delegate = self;
_listContainer.tableHeaderView = _searchBar;

Why aren't UITabBarItems' titles displaying?

First time poster. Thanks in advance.
I want to use a UITabBar in my iPhone app. I have been led to believe that I cannot use UITabViewController because I would be nesting it inside a UINavigationController. I read that you can do this, but you have to use UITabBar instead of the full-fledged controller. I can make this work with Interface Builder, but I've got a lot of redundant XIBs and I'd rather do it in code anyway. I want to know how the magic works, as it were.
Here's the .m of my controller:
#import "DumbViewController.h"
#implementation DumbViewController
-(void) loadView {
UIView *view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 100, 150, 30)];
[label setText:#"hello world"];
[view addSubview:label];
[label release];
UITabBar *tb = [[UITabBar alloc] initWithFrame:CGRectMake(0, [view frame].size.height - 64, [view frame].size.width, 64)];
[tb setDelegate:self];
[tb setItems:[NSArray arrayWithObject:[[[UITabBarItem alloc] initWithTitle:#"bob" image:[UIImage imageNamed:#"21-skull.png"] tag:0] autorelease]]];
[view addSubview:tb];
[tb release];
[self setView:view];
[view release];
}
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
NSLog(#"selected item %#", item);
}
#end
When I run the app, I get my label and a tab bar at the bottom. I get the goofy little skull icon, but "bob" never displays. Clicking the skull works as you would expect.
Why isn't that title showing?
Thanks!
Try the following code....
UITabBarItem *someTabBarItem = [[UITabBarItem alloc] initWithTitle:#"bob" image:[UIImage imageNamed:#"21-skull.png"] tag:0];
NSArray *tabBarItems = [[NSArray alloc] initWithObjects:someTabBarItem,nil];
[tabBar setItems:tabBarItems animated:NO];
[tabBar setSelectedItem:someTabBarItem];
[tabBarItems release];
[someTabBarItem release];
The problem was that I was setting the frame of the UITabBar outside the frame of the parent view.
I added this line after the UITabBar alloc line.
[tb setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin];
The advantage here is that even if I rotate or if the app is running on an iPad, the tab bar stays at the bottom of the screen.
I also changed from a 64 pixel height to a 44 pixel height because it looks much more like how the XIB did originally.

Creating a UITableViewController programmatically

This is what I tried. Nothing appears on the screen and none of the UITableView methods that you are supposed to implement are getting called.
-(void)loadView
{
UIView *view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
self.view = view;
[view release];
}
- (void)viewDidLoad
{
[super viewDidLoad];
UITableViewController *TVC = [[[UITableViewController alloc]
initWithStyle:UITableViewStylePlain] autorelease];
CGRect cgRct = CGRectMake(0, 10, 320, 100);
UIView *newView = [[UIView alloc] initWithFrame:cgRct];
TVC.view = newView;
[newView release];
[self.view addSubview:TVC.view];
}
I've looked for good examples and tutorials on doing this programmatically but there are none.
What I am trying to achieve is a Table that doenst take up my who screen. Maybe 3/4 of my screen would be good.
Many Thanks
Code
The problem is that you're creating a UITableViewController, which is a UIViewController, and will expect to be on the nav stack. What you want to do is create a UITableView, which is a UIView. You are also not setting the table's delegate and data source, which you will need to do to get calbacks.
Your viewDidLoad should look something like this
- (void)viewDidLoad
{
[super viewDidLoad];
UITableView *table = [[[UITableView alloc] initWithStyle:UITableViewStylePlain] autorelease];
table.dataSource = self;
table.delegate = self;
table.frame = CGRectMake(0, 10, 320, 100);
[self.view addSubview:table];
}
(Note that if you're going to need access to the table outside of the callbacks, you should save it in an ivar rather than declaring it locally, and should retain it. Let me know if you need a few more lines of code to show you what I mean)
Make sure you set the delegate of TVC, with
TVC.delegate = self;
That's the reason why none of those methods are getting called. Also, make sure your class implements the UITableViewDelegate protocol by changing your interface declaration to
#interface YourViewController : UIViewController <UITableViewDelegate> {
//declare variables here
}
Also, equally important, don't set TVC.view, as this already happens when you initialize the view controller. You're just setting it to a blank view, which is why you're not seeing anything.
iOS7 seems to like this way of init'ing the tableview:
//make tableview
UITableView *table = [[UITableView alloc] initWithFrame:CGRectMake(0, 81, 200, 200) style:UITableViewStylePlain];
table.dataSource = self;
table.delegate = self;
[self.dataView addSubview:table];
try that out. Hope it helps someone.
UIView *newView = [[UIView alloc] initWithFrame:cgRct];
TVC.view = newView;
I'll give you a hint. Here you are setting the view of the UITableViewController to an EMPTY VIEW...