How to modify a Done button programmatically from UIActionSheet? - ios

I have this method to create an action sheet and uipicker:
-(void)presentNewPicker{
self.aac = [[UIActionSheet alloc] initWithTitle:#"What City?"
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:#"OK", nil];
self.pickrView = [[UIPickerView alloc] initWithFrame:CGRectMake(0.0, 44.0, 0.0, 0.0)];
self.pickrView.showsSelectionIndicator = YES;
self.pickrView.dataSource = self;
self.pickrView.delegate = self;
self.cityNames = [[NSArray alloc] initWithObjects: #"Phoenix", #"Tahoe", #"Nevada", #"Lime", #"Florida", nil];
UIToolbar *pickerDateToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
pickerDateToolbar.barStyle = UIBarStyleBlackOpaque;
[pickerDateToolbar sizeToFit];
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
[barItems addObject:flexSpace];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(dismissActionSheet:)];
[barItems addObject:doneBtn];
[pickerDateToolbar setItems:barItems animated:YES];
[self.aac addSubview:pickerDateToolbar];
[self.aac addSubview:self.pickrView];
[self.aac showInView:self.view];
[self.aac setBounds:CGRectMake(0,0,320, 464)];
}
The button that appears reads DONE but I want to modify it to read, Ready. How do I accomplish this?

Replace:
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(dismissActionSheet:)];
with:
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithTitle:#"Ready" style: UIBarButtonItemStyleBordered target:self action:#selector(dismissActionSheet:)];
You may want to use UIBarButtonItemStyleDone for the style.

I don't know what you want to do, but you might be interested in using the inputView and inputAccessoryView for one of your uiviews, to replace the keyboard. That'll give you the animations for free, and a place to put a toolbar. Some answers that might help: https://stackoverflow.com/a/6075062/220820
https://stackoverflow.com/a/8583703/220820

Related

Pickerview in Actionsheet displaying in iPhone but not in iPad

Firstly, this is the pictures in iPhone and iPad
iPhone : http://img4.hostingpics.net/pics/563577Capturedcran20140610125811.png
iPad : http://img4.hostingpics.net/pics/315654Capturedcran20140610125901.png
This is how I am doing :
_questionnairePicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0.0,44.0, 320.0, 250.0)];
_questionnaireActionSheet = [[UIActionSheet alloc] initWithTitle:#"Questionnaire"
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
_questionnaireActionSheet.backgroundColor = [UIColor whiteColor];
_dateActionSheet.backgroundColor = [UIColor whiteColor];
self.selectedIndexQuestionnaire = 0;
self.questionnairePicker.delegate = self;
[self.questionnaireActionSheet addSubview:[self getToolBarActionSheet:self.questionnairePicker]];
[self.questionnaireActionSheet addSubview:self.questionnairePicker];
and
-(UIToolbar *)getToolBarActionSheet:(UIView *)aPicker
{
UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
[pickerToolbar sizeToFit];
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self action:#selector(pickerDoneClick:)];
UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
target:self action:#selector(pickerCancelClick:)];
UIBarButtonItem *flex = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:self action:nil];
if(aPicker.tag == 2) doneBtn.tag = cancelBtn.tag = 2;
[barItems addObject:cancelBtn];
[barItems addObject:flex];
[barItems addObject:doneBtn];
[pickerToolbar setItems:barItems animated:YES];
return pickerToolbar;
}
So, someone know a good way to resolve it ?
UIPickerViews on the ipad don't have a default size (ie width).
change
_questionnairePicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0.0,44.0, 320.0, 250.0)];
to
_questionnairePicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 345, 400, 216)];

Flexible bar button item now displayed on UIToolbar

