UIPickerView without IB? - iphone

I have my pickerview set up and everything works fine, but right now I have that big pickerview taking up a lot of space in Interface Builder. It's annoying having to move it around when working in Interface Builder. How do I set the pickerview and it's location without using IB?

Try this:
// Add the picker
UIDatePicker *pickerView = [[UIDatePicker alloc] initWithFrame:CGRectMake(0,100,0,0)];
[pickerView setDatePickerMode:UIDatePickerModeTime];
[self.view addSubview:pickerView];
[self.view showInView:self.view];
[self.view setBounds:CGRectMake(0,0,320, 500)];
[pickerView release];
The above code is for UIDatePicker, you may similarly implement it for other types of uipickers as well.

Related

How to use parent viewcontroller to launch uiactionsheet so that it does not get clipped?

Hey so I have a problem where I am inserting an actionsheet into a view that is inside of a scrollview in a different view controller. The actionsheet works just fine, the problem is that if i go down at all in the scrollview the actionsheet gets clipped off. I've tried several solutions with no luck. I think the problem is that the actionsheet is being inserted into the view that is placed inside the scrollview. Anyone have any idea how to launch the action sheet from the view controller that the scrollview is in instead? Here is how I am trying to do it right now:
When a button is touched it calls this method:
- (IBAction)callDP:(id)sender {
UIButton *selectedButton = (UIButton *)sender;
actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
[actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
CGRect pickerFrame = CGRectMake(0, 40, 0, 0);
datePickerView = [[UIDatePicker alloc] initWithFrame:pickerFrame];
datePickerView.tag = 10;
[datePickerView addTarget:self action:#selector(changeDate:) forControlEvents:UIControlEventValueChanged];
[actionSheet addSubview:datePickerView];
[actionSheet setTitle:#"Start Date"];
UISegmentedControl *closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:#"Done"]];
closeButton.momentary = YES;
closeButton.frame = CGRectMake(260, 7.0f, 50.0f, 30.0f);
closeButton.segmentedControlStyle = UISegmentedControlStyleBar;
closeButton.tintColor = [UIColor blackColor];
[closeButton addTarget:self action:#selector(dismissActionSheet) forControlEvents:UIControlEventValueChanged];
[actionSheet addSubview:closeButton];
datePickerView.datePickerMode = UIDatePickerModeDate;
//THIS IS THE PART THAT I THINK I AM HAVING THE PROBLEM WITH
// [actionSheet showFromToolbar:self.toolbarItems];
//[actionSheet showFromTabBar:self.parentViewController.tabBarController.tabBar];
[actionSheet showInView:self.parentViewController.view];
}
Here is where I insert the view into the scrollview. I have it set up so that I am using a view from a different uiviewcontroller class to control everything. The reason I do that is so that i can have the scrollable part, but be able to visually create everything that I need without having to do it programmatically....I apologize if that is kind of confusing. I can clarify if needs be...but here it is. The viewcontroller class that contains the view I want to put into the scroll view is called registration page. Its inside of registrationPage that it calls the actionSheet. Let me know what you think...
registrationPage = [[RegistrationPageToInsertViewController alloc]init];
viewToInsert = registrationPage.view;
[scrollView addSubview:viewToInsert];
[scrollView setContentSize:viewToInsert.frame.size];
// [scrollView sendSubviewToBack:imgView];
//scrollView.contentSize=CGSizeMake(320,416);
[self.view bringSubviewToFront:scrollView];
Here are a couple of screenshots to help you see what I'm talking about:
instead of this line
[actionSheet showInView:self.parentViewController.view];
try like this.
[actionSheet showInView:self.view];
Just do as i say.You have a nib or viewController. Whatever. Just add a scrollView as subView to what you have.Then you just add all your textfields, labels and all will be subview of scrollView.this is the correct way to do that.if you present action sheet,you have to show that in self.View only.

how to use uidatepickerview in timer?

I want to use timer with UIDatePickerView?
What I want to write code?
Pls help me.
Change mode of UIDatePickerView to timer....Do as i had done below
// Add the picker
UIDatePicker *pickerView = [[UIDatePicker alloc] initWithFrame:CGRectMake(0,100,0,0)];
[pickerView setDatePickerMode:UIDatePickerModeCountDownTimer];
[self.view addSubview:pickerView];
[self.view setBounds:CGRectMake(0,0,320, 500)];
[pickerView release];
Good Luck!

How to add custom toolbar to UIPickerView Control?

I want to add a custom toolbar at the top of the UIPickerView control. I did this on Keyboard but not able to do with UIPickerView.
Nishant
make a toolBar by IBoutlet and set its y coordinate to 100 or more than view's height. then in particular action change y coordinate accordingly. this is done via animation.
You also use this with keyboard .
by using
-(void) keyboardWillShow:(NSNotification *) note
and
-(void) keyboardWillHide:(NSNotification *) note
UIToolBar *toolBar= [[UIToolbar alloc] initWithFrame:CGRectMake(0,0,320,44)];
[toolBar setBarStyle:UIBarStyleBlackOpaque];
UIBarButtonItem *barButtonDone = [[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStyleBordered target:self action:#selector(doneBtnTapped:)];
toolBar.items = [[NSArray alloc] initWithObjects:barButtonDone,nil];
barButtonDone.tintColor=[UIColor blackColor];
[popoverView addSubview:toolBar];
[popoverView addSubview:pickerView];
I created a custom view and added a done button on that along with picker view.
Thanks

iPhone, how can I make the font size smaller in actionsheet buttons?

I want to make the font size smaller for the text in the buttons of my action sheet as my text is longer than the width.
How can I make the font size smaller?
I'm guessing as you can add things like picker views to action sheets that I may need to add my own buttons to the action sheet, if so how ?
I need an example.
EDIT: I've made a little progress, but the actual button isn't shown, but you can see it working with the change in the button text when you click on it.
UIActionSheet *menu = [[UIActionSheet alloc] initWithTitle:#"Date Picker"
delegate:self
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:#"OK",nil];
CGRect frame = CGRectMake(100.0, 80.0, 100.0, 40.0);
UIButton *pickerView = [[UIButton alloc] initWithFrame:frame];
[pickerView setTitle:#"FRED" forState:UIControlStateNormal];
[pickerView setTitle:#"Go Back" forState:UIControlStateNormal];
[pickerView setTitleColor:[UIColor blackColor] forState:UIControlEventTouchDown];
pickerView.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
pickerView.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
//[removeButton addTarget:self action:#selector(buttonEvent:) forControlEvents:UIControlEventTouchDown];
[pickerView setBackgroundColor:[UIColor blueColor]];
[menu addSubview:pickerView];
[menu showInView:self.view];
[pickerView release];
[menu release];
You may be able to do this directly, but note that actionsheet buttons are not standard UIButtons, but are their own special private control subclass. For this reason, when I need to customize an action sheet, I add my own custom subview to the UIActionSheet and then resize the sheet to match. The downside is that to match the look and feel I need a custom button image background. I adapted (but didn't check in a compiler) this example from Fitting a UIDatePicker into a UIActionSheet. Instead of the picker, just add your xib created UIView.
UIDatePicker *pickerView = [[UIDatePicker alloc] init];
pickerView.datePickerMode = UIDatePickerModeDate;
[myUIActionSheet addSubview:pickerView];
[myUIActionSheet showInView:self.view];
[myUIActionSheet setBounds:CGRectMake(0,0,320, myUIActionSheet.bounds.size.height + pickerView.bounds.size.height)];
CGRect pickerRect = pickerView.bounds;
pickerRect.origin.y = - pickerView.bounds.size.height; //you may need to change this offset
pickerView.bounds = pickerRect;
[pickerView release];
[myUIActionSheet release];
EDIT: To include a subview from a xib, add a UIView to the top level of the xib and wire it to a UIView property in your controller just as if it was a normal interface component. Within your controller, it will now be available.
You can also wire button actions normally instead of using the action sheet callback. You will need to close the action sheet after the press with something like [myUIActionSheet.dismissWithClickedButtonIndex:0 animated:YES]; I think that dismissing it as if it is modal will also work, IIRC.

UIDatePicker in UIActionSheet layering problem

Someone posted this code in another question to put a UIDatePicker in a UIAlertSheet:
UIActionSheet *menu = [[UIActionSheet alloc] initWithTitle:#"Date Picker"
delegate:self
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:nil];
// Add the picker
UIDatePicker *pickerView = [[UIDatePicker alloc] init];
pickerView.datePickerMode = UIDatePickerModeDate;
[menu addSubview:pickerView];
[menu showInView:self.view];
[menu setBounds:CGRectMake(0,0,320, 500)];
CGRect pickerRect = pickerView.bounds;
pickerRect.origin.y = -100;
pickerView.bounds = pickerRect;
[pickerView release];
[menu release];
I have modified it to this:
UIActionSheet *menu = [[UIActionSheet alloc] initWithTitle:nil
delegate:nil
cancelButtonTitle:#"Done"
destructiveButtonTitle:nil
otherButtonTitles:nil];
// Add the picker
UIDatePicker *pickerView = [[UIDatePicker alloc] init];
pickerView.datePickerMode = UIDatePickerModeCountDownTimer;
[menu addSubview:pickerView];
[menu showInView:self.navigationController.tabBarController.view];
[menu setBounds:CGRectMake(0,0,320, 516)];
[pickerView setFrame:CGRectMake(0, 85, 320, 216)];
This yields an alert sheet correctly positioned (ignore the nil delegate; this is just for display purposes). The problem lies here:
(source: hosting-wizards.com)
As you can see, above the minute scroller, there is a layering problem. I have not confirmed if this occurs on the device. Any ideas as to why this is happening?
Another question: Why does the setBounds selector on the UIActionSheet have the Y-size set at such a high value for correct display? Setting the Y-size to 480 should create a full screen display, should it not? Setting this value to zero makes it nearly invisible.
SIDE NOTE: The code pasted above does not position the UIDatePicker in the same place that it is in the picture, but the layering problem occurs no matter where the UIDatePicker is placed. The layering issue is only on the top, not on the bottom.
You will avoid the display problem and get a much better looking result if you simply put the UIDatePicker in a new view and implement the slide-up-from-the-bottom behavior yourself. It's not that hard.
Create a UIView containing a date picker and a toolbar with a Done button on it. (In code or use Interface Builder, doesn't matter.)
Add the popup view as a subview of your main view.
Set the popup view's frame so that it is off the bottom of the screen: frame.origin.y = CGRectGetMaxY(mainView.bounds)
Call [UIView beginAnimations:nil context:nil]
Set the frame to where it should be: frame.origin.y -= CGRectGetHeight(popupView.bounds)
Call [UIView commitAnimations]
(Note that the frame setting stuff is pseudocode.)
That's it. Do it in reverse when you need to dismiss the view. I think it will be worth the trouble; sticking a date picker in a UIActionSheet looks incredibly tacky.
Using benzado's steps above, here's roughly what your code would look like if you use a UIView instead of an ActionSheet:
int height = 255;
//create new view
UIView * newView = [[UIView alloc] initWithFrame:CGRectMake(0, 200, 320, height)];
newView.backgroundColor = [UIColor colorWithWhite:1 alpha:1];
//add toolbar
UIToolbar * toolbar = [[UIToolbar alloc] initWithFrame: CGRectMake(0, 0, 320, 40)];
toolbar.barStyle = UIBarStyleBlack;
//add button
UIBarButtonItem *infoButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStyleDone target:self action:nil];
toolbar.items = [NSArray arrayWithObjects:infoButtonItem, nil];
//add date picker
UIDatePicker *datePicker = [[UIDatePicker alloc] init];
datePicker.datePickerMode = UIDatePickerModeDate;
datePicker.hidden = NO;
datePicker.date = [NSDate date];
datePicker.frame = CGRectMake(0, 40, 320, 250);
[datePicker addTarget:self action:nil/*#selector(changeDateInLabel:)*/ forControlEvents:UIControlEventValueChanged];
[newView addSubview:datePicker];
//add popup view
[newView addSubview:toolbar];
[self.view addSubview:newView];
//animate it onto the screen
CGRect temp = newView.frame;
temp.origin.y = CGRectGetMaxY(self.view.bounds);
newView.frame = temp;
[UIView beginAnimations:nil context:nil];
temp.origin.y -= height;
newView.frame = temp;
[UIView commitAnimations];
I agree, it looks a lot better than using ActionSheets which are designed to display buttons, not pickers. You still need to add your own action handlers on the "Done" button and when the user changes the picker but this should get you started.
Do you really need to put your picker on the UIActionSheet? Why don't you use an inputView property from UIResponder class? It shows UIDatePicker (or any other view) automatically when your control (e.g. UITextField) becomes a first responder.
An example of using inputView with source code can be found here: Working with pickers.
try this
[menu sendSubviewToBack: pickerView];
and also move your picker 10 pixels down as you see in the screenshot it is not aliged to the bottom, might be
[pickerView setFrame:CGRectMake(0, 100, 320, 216)];
You might find this thread helpful for an alternative approach: http://discussions.apple.com/message.jspa?messageID=7923095