UIPIckerView in UIActionSheet in a TabBarController - ios5

I am trying to create a UIPickerView inside of a UIActionSheet with a "done" button in a bar on the action sheet above the picker and I am trying to display the action sheet from the tab bar.
here is the code I am using to create my picker:
_beepPicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, 320, 216)];
_beepPicker.showsSelectionIndicator = YES;
_beepPicker.dataSource = self;
_beepPicker.delegate = self;
here is the code I am using to create my action sheet (with my "done" button)
_beepPickerActionSheet = [[UIActionSheet alloc]
initWithTitle:nil
delegate:nil
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
UISegmentedControl *doneButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:#"Done"]];
doneButton.momentary = YES;
doneButton.frame = CGRectMake(260, 7.0f, 50.0f, 30.0f);
doneButton.segmentedControlStyle = UISegmentedControlStyleBar;
doneButton.tintColor = [UIColor blackColor];
[doneButton addTarget:self action:#selector(dismissBeepPicker:) forControlEvents:UIControlEventValueChanged];
[_beepPickerActionSheet addSubview:doneButton];
and here is how I'm adding my "done" button:
[_beepPickerActionSheet addSubview:_beepPicker];
and finally, here is how I am trying to display the action sheet:
[_beepPickerActionSheet showFromTabBar:self.tabBarController.tabBar];
The Action Sheet does display... but, I can only see about 30 pixels of the top of the Action sheet????????
Any idea what I may be doing incorrectly?
When I check my debugger... here is what I see for the frame of the Action Sheet
UIActionSheet: 0x6b304f0; frame = (0 455; 320 25);
Screenshot before:
Screenshot after:

Ok... I know what I was doing incorrect... I had to add
[_beepPickerActionSheet setBounds:CGRectMake(0,0,320,485)];
After I added it to the view.

Related

Adding UIPickerView to UIActionSheet (buttons at the bottom)

I want to
show a picker view (sliding up)
deactivate the background while it's visible
show (UIActionSheet) buttons at the bottom (not at the top)
It seems to me an easy solution at first since you can find code for adding a picker view to an action sheet everywhere in the web but all solutions position the buttons at top and I need to put the buttons at the bottom of the action sheet (alignment?).
Is this possible? I guess it is if I look at: Example
Regards
Jeven
EDITED (my solution based on coded dads solution)
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil
delegate:nil
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
[actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
CGRect pickerFrame = CGRectMake(0, 0, 0, 0);
UIPickerView *pickerView = [[UIPickerView alloc]initWithFrame:pickerFrame];
pickerView.showsSelectionIndicator = YES;
pickerView.dataSource = self;
pickerView.delegate = self;
UISegmentedControl *backButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:#"Back", nil]] ;
[backButton addTarget:self action:#selector(someFunction:) forControlEvents:UIControlEventValueChanged];
backButton.tintColor = [UIColor colorWithRed:0.10 green:0.20 blue:0.52 alpha:0.5];
backButton.segmentedControlStyle = UISegmentedControlStyleBar;
[backButton addTarget:self action:#selector(btnActionCancelClicked:) forControlEvents:UIControlEventAllEvents];
backButton.frame = CGRectMake(20.0, 220.0, 280.0, 40.0);
UISegmentedControl *acceptButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:#"Accept", nil]] ;
[acceptButton addTarget:self action:#selector(anotherFunction:) forControlEvents:UIControlEventValueChanged];
acceptButton.tintColor = [UIColor colorWithRed:0.10 green:0.20 blue:0.52 alpha:0.5];
acceptButton.segmentedControlStyle = UISegmentedControlStyleBar;
acceptButton.frame = CGRectMake(20.0, 280.0, 280.0, 40.0);
[actionSheet addSubview:pickerView];
[actionSheet addSubview:backButton];
[actionSheet addSubview:acceptButton];
[actionSheet showInView:[[UIApplication sharedApplication] keyWindow]];
[actionSheet setFrame:CGRectMake(0,150,320, 400)];
... then just apply iPhone 5/iphone 4 dependent view size constraints
EDITED 2:
Just another hint! Originally I wanted to use the standard Actionsheet, but didn't want to place the pickerview at the bottom. I didn't find a way how to move the buttons. Obviously it s really simple: Add UIView as subView on UIActionSheet Good to know ;)
You have 2 possibilites:
Option1: the big tweak.
ActionSheet shows the buttons with the order defined during init. So place some dummy buttons at the top and Cancel will be the only visible, all other buttons will be hidden by the pickerview. Code (warning-tweak at otherButtonTitles):
UIActionSheet *menu = [[UIActionSheet alloc] initWithTitle:#"Actionsheet"
delegate:self
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:#"1", #"2", #"3", #"4", nil];
UIPickerView *pickerView = [[UIPickerView alloc] init];
pickerView.delegate = self;
pickerView.dataSource = self;
[menu addSubview:pickerView];
[menu showInView:self.view];
[menu setBounds:CGRectMake(0,0,320, 500)];
CGRect pickerRect = pickerView.bounds;
pickerRect.origin.y = 35;
pickerView.frame = pickerRect;
Option2: the selfmade
menu = [[UIActionSheet alloc] initWithTitle:#"Actionsheet"
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
UIPickerView *pickerView = [[UIPickerView alloc] init];
pickerView.delegate = self;
pickerView.dataSource = self;
CGRect pickerRect = pickerView.bounds;
pickerRect.origin.y = 35;
pickerView.frame = pickerRect;
UIButton *startbtn = [UIButton buttonWithType:UIButtonTypeCustom];
startbtn.frame = CGRectMake(80,230, 170, 73);
[startbtn setBackgroundImage:[UIImage imageNamed:#"yourbutton.png"] forState:UIControlStateNormal];
[startbtn addTarget:self action:#selector(pressedbuttonCancel:) forControlEvents:UIControlEventTouchUpInside];
[menu addSubview:pickerView];
[menu addSubview:startbtn];
[menu showInView:self.view];
[menu setFrame:CGRectMake(0,150,320, 350)];
Check that for option2 the button should be on the actionsheet unless the clicks will not be dispatched to the selector method.

reload UIPickerView that is in UIAlertView

I have created an UIPickerView in the UIAlertView using this code:
alert1 = [[UIAlertView alloc] initWithFrame:CGRectMake(0, 0, 300, 200)];
alert1.title = #"All Notes";
alert1.message = #"\n\n\n\n\n\n\n";
alert1.delegate = self;
[alert1 addButtonWithTitle:#"Cancel"];
myPickerView = [[UIPickerView alloc] initWithFrame:CGRectZero];
myPickerView.tag = TAG_PICKER0;
myPickerView.delegate = self;
myPickerView.showsSelectionIndicator = YES;
myPickerView.autoresizingMask = UIAlertViewStyleDefault;
myPickerView.frame = CGRectMake(10, 40, 265, 150);
[alert1 addSubview:myPickerView];
[alert1 show];
I want to reload this pickerView when pressing a button, in button action I added a new element to the array that appears in the picker and I also added this line
[self.myPickerView reloadAllComponents];
self.myPickerView.delegate=self;
but the new element doesn't appear in the picker at this moment, it appears when I reopen the application
how can I solve this issue to make the new element appears in the pickerView at the moment of adding ??

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 add a custom control to ActionSheet with default buttons?

I want to customise a UIActionSheet slightly; Just add a custom UIButton/UIImageView above the existing 2 buttons like in this previous post but with a custom button/image instead of text above the default buttons. I tried adding a simple UILabel to see if it works as in this post: UIActionSheet Customization.
UIActionSheet *actionSheet = [[UIActionSheet alloc]
initWithTitle: #""
delegate: self
cancelButtonTitle: NSLocalizedString(#"Cancel",#"cancel button")
destructiveButtonTitle: nil
otherButtonTitles: #"Send",nil];
actionSheet.tag = kCallActionSheetTag;
UILabel *btn = [[UILabel alloc] initWithFrame:
CGRectMake(18, 10, 140, 50)];
accountLabel.textAlignment = UITextAlignmentLeft;
accountLabel.textColor = [UIColor whiteColor];
accountLabel.text = #"Account";
accountLabel.font = [UIFont boldSystemFontOfSize:16];
accountLabel.backgroundColor = [UIColor clearColor];
[actionSheet addSubview:accountLabel];
[accountLabel release];
[actionSheet showInView:self.tabBarController.view];
[actionSheet setBounds:CGRectMake(0,0,320, 260)];
[actionSheet release];
If I use the code above, the two buttons are displaced vertically upwards and UILabel overlaps the top button. They seem to be locked to the top of the actionsheet. I would rather not add my own buttons and position them. What is the best way to do this?
Thanks
here is my suggestion,
My experience is from UIAlertView, I have not tested.
It is supposed to set top message huge to offset button y-offset.
then add your UILabel

iPhone fill values for UIPickerView

I've created a UIPickerView, which displays on button click. The question is how I fill in the rowItems
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:#"Select Location"
delegate:self
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:nil];
[actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0,100,0,0)];
pickerView.showsSelectionIndicator = YES;
pickerView.dataSource = self;
pickerView.delegate = self;
[actionSheet addSubview:pickerView];
[pickerView release];
UISegmentedControl *closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:NSLocalizedString(#"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];
[closeButton release];
[actionSheet showInView:self.view];
[actionSheet setBounds:CGRectMake(0, 0, 320, 485)];
and How do I get value when "done" is clicked.
you've set the delegate of the alert, and the picker already. Now you have to implement them. You don't fill in data for the picker, the picker asks your delegate for the data.
You need the mandatory functions defined in these three protocols.
UIPickerViewDelegate
UIPickerViewDataSource
UIActionSheetDelegate
You have to implement -(IBAction)dismissActionSheet:(UIButton *)sender too.
and How do I get value when "done" is clicked.
I would store the value to a instance variable every time the picker changes, and once the done button is clicked I would assign it to my data model.
Edit: If you want to learn something I would get rid of the copy&paste solution and try to implement it on my own. But YMMV.