I know it may seem very simple and has been asked many times but i can't get it to work .I want to add two buttons to my toolbar. One on the right side and the other one on the left. Here is the code but the flexible one which is supposed to show up on the right side doesn't appear at all. Here is the code:
toolbar = [[UIToolbar alloc] init];
toolbar.frame = CGRectMake(0, 0, self.view.frame.size.width, 44);
NSMutableArray *items = [[NSMutableArray alloc] init];
[items addObject:[[UIBarButtonItem alloc] initWithTitle:#"next" style:UIBarButtonItemStylePlain target:self action:#selector(nextResult:)]];
UIBarButtonItem *done=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:self action:#selector(showSearch:)];
[done setImage:[UIImage imageNamed:#"cancel"]];
[items addObject:done];
[toolbar setItems:items animated:NO];
You need to use UIBarButtonSystemItemFlexibleSpace instead of UIBarButtonSystemItemFixedSpace
Code:
toolbar = [[UIToolbar alloc] init];
toolbar.frame = CGRectMake(0, 0, self.view.frame.size.width, 44);
NSMutableArray *items = [[NSMutableArray alloc] init];
[items addObject:[[UIBarButtonItem alloc] initWithTitle:#"next" style:UIBarButtonItemStylePlain target:self action:#selector(nextResult:)]];
UIBarButtonItem *space = [[UIBarButtonItem alloc]initWithBarButtonSystemItem: UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[items addObject:space];
[items addObject:[[UIBarButtonItem alloc] initWithTitle:#"cancel" style:UIBarButtonItemStylePlain target:self action:#selector(showSearch:)]];
[toolbar setItems:items animated:NO];
Reference : UIBarButtonItem Class Reference
The item with the system item UIBarButtonSystemItemFixedSpace is a space, not button object. Therefore it does show up. It's just no button.
Change your code to
toolbar = [[UIToolbar alloc] init];
toolbar.frame = CGRectMake(0, 0, self.view.frame.size.width, 44);
NSMutableArray *items = [[NSMutableArray alloc] init];
[items addObject:[[UIBarButtonItem alloc] initWithTitle:#"next" style:UIBarButtonItemStylePlain target:self action:#selector(nextResult:)]];
UIBarButtonItem *space = [[UIBarButtonItem alloc]initWithBarButtonSystemItem: UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[items addObject:space];
[items addObject:[[UIBarButtonItem alloc] initWithTitle:#"cancel" style:UIBarButtonItemStylePlain target:self action:#selector(showSearch:)]];
[toolbar setItems:items animated:NO];

UITextField delegate causes UIToolBar not show in iOS

This is what I am doing to display a UIToolbar in a UITextField's acceccory view.. Unfortunately I am not able to see it for some reason. What am I doing wrong here?
UIBarButtonItem *flexiableItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(doneTyping)];
UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithTitle:#"Next" style:UIBarButtonItemStyleBordered
target:self action:#selector(gotoNextTextfield:)];
UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
NSArray *items = [NSArray arrayWithObjects:item2, flexiableItem, item1, nil];
toolbar.items = items;
[inputAccView addSubview:toolbar];
[self.msgTextField setInputAccessoryView:inputAccView];
Update:
if I remove self.msgTextField.delegate = self; then I can see the toolbar.. but why??
Try replacing
[inputAccView addSubview:toolbar];
[self.msgTextField setInputAccessoryView:inputAccView];
with
[self.msgTextField setInputAccessoryView:toolbar];
In your .h file add <UITextFieldDelegate>
Then in your - (void)viewDidLoad try This, it works fine for me:
// Keyboard Tool Bar
UIToolbar *toolbar = [[UIToolbar alloc] init];
[toolbar setBarStyle:UIBarStyleBlackTranslucent];
[toolbar sizeToFit];
UIBarButtonItem *nextField =[[UIBarButtonItem alloc] initWithTitle:#"Next" style:UIBarButtonItemStyleBordered target:self action:#selector(nextField)];
UIBarButtonItem *flexButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
UIBarButtonItem *doneButton =[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(resignKeyboard)];
NSArray *itemsArray = [NSArray arrayWithObjects: nextField, flexButton, doneButton, nil];
[toolbar setItems:itemsArray];
[self.msgTextField setInputAccessoryView:toolbar];

UIPickerview not appearing at correct postion

Sorry for asking such a stupid question but I dont know why my UIPickerView with done button is not appearing at the correct place. I am using the following code but I dont know why I am getting such issue.
actionSheet=[[UIActionSheet alloc] initWithTitle:#"" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0,320,40)];
[pickerToolbar sizeToFit];
pickerToolbar.barStyle = UIBarStyleBlackTranslucent;
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithTitle:#"Cancel" style:UIBarButtonSystemItemCancel target:self action:#selector(cancelButtonTapped:)];
[barItems addObject:cancelBtn];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[barItems addObject:flexSpace];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStyleDone target:self action:#selector(doneButtonTapped:)];
[barItems addObject:doneBtn];
[pickerToolbar setItems:barItems animated:YES];
[actionSheet addSubview:pickerToolbar];
//-----------
maritalStatusPickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 44, 320, 216)];
maritalStatusPickerView.delegate = self;
maritalStatusPickerView.showsSelectionIndicator = YES;
[actionSheet addSubview:maritalStatusPickerView];
[actionSheet showInView:self];
use below code
actionSheet=[[UIActionSheet alloc] initWithTitle:#"" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
actionSheet.frame = CGRectMake(0, 234, 320, 256);
UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0,320,40)];
[pickerToolbar sizeToFit];
pickerToolbar.barStyle = UIBarStyleBlackTranslucent;
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithTitle:#"Cancel" style:UIBarButtonSystemItemCancel target:self action:#selector(cancelButtonTapped:)];
[barItems addObject:cancelBtn];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[barItems addObject:flexSpace];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStyleDone target:self action:#selector(doneButtonTapped:)];
[barItems addObject:doneBtn];
[pickerToolbar setItems:barItems animated:YES];
[actionSheet addSubview:pickerToolbar];
//-----------
maritalStatusPickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 44, 320, 216)];
maritalStatusPickerView.delegate = self;
maritalStatusPickerView.showsSelectionIndicator = YES;
// [self addSubview:maritalStatusPickerView];
[actionSheet addSubview:maritalStatusPickerView];
[self.view addSubview:actionSheet];
Why you are adding maritalStatusPickerView 2 times
[self addSubview:maritalStatusPickerView];
[actionSheet addSubview:maritalStatusPickerView];
Just include the frame of actionsheet to your code after initialization
actionSheet.frame = CGRectMake(0, 0, 320, 256);
refer this question
you have to add your action sheet in your view, and change the co-Ordinate if you want to open pickerview from bottom of view.
[actionSheet addSubview:maritalStatusPickerView];
//Replace this : [actionSheet showInView:self];
[self.view addSubview:actionSheet];

Pickerview for TextField

I have this code, it's inside an action that it's triggered when the user selects the textfield to show the datepicker:
pickerViewPopup = [[UIActionSheet alloc] initWithTitle:nil delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
pickerView = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 44, 0, 0)];
pickerView.datePickerMode = UIDatePickerModeDateAndTime;
pickerView.hidden = NO;
pickerView.date = [NSDate date];
UIToolbar *pickerToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
pickerToolbar.barStyle = UIBarStyleBlackTranslucent;
[pickerToolbar sizeToFit];
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
[barItems addObject:flexSpace];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStyleDone target:self action:#selector(doneButtonPressed:)];
[barItems addObject:doneBtn];
[pickerToolbar setItems:barItems animated:YES];
[pickerViewPopup addSubview:pickerToolbar];
[pickerViewPopup addSubview:pickerView];
[pickerViewPopup showFromTabBar:self.tabBarController.tabBar];
[pickerViewPopup setBounds:CGRectMake(0,0,320, 500)];
I was using this code in an app with a tab bar for navigation. Now I would like to use it in an app without it, but when the process reaches [pickerViewPopup showFromTabBar:self.tabBarController.tabBar]; it crashes.
Any idea on how I could fix it?
Use one of the following,
– showFromToolbar:
– showInView:
– showFromBarButtonItem:animated:
– showFromRect:inView:animated:
For eg:-
[pickerViewPopup showFromToolbar:pickerToolbar];
The problem was that you dont have a tab bar and you are trying to access the tab bar object to show the picker from it. Since tabbar is nil for you, it will crash.
For more details check the apple documentation.

Resources