Showing a selection list on a popup using UIActionSheet - ios

I'm developing an iOS 5.0+ app with latest SDK.
I want to show a popup with some options to choose. But I don't know how to do it.
I have this code to show an UIActionSheet with a seven buttons:
- (void)setUserActivity
{
activityActionSheet = [[UIActionSheet alloc] initWithTitle:nil
delegate:nil
cancelButtonTitle:#""
destructiveButtonTitle:nil
otherButtonTitles:nil];
[activityActionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
for (int index = 0; index < 7; index++)
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:#selector(activitySelected:)
forControlEvents:UIControlEventTouchDown];
CGRect frame = CGRectMake(0.0, index*40 + 10, 160.0, 40.0);
button.frame = frame;
button.tag = index;
[button setTitle:[NSString stringWithFormat:#"Button %d", index] forState:UIControlStateNormal];
[activityActionSheet addSubview:button];
}
CGRect toolbarFrame = CGRectMake(0, 0, activityActionSheet.bounds.size.width, 44);
UIToolbar* controlToolbar = [[UIToolbar alloc] initWithFrame:toolbarFrame];
[controlToolbar setBarStyle:UIBarStyleBlack];
[controlToolbar sizeToFit];
UIBarButtonItem* spacer =
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil
action:nil];
UIBarButtonItem* setButton =
[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(#"Set", nil)
style:UIBarButtonItemStyleDone
target:self
action:#selector(dismissActivityActionSheet)];
UIBarButtonItem* cancelButton =
[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(#"Cancel", nil)
style:UIBarButtonItemStyleBordered
target:self
action:#selector(cancelActivityActionSheet)];
[controlToolbar setItems:[NSArray arrayWithObjects:spacer, cancelButton, setButton, nil]
animated:NO];
[activityActionSheet addSubview:controlToolbar];
[activityActionSheet showInView:self.view];
[activityActionSheet setBounds:CGRectMake(0, 0, 320, 485)];
}
And this is what I get:
How can I add a scroll to allow user click on button 6? Or, is there a better approach to do it?

Is there any reason why you're adding buttons as a subview of the UIActionSheet?
The preferred method is to provide additional button titles in via the otherButtonTitle method parameter:
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:#"Title"
delegate:self
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:#"Okay"
otherButtonTitles:#"One", #"Two", #"Three", #"Four", #"Five", #"Six", nil];
Then if you conform to the delegate, you can use the method actionSheet:didDismissWithButtonIndex: and determine which of the buttons was selected.

You should add an UIScrollView between your buttons and the ActivityActionSheet...
...
UIScrollView *myButtonScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0,0,160,441)];
for (int index = 0; index < 7; index++)
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:#selector(activitySelected:)
forControlEvents:UIControlEventTouchDown];
CGRect frame = CGRectMake(0.0, index*40 + 10, 160.0, 40.0);
button.frame = frame;
button.tag = index;
[button setTitle:[NSString stringWithFormat:#"Button %d", index] forState:UIControlStateNormal];
//[activityActionSheet addSubview:button];
[myButtonScroll addSubView:button];
}
[myButtonScroll setContentSize:CGSizeMake(160, 7*40 + 20)];
[activityActionSheet addSubView:myButtonScroll];
...
Assuming there are only 7 buttons on your view...

I would use a UIActionSheet only if you plan to insert the buttons through this method:
activityActionSheet = [[UIActionSheet alloc] initWithTitle:nil
delegate:nil
cancelButtonTitle:#""
destructiveButtonTitle:nil
otherButtonTitles:nil];
You can easily insert the buttons as a list of strings i.e. #"Button1",#"Button2",nil (with a nil delimiter at the end) and pass it in the otherButtonTitles parameter.
If you want to use customised views (adding buttons to the view), I would rather use another control (create my own UIView subclass that acts similarly to UIActionSheet).

Related

iOS8 drawing rectangle not showing

I have the below code to draw a datepicker (showing around the middle of the screen). It works in iOS 7 but in iOS 8 it is not showing. Only the "Select Expiry Date" is shown in a small box in the bottom of the screen.
menu = [[UIActionSheet alloc] initWithTitle:#"Select Expiry Date"
delegate:nil
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
pickerView = [[CDatePickerViewEx alloc] init];
CGRect pickerRect = pickerView.bounds;
pickerRect.origin.y = 50;
pickerRect.size.height = 100;
pickerView.frame = pickerRect;
UIButton *startbtn = [UIButton buttonWithType:UIButtonTypeCustom];
startbtn.frame = CGRectMake(240,12, 60, 25);
[startbtn setTitle:#"OK" forState:UIControlStateNormal];
[startbtn setBackgroundImage:[UIImage imageNamed:#"btn"] forState:UIControlStateNormal];
[startbtn addTarget:self action:#selector(pressedbuttonCancel:) forControlEvents:UIControlEventTouchUpInside];
[menu addSubview:pickerView];
[menu addSubview:startbtn];
[menu showInView:self];
[menu setFrame:CGRectMake(0,200,320, 260)];
Check if AutoLayout is enabled for your view, as well as Size classes. I ran into a similar issue before and remember of fixing it with programmatic constraints.

Putting 4 navigation bar buttons

i am new to iOS programming and i am wondering if it is possible to put 4 bar buttons into the UINavigationBar? I have tried a few methods that i have found on stack overflow but the buttons positions are not equal.
Here is a sample screenshot
.
The method that i used to code my navigation bar is:
UIToolBar *tools = [[UIToolBar alloc] initWithFrame:CGRectMake(0, 0, 100, 50)];
UIImage *backImage = [UIImage imagedName:#"backIcon.png"];
UIImage *shareImage = [UIImage imagedName:#"shareIcon.png"];
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[backButton setImage:backImage forState:UIControlStateNormal];
// create the button and assign the image to the leftsidebutton
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[backButton setImage:backImage forState:UIControlStateNormal];
backButton.frame = CGRectMake(0, 0, backImage.size.width, backImage.size.height);
[backButton addTarget:self.navigationController action:#selector(popViewControllerAnimated:) forControlEvents:UIControlEventTouchUpInside];
UIButton *shareButton = [UIButton buttonWithType:UIButtonTypeCustom];
[shareButton setImage:shareImage forState:UIControlStateNormal];
shareButton.frame = CGRectMake(0, 0, shareImage.size.width, shareImage.size.height);
//create space between the buttons
UIBarButtonItem *bi = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:NULL];
UIBarButtonItem *customBarButton = [[UIBarButtonItem alloc] initWithCustomView:backButton];
UIBarButtonItem *shareBarButton = [[UIBarButtonItem alloc] initWithCustomView:shareButton];
self.navigationItem.hidesBackButton = YES;
NSArray *leftActionButtonItems = #[customBarButton, bi, shareBarButton];
[tools setItems:leftActionButtonItems animated:NO];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:tools];
I did the same for rightBarButtonItem but it is not working so well.
Any help will be greatly appreciated! Thanks!
Try this one :
Edit :
it display Proper check it :
take .h file :
UIToolbar *toolBar_T;
UIBarButtonItem *item1,*item2,*item3,*item4;
take .m file :
toolBar_T=[[UIToolbar alloc] init];
toolBar_T.frame=CGRectMake(0,5,168, 44);
[self.view addSubview:toolBar_T];
UIButton *button0 = [UIButton buttonWithType:UIButtonTypeCustom];
[button0 setImage:[UIImage imageNamed:#"1.png"] forState:UIControlStateNormal];
[button0 addTarget:self action:#selector(button0:) forControlEvents:UIControlEventTouchUpInside];
button0.frame = CGRectMake(0, 7, 35,29);
item1 = [[UIBarButtonItem alloc] initWithCustomView:button0];
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
[button1 setImage:[UIImage imageNamed:#"2.png"] forState:UIControlStateNormal];
[button1 addTarget:self action:#selector(button1:) forControlEvents:UIControlEventTouchUpInside];
button1.frame = CGRectMake(0, 7, 35,29));
item2 = [[UIBarButtonItem alloc] initWithCustomView:button1];
UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
[button2 setImage:[UIImage imageNamed:#"3.png"] forState:UIControlStateNormal];
[button2 addTarget:self action:#selector(button2:) forControlEvents:UIControlEventTouchUpInside];
button2.frame = CGRectMake(0, 7, 35,29);
item3 = [[UIBarButtonItem alloc] initWithCustomView:button2];
UIButton *button3 = [UIButton buttonWithType:UIButtonTypeCustom];
[button3 setImage:[UIImage imageNamed:#"4.png"] forState:UIControlStateNormal];
[button3 addTarget:self action:#selector(button3:) forControlEvents:UIControlEventTouchUpInside];
button3.frame = CGRectMake(0, 7, 35,29);
item4 = [[UIBarButtonItem alloc] initWithCustomView:butto3];
NSArray *buttons = [NSArray arrayWithObjects: item1, item2,item3, nil];
[toolBar_T setItems: buttons animated:NO];
toolBar_T.barStyle=-1;// For Clear backgroud
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:toolBar_T];
Hope it will help.
Happy coding...
You can try smth like this...
-(void)setupToolbarButtons:(UIViewController *)navC
{
//create a toolbar in the right
UIToolbar *tools = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 135, 44.01)];
tools.delegate = self;
tools.barTintColor = [UIColor clearColor];
//create the array to hold the buttons, which then gets added to the toolbar
NSMutableArray *buttons = [[NSMutableArray alloc] initWithCapacity:4];
//create a standart 'play' button
UIBarButtonItem *bi = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemPlay target:self action:nil];
[buttons addObject:bi];
[bi release];
//create a standart 'stop' button
bi = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemStop target:self action:nil];
[buttons addObject:bi];
[bi release];
//create a standart 'fast forward' button
bi = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFastForward target:self action:nil];
[buttons addObject:bi];
[bi release];
//create a standart 'pause' button
bi = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemPause target:self action:nil];
[buttons addObject:bi];
[bi release];
//stick the buttons in the toolbar
[tools setItems:buttons animated:NO];
[buttons release];
//and put the toolbar in the nav bar
navC.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:tools];
[tools release];
}
...also you can add a spacer if it's needed
bi = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
[buttons addObject:bi];
[bi release];
and if something is wrong in vertical spacing, then add this
- (UIBarPosition)positionForBar:(id <UIBarPositioning>)bar
{
return UIBarPositionTopAttached;
}
UINavigationBar is Inherits from UIView, so you can simply add any object in.
Try the following code
self.navigationItem.leftBarButtonItems = [NSArray arrayWithObjects:customBarButton, bi, shareBarButton, nil];
self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:BUTTON OBJECTS, nil];

UIButton inside UITextField

I'm trying to put a UIButton inside UITextfield, to act as a "Done" button to resign the responder. I've tried with the following code, when the field starts editing the done button in the UITextField leftView is visible, however if I select a value from the datePicker or make any text input with the keyboard (in simulator), the done button disappears and the right clearButton is shown.
It seems like it toggles between them both, I changed textFld.LeftViewMode to display always the button but that is not what I'd like...
Thanks in advance!.
- (void)viewDidLoad
{
[super viewDidLoad];
txtFld = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 310, 25)];
txtFld.placeholder = #"__/__/____";
txtFld.font = [UIFont fontWithName:#"HelveticaNeue" size:11];
txtFld.textAlignment = NSTextAlignmentCenter;
txtFld.contentVerticalAlignment = UIControlContentHorizontalAlignmentCenter;
txtFld.contentHorizontalAlignment = UIControlContentVerticalAlignmentCenter;
txtFld.clearButtonMode = UITextFieldViewModeWhileEditing;
txtFld.borderStyle = UITextBorderStyleRoundedRect;
UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
doneButton.bounds = CGRectMake(0, 0, 40, 20);
doneButton.frame = CGRectMake(0.0, 0.0, 40.0, 20.0);
doneButton.titleLabel.font = [UIFont fontWithName:#"HelveticaNeue" size:10];
[doneButton setTitle:#"Done" forState:UIControlStateNormal];
doneButton.imageEdgeInsets = UIEdgeInsetsMake(0, -16, 0, 0);
[doneButton addTarget:self action:#selector(resignResponder:) forControlEvents:UIControlEventTouchUpInside];
doneButton.userInteractionEnabled = YES;
// Add button to the UITextField
txtFld.leftView = doneButton;
txtFld.leftViewMode = UITextFieldViewModeWhileEditing;
UIDatePicker *datePicker = [[UIDatePicker alloc] init];
datePicker.datePickerMode = UIDatePickerModeDate;
[datePicker addTarget:self action:#selector(datePickerValueChanged:) forControlEvents:UIControlEventValueChanged];
[txtFld setInputView:datePicker];
[self.view addSubview:txtFld];
}
-(void)resignResponder:(UIButton *)sender{
UITextField *associatedTxtFld = (UITextField *) sender.superview ;
if ([associatedTxtFld isFirstResponder]) {
[associatedTxtFld resignFirstResponder];
}
}
-(void)datePickerValueChanged:(id)sender{
UIDatePicker *picker = (UIDatePicker *)sender;
NSDateFormatter *formater = [[NSDateFormatter alloc]init];
[formater setDateFormat:#"dd-MM-yyyy"];
txtFld.text = [formater stringFromDate:picker.date];
}
The below code works fine for me . This could be a solution to resign your pickerview
UITextField *dispayNameField = [UITextField alloc]init];
UIButton *userStatusButton = [UIButton buttonWithType:UIButtonTypeCustom];
userStatusButton.frame=CGRectMake(0, 0, 20, 20);
[userStatusButton addTarget:self action:#selector(selectUserStatus) forControlEvents:UIControlEventTouchUpInside];
displayNameField.rightViewMode = UITextFieldViewModeAlways;
displayNameField.rightView = _userStatusButton;
-(void)selectUserStatus
{
displayNameField.inputView = _dataPickerView;
self.editUserProfileScrollView.scrollEnabled = YES;
UIActionSheet *actionSheetForDate = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
[actionSheetForDate showInView:self.parentViewController.tabBarController.view];
[actionSheetForDate setBounds:CGRectMake(0,0,320, 500)];
UIDatePicker = [[UIDatePicker alloc]initWithFrame:CGRectMake(0, 50, 320, 216)];
_dataPickerView.tag = 1;
_dataPickerView.showsSelectionIndicator = YES;
_dataPickerView.dataSource = self;
_dataPickerView.delegate = self;
[_actionSheetForDate addSubview:_dataPickerView];
[_actionSheetForDate sendSubviewToBack:_dataPickerView];
UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
pickerToolbar.barStyle = UIBarStyleBlackTranslucent;
[pickerToolbar sizeToFit];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(dismissActionSheetUserField)];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
UIBarButtonItem *btnBarCancel = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(dismissActionSheetUserField)];
[pickerToolbar setItems:[NSArray arrayWithObjects:btnBarCancel,flexSpace,doneButton, nil] animated:YES];
[_actionSheetForDate addSubview:pickerToolbar];
displayNameField.inputAccessoryView = pickerToolbar;
}

UIdate picker does not move freely

I have a UIbutton on my UIViewController. Clicking that opens up an UIActionSheet with a UIDatePicker on it and a close button. I set the UIDatePicker to date mode .So it shows up with todays date. Suppose i want to select 2015 instead of 2013, i have to scroll down. But it won't allow me to scroll down. I have to scroll up once to 2012 and then scroll down. This seems like a minor issue but the client wants to scroll properly down. I checked on my personal phone to set alarm, i can freely scroll down. I don't have to scroll up and then scroll down. So this is what happens on button click.
- (IBAction)btnShowDateNeededClick:(UIButton *)sender
{
UIDatePicker *datePickerView = [[UIDatePicker alloc]init];
datePickerView.datePickerMode = UIDatePickerModeTime;
self.actSheet = [[ UIActionSheet alloc]init];
UISegmentedControl *closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:#"Close"]];
closeButton.momentary = YES;
closeButton.frame = CGRectMake(260, 7.0f, 50.0f, 30.0f);
closeButton.segmentedControlStyle = UISegmentedControlStyleBar;
closeButton.tintColor = [UIColor blueColor];
closeButton.tag = 101;
[closeButton addTarget:self action:#selector(dismissActionSheetWithTime:) forControlEvents:UIControlEventValueChanged];
[self.actSheet addSubview:closeButton];
[self.actSheet showInView:self.view];
[self.actSheet addSubview:datePickerView];
[self.actSheet sendSubviewToBack:datePickerView];
[self.actSheet setBounds:CGRectMake(0, 0, 320, 500)];
}
As you see, i have datePicker, segment control as a button added to UIActionsheet. Now do i have to change settings or date picker? If you need more information, please ask. Thanks.
Try This. may be this help.
In .h File.
UIDatePicker *datePickerView;
UIActionSheet *Actionsheet1;
In .m File
In viewDidLoad Method
// Picker Initialization
datePickerView = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 40, 0, 0)];
datePickerView.datePickerMode = UIDatePickerModeDateAndTime;
Make a function which will be called on your Button click
-(void) Create_Start_Date_Picker_Function
{
Actionsheet1 = [[UIActionSheet alloc] initWithTitle:nil delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
Actionsheet1.actionSheetStyle = UIActionSheetStyleBlackOpaque;
[Actionsheet1 addSubview: datePickerView];
[Actionsheet1 showInView:self.view];
[Actionsheet1 setBounds:CGRectMake(0, 0, 320, 485)];
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 blueColor];
[closeButton addTarget:self action:#selector(Dismiss_Actionshit1) forControlEvents:UIControlEventValueChanged];
[Actionsheet1 addSubview:closeButton];
}
And in method for dismiss actionshit.
-(void) Dismiss_Actionshit1
{
// Your action sheet dismiss code
}

Initial and place an UIButton in an UIActionSheet with an UIPickerView

I am implementing picker by Add UIPickerView & a Button in Action sheet - How?. I tried to replace the UISegmentedControl with a UIButton but it does not working. Could someone remind me why UIButton cannot be shown here?
Thank you.
- (IBAction) makeButtonPressed: (id) sender;
{
UIActionSheet *actionSheet = [[UIActionSheet alloc] init];
[actionSheet setTitle:#"Select Make"];
[actionSheet setActionSheetStyle:UIActionSheetStyleBlackOpaque];
UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 44, 0, 0)];
pickerView.dataSource = self;
pickerView.delegate = self;
pickerView.showsSelectionIndicator = YES;
[actionSheet addSubview:pickerView];
// 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(makeDone:) forControlEvents:UIControlEventValueChanged];
// [actionSheet addSubview:doneButton];
UIButton *doneButton = [[UIButton alloc] init];
doneButton.frame = CGRectMake(260, 7.0f, 50.0f, 30.0f);
[doneButton addTarget:self action:#selector(makeDone:) forControlEvents:UIControlEventValueChanged];
[actionSheet addSubview:doneButton];
[actionSheet showInView:[[UIApplication sharedApplication] keyWindow]];
[actionSheet setBounds:CGRectMake(0, 0, 320, 460)];
}
The designated initializer for UIButton is +buttonWithType:. You should create your button with one of the predefined styles, or use UIButtonTypeCustom.
UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeRoundRect];
doneButton.frame = CGRectMake(260, 7.0f, 50.0f, 30.0f);
[doneButton addTarget:self action:#selector(makeDone:) forControlEvents:UIControlEventValueChanged];
[actionSheet addSubview:doneButton];

Resources