ActionSheet keeps appearing for every button - ios

I have a method which throws up an actionSheet which is populated by an array.
The problem is that as soon as I place that method in the sender for 1 button, it fires off for every single button press.
I only want it to display for the one button (btnPortfolio).
Here is the code for my buttons:
- (IBAction)btnPortfolio:(id)sender {
[self populatePortfolioList];
}
- (IBAction)btnAdd:(id)sender {
}
- (IBAction)btnRefresh:(id)sender {
}
and here is my method:
-(void)populatePortfolioList{
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:#"Choose your option"
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
for (NSString *title in portfolio_list) {
[actionSheet addButtonWithTitle:title];
}
actionSheet.cancelButtonIndex = [actionSheet addButtonWithTitle:#"Cancel"];
[actionSheet showInView:self.view];
}

since you are using IBActions, I assume that you have somehow created the methods by ctrl+dragging from the buttons into your code? is that the case? it might be possible that you accidentally connected more than one of your buttons with the method btnPortfolio which causes populatePortfolioList to be called any time the button is pressed

Related

Not able to change text size of actionsheet button text

Hi I am facing very different kind of problem. I am using actionsheet in my iPhone application. So when I use predefine values of otherButtonTitles and if I try to change the text size then it works fine. But when I add buttons dynamically then it's not working properly.
My code looks like this:
- (void)showActions
{
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil
delegate:self
cancelButtonTitle:cancelTitle
destructiveButtonTitle:nil
otherButtonTitles:notifications, about, faq, settings, logout, nil];
[actionSheet showInView:self.view];
}
- (void)willPresentActionSheet:(UIActionSheet *)actionSheet {
for (UIView *_currentView in actionSheet.subviews) {
if ([_currentView isKindOfClass:[UIButton class]]) {
UIButton *button = (UIButton *)_currentView;
button.titleLabel.font = [UIFont systemFontOfSize:FONT_SIZE_16];
}
}
}
So above thing working fine. But if I try following things its not working
-(void) showCategories
{
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
// categoryData is string array....
for (NSString *category in categoryData) {
[actionSheet addButtonWithTitle:category];
}
actionSheet.cancelButtonIndex = [actionSheet addButtonWithTitle:#"Cancel"];
[actionSheet showInView:self.view];
}
And I tried to change text size like above then its not working. Is there any reason? Need help. Thank you.
When total number of buttons exceeds height to display without
scrolling this problem occurs. (If anyone else finds the way then
please post an answer. But i think there's no answer to this !!)
On 4 inch device (640x1136 resolution), number of max buttons displayed are 11
On 3.5 inch device (640x960 resolution), number of max buttons displayed are 8.
A piece of advice:: I think if you use actionsheet this way to display more than 10 elements, Apple could reject your app. Use UIPickerView for this kind of behaviour.

iOS - alertView shouldEnableFirstButton

I want the user to not be able to click the cancel button on UIAlertView, yet I still want it to be there, but shaded out. I know a shouldEnableFirstButton function is there but it doesn't involve cancel button. Do you have any idea how to do it?
Thanks.
Use the following:
UIAlertView *vw = [[UIAlertView alloc] initWithTitle:#"Information" message:#"This is a message" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:#"Cancel", nil];
[vw show];
So, essentially you are setting the "Cancel" as other button.
Now, the following will solve your problem:
- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView
{
return NO;
}
If you want to perform some action in OK button press you can always do that here:
- (void)alertViewCancel:(UIAlertView *)alertView

Prompt for user input and store the data

I just started iOS development, and now I'm stuck. I have a table cell, where the user cann see data. Next to it, there are two buttons. One to delete, one to edit. The edit part is where I'm stuck. I have a label field. When the user clicks on the edit button a prompt should come where the user can type in data. I already found something to do it. But there's no save button and I don't know how I could then save the variable to an NSString
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:#"Name der neuen Liste" message:#"" delegate:self cancelButtonTitle:#"Abbrechen" otherButtonTitles:nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[alert show];
I currently have this. lNow a prompt pops up, where I can write something, but not save. I just can press cancel afterwards. How do I work with user input in iOS? I don't want to use the label as an UITextField since it has to be clickable to perform another action.
Try this:
- (void)noteInput
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"input"
message:#"input"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Save", nil];
[alertView setAlertViewStyle:UIAlertViewStylePlainTextInput];
[alertView show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(buttonIndex == 1)
{
UITextField *noteText = [alertView textFieldAtIndex:0];
NSString *note = noteText.text;
}
}
Try adding a Save button in otherButtonTitles. Then, in your UIAlertViewDelegate:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 1) {
// Save button pressed
}
}

Place Cancel button in UIActionSheet at the bottom

In UIActionSheet i have a uitableview and cancel button. By default cancel button appears at the top. I want to make it appear at the bottom after tableview. How do i do that?
it is work for me:
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:#"SomeTitle" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil, nil];
[actionSheet addButtonWithTitle:#"Some Action"];
[actionSheet addButtonWithTitle:#"Cancel"];
actionSheet.cancelButtonIndex = actionSheet.numberOfButtons -1;
I tried this and Cancel button is at the bottom:
menu = [[UIActionSheet alloc] initWithTitle:#"Actionsheet"
delegate:self
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:#"destructive"
otherButtonTitles:#"other1", nil];
menu.actionSheetStyle = UIActionSheetStyleDefault;
[menu addButtonWithTitle:#"Cancel"];
By default Cancel button is hidden, adding a Cancel will shows it.
BUT: if you have additional gui elements on your actionsheet you have to
option1) position those to hide other buttons (to have space for your gui element). Its a tweak, but can work in some situation OR
option2) you have to manually add your button to the actionsheet
Actionsheet's built-in buttons are not align-able to the bottom freely because the purpose is different for this built-in gui elements.
See this: Adding UIPickerView to UIActionSheet (buttons at the bottom)
Example in Swift:
func presentMyActionSheetIOS7() {
let actionSheet: UIActionSheet = UIActionSheet(title: "What do you want to do?", delegate: self, cancelButtonTitle: nil, destructiveButtonTitle: nil)
actionSheet.addButtonWithTitle("Change Transparency")
actionSheet.addButtonWithTitle("Hide Photo")
actionSheet.addButtonWithTitle("Cancel")
actionSheet.cancelButtonIndex = actionSheet.numberOfButtons - 1
actionSheet.showInView(self.view)
}
func actionSheet(actionSheet: UIActionSheet, clickedButtonAtIndex buttonIndex: Int) {
switch buttonIndex {
case 0:
println("Change Transparency")
case 1:
println("Hide Photo")
case 2:
println("Cancel")
default:
println("Default")
}
}

Create UIActionSheet 'otherButtons' by passing in array, not varlist

I have an array of strings that I want to use for button titles on a UIActionSheet. Unfortunately, the otherButtonTitles: argument in the method invocation takes a variable length list of strings, not an array.
So how I can I pass these titles into the UIActionSheet? The workaround I've seen suggested is to pass nil into otherButtonTitles:, then specify the button titles individually by using addButtonWithTitle:. But this has the problem of moving the "Cancel" button to the first position on the UIActionSheet rather than the last; I want it to be the last one.
Is there a way to 1) pass an array in lieu of a variable list of strings, or alternatively 2) move the cancel button to the bottom of the UIActionSheet?
Thanks.
I got this to work (you just need to, be ok with a regular button, and just add it after :
NSArray *array = #[#"1st Button",#"2nd Button",#"3rd Button",#"4th Button"];
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:#"Title Here"
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
// ObjC Fast Enumeration
for (NSString *title in array) {
[actionSheet addButtonWithTitle:title];
}
actionSheet.cancelButtonIndex = [actionSheet addButtonWithTitle:#"Cancel"];
[actionSheet showInView:self.view];
One little note: [actionSheet addButtonWithTitle:] returns the index of that button, so to be safe and "clean" you can do this:
actionSheet.cancelButtonIndex = [actionSheet addButtonWithTitle:#"Cancel"];
Taking Jaba's and Nick's answers and extending them a little further. To incorporate a destruction button into this solution:
// Create action sheet
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:title
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
// Action Buttons
for (NSString *actionName in actionNames){
[actionSheet addButtonWithTitle: actionName];
}
// Destruction Button
if (destructiveName.length > 0){
[actionSheet setDestructiveButtonIndex:[actionSheet addButtonWithTitle: destructiveName]];
}
// Cancel Button
[actionSheet setCancelButtonIndex: [actionSheet addButtonWithTitle:#"Cancel"]];
// Present Action Sheet
[actionSheet showInView: self.view];
There is the swift version for the response :
//array with button titles
private var values = ["Value 1", "Value 2", "Value 3"]
//create action sheet
let actionSheet = UIActionSheet(title: nil, delegate: self, cancelButtonTitle: nil, destructiveButtonTitle: nil)
//for each value in array
for value in values{
//add a button
actionSheet.addButtonWithTitle(value as String)
}
//display action sheet
actionSheet.showInView(self.view)
To get value selected, add delegate to your ViewController :
class MyViewController: UIViewController, UIActionSheetDelegate
And implement the method "clickedButtonAtIndex"
func actionSheet(actionSheet: UIActionSheet, clickedButtonAtIndex buttonIndex: Int) {
let selectedValue : String = values[buttonIndex]
}

Resources