UIView inside a UIAlertController - ios

Just a few points before I start:
I have just taken over this code
I have little to no experience in iOS code
Consider me a noob at this so go easy on me :P
I have some code below, which used to show a UIActionSheet. In iOS 8 UIActionSheet has been removed (Thanks apple!) so I have started to change it all to use UIAlertController...
I ran into many problems which I have solved, now I am completely confused and need some experienced help from you guys and girls....!
Here is a screenshot
Now as you can see the UIAlertController is being displayed but the UIView is places incorrectly and because of this the controls are impossible to use! You can drag the numbers as expected but it's not as easy...
There are three controllers as you can see, one for choosing a date time, one for number of passengers and the other for choosing a vehicle type.
I just need a nice simple fix that will work on all iOS8 phones and it can be slightly dodgy as we are about to hire someone to re-write the entire code as the code is pretty bad overall and we want to redesign the app from the bottom up.
If you want any more information do let me know, once again my iOS coding experience is very small ( I Do want to learn as I am enjoying it, albeit not as much as Android)
else if (selectedTableField == travelTimeText || selectedTableField == noPassengers || selectedTableField == carType) {
// setup actionsheet to contain the UIPicker
if (version < 8)
{
actionSheet = [[UIActionSheet alloc] initWithTitle:#" "
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
[actionSheet showInView:self.view];
}
if (version >= 8)
{
alertController = [UIAlertController alertControllerWithTitle:#"title"
message:#"messages"
preferredStyle:UIAlertControllerStyleAlert];
}
//CGRect actionSheetBounds = CGRectMake(0,0,320 + offset, 570);
CGRect actionSheetBounds;
if (selectedTableField == travelTimeText)
// actionSheetBounds = CGRectMake(0,0,320 + offset, 655);
if (version >= 8)
{
[alertController.view setBounds:actionSheetBounds];
}
else
{
[actionSheet setBounds:actionSheetBounds];
}
UIView *bgView = [[UIView alloc] initWithFrame:actionSheetBounds];
if (version < 7)
bgView.backgroundColor = [[[ConfigManager sharedInstance].skin valueForKey:#"popupBackgroundColour"] toUIColor];
UIButton *doneBtn = [UIButton buttonWithType:UIButtonTypeCustom];
// swap asap and select around if on travel time picker
if (selectedTableField == travelTimeText) {
doneBtn.frame = CGRectMake(10+bOffset, 305, 300, 40);
} else {
doneBtn.frame = CGRectMake(10+bOffset, 250, 300, 40);
}
[doneBtn addTarget:self action:#selector(pickerDone:) forControlEvents:UIControlEventTouchUpInside];
[doneBtn setBackgroundImage:[UIImage imageNamed:#"button_large.png"] forState:UIControlStateNormal];
[doneBtn setTitle:#"Select" forState:UIControlStateNormal];
doneBtn.titleLabel.font = [UIFont boldSystemFontOfSize:19];
doneBtn.titleLabel.textColor = [[[ConfigManager sharedInstance].skin valueForKey:#"popupButtonFontColour"] toUIColor];
[bgView addSubview:doneBtn];
UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320 + offset, 20)];
title.font = [UIFont boldSystemFontOfSize:12];
title.backgroundColor = [UIColor clearColor];
title.textAlignment = UITextAlignmentCenter;
title.textColor = [[[ConfigManager sharedInstance].skin valueForKey:#"popupLabelFontColour"] toUIColor];
if (selectedTableField == travelTimeText) {
title.text = #"Booking Time";
UIButton *asapBtn = [UIButton buttonWithType:UIButtonTypeCustom];
asapBtn.frame = CGRectMake(10 +bOffset, 250, 300, 40);
[asapBtn addTarget:self action:#selector(pickerAsap:) forControlEvents:UIControlEventTouchUpInside];
[asapBtn setTitle:#"As soon as possible" forState:UIControlStateNormal];
[asapBtn setBackgroundImage:[UIImage imageNamed:#"button_large.png"] forState:UIControlStateNormal];
asapBtn.titleLabel.font = [UIFont boldSystemFontOfSize:19];
asapBtn.titleLabel.textColor = [[[ConfigManager sharedInstance].skin valueForKey:#"popupButtonFontColour"] toUIColor];
[bgView addSubview:asapBtn];
} else if (selectedTableField == noPassengers)
title.text = #"Passengers";
else
title.text = #"Vehicle";
[bgView addSubview:title];
[title release];
if (version < 8)
{
[actionSheet addSubview:bgView];
}
else
{
[alertController.view addSubview:bgView];
}
[bgView release];
// travel time uses a different picker
if (selectedTableField == travelTimeText) {
UIDatePicker *datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0.0 + bOffset, 20, 0.0, 0.0)];
[datePicker addTarget:self action:#selector(travelTimeChanged:) forControlEvents:UIControlEventValueChanged];
datePicker.datePickerMode = UIDatePickerModeDateAndTime;
datePicker.minuteInterval = 5;
if (travelTime == nil)
datePicker.date = [[NSDate date] addTimeInterval:15*60];
else
datePicker.date = travelTime;
if (version < 8)
{
[actionSheet addSubview:datePicker];
}
else
{
[alertController.view addSubview:datePicker];
}
// update table field to show now
[self travelTimeChanged:datePicker];
[datePicker release];
if (version >= 8)
{
[self presentViewController:alertController animated:NO completion:nil];
}
} else {
UIPickerView *picker = [[UIPickerView alloc] initWithFrame:CGRectMake(0.0 + bOffset, 20, 0.0, 0.0)];
picker.dataSource = self;
picker.delegate = self;
if (selectedTableField == noPassengers) {
picker.showsSelectionIndicator = YES;
[picker selectRow:[passengerOptions indexOfObject:noPassengers.text] inComponent:0 animated:NO];
} else if (selectedVehicle != nil) {
[picker selectRow:[filteredCarOptions indexOfObject:selectedVehicle] inComponent:0 animated:NO];
}
if (version < 8)
{
[actionSheet addSubview:picker];
}
else
{
[alertController.view addSubview:picker];
}
[picker release];
if (version >= 8)
{
[self presentViewController:alertController animated:NO completion:nil];
}
}
} else if (selectedTableField == bookButton) {
// validate the form
[self validateForm];
}

I am also struggling with the same problem. I need to have a UIPickerView inside a UIAlertController. After some hours of investigations I distinguished that there is no way to customize the view of UIAlertController. In fact it is written explicitly on apple documentations.
The UIAlertController class is intended to be used as-is and does not support subclassing.
The view hierarchy for this class is private and must not be modified.
By the way you can try alternative solutions - uipickerview-programatic-example.

Related

UIAlertController is not is window hierarchy

Im trying to show up a pop up containing a UIPickerView and UITextField.
Following is my code that I use to create the pop up.
self.alert = [UIAlertController alertControllerWithTitle:nil message:#"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n" preferredStyle:UIAlertControllerStyleAlert];
CGRect pickerFrame = CGRectMake(0, 0, 270, 200);
UIPickerView *regionsPicker = [[UIPickerView alloc] initWithFrame:pickerFrame];
regionsPicker.tag = 1;
regionsPicker.dataSource = (id <UIPickerViewDataSource>)self;
regionsPicker.delegate = (id <UIPickerViewDelegate>) self;
[regionsPicker setShowsSelectionIndicator:YES];
UIView *toolView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 270.0f, 44.f)];
toolView.backgroundColor = [UIColor blackColor];
CGRect buttonFrame = CGRectMake(0, 5, 100, 30);
UIButton *button = [[UIButton alloc] initWithFrame: buttonFrame];
button.imageEdgeInsets = UIEdgeInsetsMake(0, button.titleLabel.frame.size.width, 0, -button.titleLabel.frame.size.width);
UIImage *btnImage = [UIImage imageNamed:#"delete.png"];
[button setImage:btnImage forState:UIControlStateNormal];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[button setTitleColor: [UIColor blueColor] forState: UIControlStateNormal];
[toolView addSubview:button];
[button addTarget:self action:#selector(closeDialog:) forControlEvents:UIControlEventTouchUpInside];
self.textField = [[UITextView alloc] initWithFrame:CGRectMake(30, 170, 220, 200)];
_textField.editable = NO;
_textField.hidden=true;
self.textField.text = #"Enter text...";
self.textField.font = [UIFont systemFontOfSize:15];
self.textField.autocorrectionType = UITextAutocorrectionTypeNo;
self.textField.keyboardType = UIKeyboardTypeDefault;
self.textField.returnKeyType = UIReturnKeyDone;
self.textField.textColor = [UIColor lightGrayColor];
self.textField.delegate = (id <UITextViewDelegate>) self;
self.textField.layer.borderWidth = 1.0f;
self.textField.layer.cornerRadius = 5;
self.textField.layer.borderColor = [[UIColor grayColor] CGColor];
[toolView addSubview:self.textField];
CGRect actionFrame = CGRectMake(80, 400, 100, 30);
UIButton *actionBtn = [[UIButton alloc] initWithFrame: actionFrame];
[actionBtn setTitle:#"Submit" forState:UIControlStateNormal];
[actionBtn setTitleColor: [UIColor whiteColor] forState: UIControlStateNormal];
[actionBtn setBackgroundColor:[UIColor purpleColor]];
[self.textField setUserInteractionEnabled:YES];
[toolView setUserInteractionEnabled:YES];
[self.alert.view setUserInteractionEnabled:YES];
[self.alert.view addSubview:regionsPicker];
[self.alert.view addSubview:self.textField];
[self.alert.view addSubview:toolView];
[self.alert.view addSubview:actionBtn];
id rootViewController=[UIApplication sharedApplication].delegate.window.rootViewController;
if([rootViewController isKindOfClass:[UINavigationController class]])
{
rootViewController=[((UINavigationController *)rootViewController).viewControllers objectAtIndex:0];
}
[rootViewController presentViewController:_alert animated:YES completion:nil];
It works fine.However, if Im trying to enter text in the UITextField by tapping it, the keyboard doesn't appear and hence Im not be able to type anything within the UITextField. How can I sort this out?
the reason is you added textField, button, all those inside toolview , but the toolview height is very less
change your toolView height from 44
into 500 or else (combination of textfield, button + 50).
Change below statement
UIView *toolView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 270.0f, 44.f)];
to
UIView *toolView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 270.0f, 500.f)];
Update
change this line
[self presentViewController:alert animated:YES completion:nil];
into
id rootViewController=[UIApplication sharedApplication].delegate.window.rootViewController;
if([rootViewController isKindOfClass:[UINavigationController class]])
{
rootViewController=[((UINavigationController *)rootViewController).viewControllers objectAtIndex:0];
}
[rootViewController presentViewController:alert animated:YES completion:nil];
Update New
UIViewController *rootViewController=[UIApplication sharedApplication].delegate.window.rootViewController;
if([rootViewController isKindOfClass:[UINavigationController class]])
{
rootViewController=[((UINavigationController *)rootViewController).viewControllers objectAtIndex:0];
}else
{
while (rootViewController.presentedViewController) {
rootViewController = rootViewController.presentedViewController;
}
}
[rootViewController presentViewController:alert animated:YES completion:nil];
Swift
var rootViewController: UIViewController = UIApplication.sharedApplication().delegate.window.rootViewController
if (rootViewController is UINavigationController.self) {
rootViewController = ((rootViewController as! UINavigationController)).viewControllers[0]
}
else {
while rootViewController.presentedViewController {
rootViewController = rootViewController.presentedViewController
}
}
rootViewController.presentViewController(alert, animated: true, completion: { _ in })
Try this it may help you:-
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:#" Your message " preferredStyle:UIAlertControllerStyleAlert];
UIView *toolView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 270, 200)];
toolView.backgroundColor = [UIColor blackColor];
CGRect buttonFrame = CGRectMake(0, 5, 100, 30);
UIButton *button = [[UIButton alloc] initWithFrame:buttonFrame];
[button setTitle:#"Image" forState:UIControlStateNormal];
[button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[toolView addSubview:button];
UITextField *txtField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 270, 44)];
txtField.borderStyle = UITextBorderStyleRoundedRect;
txtField.backgroundColor = [UIColor grayColor];
txtField.font = [UIFont systemFontOfSize:15];
txtField.placeholder = #"enter text";
txtField.autocorrectionType = UITextAutocorrectionTypeNo;
txtField.keyboardType = UIKeyboardTypeDefault;
txtField.returnKeyType = UIReturnKeyDone;
txtField.clearButtonMode = UITextFieldViewModeWhileEditing;
txtField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
txtField.delegate = (id<UITextFieldDelegate>)self;
[toolView addSubview:txtField];
[txtField setUserInteractionEnabled:YES];
[toolView setUserInteractionEnabled:YES];
[alertController.view setUserInteractionEnabled:YES];
[alertController.view addSubview:toolView];
[self presentViewController:alertController animated:YES completion:nil];

How to Show the Pickerview value in Textfield and hide the pickerView using barbuttonitem?

I am Having 2 Textfields stateText and providerText.If i click the first textfield means,it shows the pickerView.After That i will Select the pickerView Value.Also i have added the toolbar barbuttonitem in that pickerView.Then I have to show the pickerView value in the Textfields and hide the pickerView after selecting the value in the PickerView.Also Pointer should moves to next TextField connectionNoText after getting Values in TextFields.
1.How to hide the PickerView and show the value in TextField?
2.How to show the pickerview in the Bottom of the View irrespective of phone size?
This is my Source Code:
NewConnectionviewController.m
-(void)viewDidLoad
{
[super viewDidLoad];
//ScrollView
self.view.backgroundColor = [UIColor grayColor];
scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[scrollView setContentSize:CGSizeMake(self.view.frame.size.width, self.navigationController.navigationBar.frame.size.height + 510.0)];
[self.view addSubview:scrollView];
//statePickerView
_statePickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, self.view. frame.size.height, self.view.frame.size.width, 216)];
_statePickerView.delegate = self;
_statePickerView.dataSource = self;
_statePickerView.showsSelectionIndicator = YES;
[_statePickerView setBackgroundColor:[UIColor lightGrayColor]];
[self.view addSubview:_statePickerView];
//providerPickerView
_providerPickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, self.view. frame.size.height, self.view.frame.size.width, 216)];
_providerPickerView.delegate = self;
_providerPickerView.showsSelectionIndicator = YES;
[_providerPickerView setBackgroundColor:[UIColor lightGrayColor]];
[self.view addSubview:_providerPickerView];
//ToolBar
UIToolbar *toolBar= [[UIToolbar alloc] initWithFrame:CGRectMake(0,0,self.view.frame.size.width,44)];
[toolBar setBarStyle:UIBarStyleBlackOpaque];
UIBarButtonItem *barButtonDone = [[UIBarButtonItem alloc] initWithTitle:#"Done"
style:UIBarButtonItemStyleBordered target:self action:#selector(hidePickerView:)];
toolBar.items = #[barButtonDone];
barButtonDone.tintColor=[UIColor lightTextColor];
[barButtonDone setEnabled:YES];
[_statePickerView addSubview:toolBar];
//[_providerPickerView addSubview:toolBar];
//GettingView
[self stateView];
[self providerView];
[self connectionNoView];
[self addTestView];
}
-(void)hidePickerView:(UIBarButtonItem *)sender
{
NSLog(#"clicked");
[self.selectState resignFirstResponder];
}
-(void)stateView
{
UIView *stateView = [[UIView alloc]initWithFrame:CGRectMake(0, self.navigationController.navigationBar.frame.size.height, self.view.frame.size.width, 80)];
stateView.backgroundColor = [UIColor blueColor];
[scrollView addSubview:stateView];
//stateLabel
UILabel *stateLabel = [[UILabel alloc]initWithFrame:CGRectMake(stateView.frame.origin.x+20,35, 70, 20)];
[stateLabel setText:#"State"];
[stateLabel setTextColor:[UIColor whiteColor]];
[stateView addSubview:stateLabel];
//TextField
NSLog(#"stateViewSize==>>%f",stateView.frame.origin.y);
stateText = [[UITextField alloc]initWithFrame:CGRectMake(stateView.frame.origin.x+110, 30, 150, 40)];
stateText.textColor = [UIColor blackColor];
stateText.backgroundColor = [UIColor clearColor];
[stateText setBorderStyle:UITextBorderStyleRoundedRect];
[stateText setPlaceholder:#"select State"];
[stateText setTag:0];
[stateView addSubview:stateText];
[stateText setDelegate:self];
}
-(void)providerView
{
UIView *providerView = [[UIView alloc]initWithFrame:CGRectMake(0, self.navigationController.navigationBar.frame.size.height+80, self.view.frame.size.width, 80)];
providerView.backgroundColor = [UIColor orangeColor];
[scrollView addSubview:providerView];
NSLog(#"providerViewsize==>>%f",providerView.frame.origin.y);
//providerLabel
UILabel *providerLabel = [[UILabel alloc]initWithFrame:CGRectMake(providerView.frame.origin.x+20, 35, 70, 20)];
[providerLabel setText:#"Provider"];
[providerLabel setTextColor:[UIColor whiteColor]];
[providerView addSubview:providerLabel];
//TextField
providerText = [[UITextField alloc]initWithFrame:CGRectMake(providerView.frame.origin.x+110,30, 150, 40)];
providerText.textColor = [UIColor blackColor];
[providerText setPlaceholder:#"select Provider"];
providerText.backgroundColor = [UIColor clearColor];
[providerText setTag:1];
[providerText setBorderStyle:UITextBorderStyleRoundedRect];
[providerView addSubview:providerText];
[providerText setDelegate:self];
}
-(void)connectionNoView
{
UIView *connectionNoView = [[UIView alloc]initWithFrame:CGRectMake(0, self.navigationController.navigationBar.frame.size.height+160, self.view.frame.size.width, 150)];
connectionNoView.backgroundColor = [UIColor greenColor];
[scrollView addSubview:connectionNoView];
NSLog(#"connectionNoViewsize==>>%f",connectionNoView.frame.origin.y);
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
// Show UIPickerView
if (textField.tag ==0)
{
//stateText.inputView = _statePickerView;
[UIView animateWithDuration:0.5 delay:0.1 options:UIViewAnimationOptionCurveEaseIn animations:^{
_statePickerView.frame = CGRectMake(0, self.view.frame.size.height-216,self.view.frame.size.width, 216);
}
completion:^(BOOL finished){
}];
}
else
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#""
message:#"Please select state first."
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}
return NO;
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView {
return 1;//Or return whatever as you intend
}
- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component
{
if([thePickerView isEqual:_statePickerView])
{
return [STATE_ARRAY count];
}
else
{
return [PROVIDER_ARRAY count];
}
}
- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if([thePickerView isEqual:_statePickerView])
{
return [STATE_ARRAY objectAtIndex:row];
}
else
{
return [PROVIDER_ARRAY objectAtIndex:row];
}
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
NSString *firstPickerViewValue;
NSString *secondPickerViewValue;
if ([pickerView isEqual:_statePickerView])
{
firstPickerViewValue = [STATE_ARRAY objectAtIndex:row];
[_providerPickerView selectRow:row inComponent:0 animated:YES];
secondPickerViewValue = [PROVIDER_ARRAY objectAtIndex:row];
[_providerPickerView reloadAllComponents];
[stateText setText:[NSString stringWithFormat:#"%#",firstPickerViewValue]];
[providerText setText:[NSString stringWithFormat:#"%#",secondPickerViewValue]];
}
I have read through the code and to be honest Im pretty confused by your approach as I tend to avoid magic numbers when laying out the UI. Anyway I think both your questions can be solved by assigning the inputView of your slate textField and assigning the inputAccessoryView to the toolbar you created in the code.
So for example,
stateText.inputView = _statePickerView
Well that is Swift so you would have
[stateText setInputView:_statePickerView];
Then you set the Accessory View so
[stateText setInputAccessoryView: toolBar];
Although toolbar is only in scope inside your viewDidLoad method so you should fix that. This should be enough to get you back on the right path. To dismiss the pickerView just resignFirstResponder of the selected textField inside your hidePickerView method.
Note that your textfields are also confined to the methods they are created in so you should fix that too.
Finally i Found a Solution.PickerView itself a ViewController so we can't hide the pickerView.Alternative solution is,
1.Create the SubView with Height 260 and add that View to the Self.View.
(set Y Value as Self.View.Frame.Size.height).
2.Add the PickerView(default Height - 216) and ToolBar(default Height - 44) to that SubView.
3.In BarButtonItem action Move that view to the bottom.(ex.Set Frame like this) CGRectMake(0,Self.View.Frame.Size.Height,Self.View.Frame.Size.Width,216)

Custom UIAlertView not resizing for keyboard after its 1st appearance

I have a custom UIAlertVIew with 2 textFields that I am using to input values into. The first time that the customUIAlertVIew is displayed, the view is moved up to make room for the keyboard.
This is unfortunately, not the case for each subsequent display. Each subsequent time, the alertView isn't moved up for the keyboard.
I am looking for what is causing this problem, and how can I get around it in the future.
EDIT: Here is the code for how the custom UIAlertVIew is created.
CustomUIAlertView.m
-(id)initWithTitle:(NSString *)title {
if (self = [super init]){
self.title = title;
self.message = #"\n\n\n\n\n\n";
[self addButtonWithTitle:#"Cancel"];
[self addButtonWithTitle:#"Done"];
[self setDelegate:self];
}
return self;
}
-(void)didPresentAlertView:(UIAlertView *)alertView{
NSArray *subViews = [UIApplication sharedApplication].keyWindow.rootViewController.
presentedViewController.view.subviews;
if (subViews.count >1){
UIView *presentedView = [subViews objectAtIndex:1];
UILabel *player1Label = [[UILabel alloc]initWithFrame:CGRectMake(20, 55, 130, 21)];
player1Label.text = [NSString stringWithFormat:#"Player 1 Score"];
// player1Label.textInputMode = UIKeyboardTypeNumberPad;
player1Label.tag = 42;
[presentedView addSubview:player1Label];
UILabel *player2Label = [[UILabel alloc]initWithFrame:CGRectMake(20, 75, 130, 21)];
player2Label.text = [NSString stringWithFormat:#"player 2 Score"];
player2Label.tag = 43;
[presentedView addSubview:player2Label];
self.p1ScoreField = [[UITextField alloc]initWithFrame:CGRectMake(150, 55, 80, 21)];
self.p1ScoreField.placeholder= #"score";
self.p1ScoreField.tag = 44;
self.p1ScoreField.keyboardType = UIKeyboardTypeNumberPad;
self.p1ScoreField.borderStyle = UITextBorderStyleRoundedRect;
self.p1ScoreField.delegate = self;
[self.p1ScoreField addTarget:self action:#selector(UITextFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
[presentedView addSubview:self.p1ScoreField];
self.p2ScoreField = [[UITextField alloc]initWithFrame:CGRectMake(150, 75, 80, 21)];
self.p2ScoreField.tag = 45;
self.p2ScoreField.placeholder = #"score";
self.p2ScoreField.keyboardType = UIKeyboardTypeNumberPad;
self.p2ScoreField.borderStyle = UITextBorderStyleRoundedRect;
self.p2ScoreField.delegate = self;
[self.p2ScoreField addTarget:self action:#selector(UITextFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
[presentedView addSubview:self.p2ScoreField];
}
}
View presented in a separate view Controller. Alloc/Init is called on alertView at the viewDidAppear method.
- (IBAction)addNewRoundButtonPressed:(id)sender {
[alertView show];
}
Adding subviews to UIAlertView is not supported by Apple and is strongly discouraged.
You're better off using a regular view controller with custom modal presentation (look up UIViewControllerAnimatedTransitioning) or Googling a 3rd-party replacement.

iPad Actionsheet not showing

I am trying to display action sheet in iPad which will contain 2 buttons and date pickerview.
I displayed the action sheet in iphone properly but when I try to display the action sheet in iPad its just displaying one thick black line.
Currently its looking like :
and code for displaying action sheet is
UIButton *btnClicked=(UIButton *)sender;
menu = [[UIActionSheet alloc] initWithTitle:nil
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
/* menu=[[UIActionSheet alloc] initWithTitle:#"This will remove this recipe from all synced devices as well. Are you sure?" delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:#"Confirm Delete Recipe" otherButtonTitles:nil];*/
[menu setTag:121];
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10,5,300,20)];
[titleLabel setFont:[UIFont fontWithName:#"Trebuchet MS" size:18.0f]];
titleLabel.textAlignment = UITextAlignmentCenter;
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.textColor = [UIColor whiteColor];
[titleLabel setNumberOfLines:1];
[menu addSubview: titleLabel];
UIButton *cancelButton = [UIButton buttonWithType: UIButtonTypeCustom];
cancelButton.frame = CGRectMake(30, 30, 124, 43);
[cancelButton setBackgroundImage:[UIImage imageNamed:#"cancel_button.png"] forState: UIControlStateNormal];
[cancelButton addTarget:self action:#selector(cancelButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
cancelButton.adjustsImageWhenHighlighted = YES;
cancelButton.tag=33;
[menu addSubview: cancelButton];
UIButton *okButton = [UIButton buttonWithType: UIButtonTypeCustom];
okButton.frame = CGRectMake(170, 30, 124, 43);
[okButton setBackgroundImage:[UIImage imageNamed:#"continue_button.png"] forState: UIControlStateNormal];
[okButton addTarget:self action:#selector(okButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
okButton.adjustsImageWhenHighlighted = YES;
[menu addSubview: okButton];
datePickerView = [[UIDatePicker alloc] init];
CGRect pickerRect = datePickerView.bounds;
pickerRect.origin.y = 75;
datePickerView.frame = pickerRect;
//---Picker view for from date selection
if (btnClicked.tag==51)
{
[titleLabel setText:SELECT_FROM_DATE];
NSDate *now = [Global todayDate];
int daysToAdd = 1;
NSDate *newDate = [now dateByAddingTimeInterval:60*60*24*daysToAdd];
// NSLog(#"%#",strDtFromDate);
datePickerView.minimumDate = newDate;
if (holidayType==2)
{
NSDate *holidayFromDate=[dateFormat dateFromString:objectSelectedGroup.fromDate];
// newDate=[holidayFromDate dateByAddingTimeInterval:60*60*24*daysToAdd];
if ([[Global todayDate] compare:holidayFromDate] == NSOrderedAscending)
{
datePickerView.minimumDate = holidayFromDate;
}
else if([[Global todayDate] compare:holidayFromDate] == NSOrderedDescending)
{
datePickerView.minimumDate = newDate;
}
NSDate *holidayToDate=[dateFormat dateFromString:objectSelectedGroup.toDate];
NSLog(#"%#",objectSelectedGroup.fromDate);
NSLog(#"%#",objectSelectedGroup.toDate);
datePickerView.maximumDate = holidayToDate;
}
okButton.tag=34;
}
else if(btnClicked.tag==52)
{
[titleLabel setText:SELECT_TO_DATE];
datePickerView.minimumDate=stringDate;
NSDate *holidayToDate=[dateFormat dateFromString:objectSelectedGroup.toDate];
datePickerView.maximumDate=holidayToDate;
okButton.tag=35;
}
datePickerView.datePickerMode = UIDatePickerModeDate;
stringDate=[datePickerView date];
[datePickerView addTarget:self action:#selector(selectedDateChanged:) forControlEvents:UIControlEventValueChanged];
[menu addSubview:datePickerView];
//menu.actionSheetStyle=UIActionSheetStyleBlackTranslucent;
[menu showInView:self.view];
[menu setFrame:CGRectMake(0,self.view.frame.size.height - 290,320,290)];
And same code is working fine for iphone.
How to solve this?
Use PopOver Controller to display action sheet on ipad. To use actionsheet on Popover controller refer the following links,
UIDatePicker in UIActionSheet on iPad
UIActionSheet on iPad frame too small
UIActionaSheet is not fit for IPad and they most time behave unpredicted around frame
I would recommend a UIPopoverController with a custom UIViewController for doing this kinda user experience.
you can try replacing last two line with following lines,
[menu setFrame:CGRectMake(0,self.view.frame.size.height - 290,320,290)];
[menu showInView:[[[UIApplication sharedApplication] delegate] window]];

UIPicker / UIActionSheet works fine on iPhone but only thin line on iPad

The following Code works fine on iPhone, but not on iPad. Only get a thin line instead of the full view. Have been researching and maybe something with the rect for the picker? All size numbers seem to be reasonable.
actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:nil
cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
[actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
CGRect pickerFrame = CGRectMake(0, 40, 0, 0);
if ( isipad ) pickerFrame = CGRectMake(0, 345, 400, 216);
pickerView = [[UIPickerView alloc] initWithFrame:pickerFrame];
pickerView.showsSelectionIndicator = YES;
pickerView.dataSource = self;
pickerView.delegate = self;
[actionSheet addSubview:pickerView];
[pickerView release];
UISegmentedControl *closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:#"Select"]];
closeButton.momentary = YES;
closeButton.frame = CGRectMake(10, 7.0f, 100.0f, 30.0f);
closeButton.segmentedControlStyle = UISegmentedControlStyleBar;
closeButton.tintColor = [UIColor blackColor];
[closeButton addTarget:self action:#selector(dismissActionSheet:) forControlEvents:UIControlEventValueChanged];
[actionSheet addSubview:closeButton];
[closeButton release];
UISegmentedControl *skipButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:#"Skip Birth Year"]];
skipButton.momentary = YES;
skipButton.frame = CGRectMake(200, 7.0f, 100.0f, 30.0f);
skipButton.segmentedControlStyle = UISegmentedControlStyleBar;
skipButton.tintColor = [UIColor blackColor];
[skipButton addTarget:self action:#selector(skipActionSheet:) forControlEvents:UIControlEventValueChanged];
[actionSheet addSubview:skipButton];
[skipButton release];
[actionSheet setBounds:CGRectMake(0, 0, self.view.frame.size.width, 216)];
[actionSheet showInView:self.view];
[actionSheet setBounds:CGRectMake(0, 0, self.view.frame.size.width, 216)];
Well, got it to work by putting most of the code in
- (void)willPresentActionSheet:(UIActionSheet *)actionSheet
including
pickerFrame = CGRectMake(0, 0, 320, 214);
AND
initWithTitle:#"\n\n\n\n\n\n\n\n\n\n\n\n\n\n"
for the action sheet. Magic I guess.
Is this correct:
CGRect pickerFrame = CGRectMake(0, 40, 0, 0); //This line sets the frame height and width equal to 0.
if ( isipad ) pickerFrame = CGRectMake(0, 345, 400, 216);
Is your isipad bool working ok?
Thanks for your post ort11.
I needed to make an iPad app and I did working copy-paste code from iPhone app and on iPad is the same: a small line :)
My needs is a language chooser only, nothing else.
Here are the codes based on your answer:
- (IBAction)btLanguageChooserPressed {
NSLog(#"btLanguageChooserPressed");
[self createLanguageSheet];
// show the sheet: will be invoked the willPresentActionSheet method
[sheet showInView:self.view];
}
- (void)createLanguageSheet {
NSLog(#"createLanguageSheet()");
// hoolly crap, need to initialize the title with: #"\n\n\n\n\n\n\n\n\n\n\n\n\n\n" to get working!!!
sheet = [[UIActionSheet alloc] initWithTitle:#"\n\n\n\n\n\n\n\n\n\n\n\n\n\n"
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
[sheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
}
- (void)willPresentActionSheet:(UIActionSheet *)actionSheet
{
CGRect pickerFrame = CGRectMake(0, 0, 320, 216);
pickerView = [[UIPickerView alloc] initWithFrame:pickerFrame];
pickerView.showsSelectionIndicator = YES;
pickerView.dataSource = self;
pickerView.delegate = self;
[sheet addSubview:pickerView];
// add close button:
UISegmentedControl *closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:#"OK"]];
closeButton.momentary = YES;
closeButton.frame = CGRectMake(120, 12, 100, 30);
closeButton.segmentedControlStyle = UISegmentedControlStyleBar;
closeButton.tintColor = [UIColor blackColor];
[closeButton addTarget:self action:#selector(dismissSheet:) forControlEvents:UIControlEventValueChanged];
[sheet addSubview:closeButton];
[sheet setBounds:CGRectMake(0, 0, self.view.frame.size.width, 216)];
//[sheet showInView:self.view];//already called, why need again?
//[sheet setBounds:CGRectMake(0, 0, self.view.frame.size.width, 216)];
}
at runtime I do a cache in a variable:
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
NSLog(#"Selected: %#", [arrayLanguages objectAtIndex:row]) ;
selectedRow = row;
}
and here is the finish:
- (void)dismissSheet:(id)sender {
[sheet dismissWithClickedButtonIndex:0 animated:YES];
NSString* selectedLanguage = [arrayLanguages objectAtIndex:selectedRow];
NSLog(#"dismissSheet() selectedLanguage: %#",selectedLanguage);
switch (selectedRow) {
case 0://English
imgCurrentLanguage.image = [UIImage imageNamed:#"english.png"];
break;
case 1:// Deutsch
break;
case 2://Français
break;
case 3://Italiano
break;
case 4://Español
break;
default:
break;
}
// TODO: do the changes in GUI
}
I am not able to move this actionsheet from the center of the screen to botton- left corner. But I am trying...
I think this would be the cleanest solution :
#interface MyActionSheet : UIActionSheet
#property (nonatomic, strong) IBOutlet UIView * customView;
#end
#implementation MyActionSheet
-(CGRect)bounds {
return self.customView.bounds;
}
#end
then:
MyActionSheet * s = [[MyActionSheet alloc] initWithTitle:nil delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
s.customView = _auxView;
[s addSubview:_auxView];

Resources