How can I achieve a similar menu view? [iOS] - ios

Is there a way to achieve this menu easily with the sdk or do I have to make it manually (combining an overlay view and other view with the buttons)?
Thanks in advance!

That's UIActionSheet class from standard UIKit framework

Easy example from Apple's documentation of UIActionSheet extended a bit by me to fire call action.
Delcare in the global scope of the ViewController:
UIActionSheet * actionSheet; and UIView * yourView;
Int the viewDidLoad:
actionSheet = [[UIActionSheet alloc] initWithTitle:nil
delegate:self
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:#"Delete Note"
otherButtonTitles:#"Call",#"Add a Contact",nil];
[yourView = self.view]
To fire the menu by some declared button with IBAction you will need:
-(IBAction)viewMapButton:(id) sender
{
[actionSheet showInView:yourView];
}
To take appropriate action depending on user choice declare following method and check what [actionSheet buttonTitleAtIndex:buttonIndex] was equal to:
- (void)actionSheet:(UIActionSheet *)actionSheet
clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString * what_action = [actionSheet buttonTitleAtIndex:buttonIndex];
NSLog(#"The %# button was tapped.", what_action);
if ([what_action isEqualToString:#"Call"])
{
NSString *phoneNumber = [[NSString alloc]
initWithString:#"telprompt:1234567890"];
[[UIApplication sharedApplication]
openURL:[NSURL URLWithString:phoneNumber]];
}
}
[NOTE: Firing of call does not work on iOS Simulator]

Related

UIAlertview clicked button at index delegate method not being called

Am new to AlertViews with actions. Have set mine up as in the examples I found here including setting the delegate method in my .h, but when I debug find that it is not reaching my clickedButtonAtIndex method. Probably missing something simple :-/ Any help solving this would be appreciated, thanks.
Here's my code:
.h file
#interface LineDetailViewController : UIViewController <UIAlertViewDelegate>
. m file:
- (IBAction)removeLineButton:(UIButton *)sender {
NSString * requestSubmitText = [NSString stringWithFormat:#"Hey Bro are you sure you want to remove this line you created, it will also remove all the reviews attached and can not be undone?"];
UIAlertView *removeLineRequest = [[UIAlertView alloc] initWithTitle:#"WARNING!!"
message:requestSubmitText
delegate:nil
cancelButtonTitle:#"Remove It"
otherButtonTitles:#"Cancel",nil];
[removeLineRequest show];
- (void)alertView:(UIAlertView *)alertView
clickedButtonAtIndex:(NSInteger)buttonIndex{
//if (buttonIndex == [alertView cancelButtonIndex]){
if (buttonIndex == 0){
NSLog(#"Cancel button pressed");
}else{
NSLog(#"other button pressed");
}
}
You have to set delegate to self
UIAlertView *removeLineRequest = [[UIAlertView alloc] initWithTitle:#"WARNING!!"
message:requestSubmitText
delegate:self // <- here
cancelButtonTitle:#"Remove It"
otherButtonTitles:#"Cancel",nil];
Call removeLineRequest.delegate = self.
While creating your alert view set your delegate to self . And you have already implemented the UIAlertViewDelegate.
To solve your problem, just set the delegate to self instead of nil.
Some additional info,
UIAlertView and UIAlertViewDelegate have been deprecated. As per the documentation,
UIAlertView is deprecated. Use UIAlertController with a preferredStyle of UIAlertControllerStyleAlert instead.
Relevant tutorial here

ActionSheet keeps appearing for every button

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

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.

send action from button in collection view

I have a query that pulls the 10 nearest objects and displays the title of each in a button in a collection view. What I am trying to do is have an action that opens a new view controller displaying information of the object chosen from the collection view. What I need help with is connecting the chosen object in the buttons action. I am new to Xcode and could use some advice. This is the code defining the query:
int i = 0;
for (PFObject *object in objects) {
if (i >= [self.EventTitles count]) break;//to make sure we only write up to the max number of UILabels available in EventTitles
[(UIButton *)self.EventButtons[i] setTitle:[object objectForKey:#"name"] forState:UIControlStateNormal];
[(UIButton *)self.EventButtons[i] addTarget:self action:(social:) forControlEvents:UIControlEventTouchUpInside];
i++;
}
And then there is the code with the action
- (IBAction)social:(id)sender{
UIActionSheet *share = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:#"Close" destructiveButtonTitle:nil otherButtonTitles:#"Call",#"Directions",#"Website",#"Checkin", nil];
[share showInView:self.view];
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
//Each button title we gave to our action sheet is given a tag starting with 0.
if (buttonIndex == 0) {
NSString* yourActualNumber = [NSString stringWithFormat:#"tel:%#",????????];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:yourActualNumber]];
NSLog(#"phone");
//Check Twitter accessibility and at least one account is setup.
}
I just do not know how to connect the object to the action, and then display that information on the new view controller.
UPDATE: I updated the code with the new line in the query, however it fails stating that the action social is undeclared however it is? I also changed the action to perform an action sheet rather than a new view controller. What I need now it a connection from the object to the action and how to write in the object for key appropriately in the action sheet function.
What I need help with is connecting the chosen object in the buttons
action.
[button addTarget:self action:#selector(DetailEvent1:) forControlEvents:UIControlEventTouchUpInside]
and then display that information on the new view controller.
I don't really know what you are asking here, if you clarify I'll try my best to help :)
Here is an example where an action sheet is displayed when an item is selected. I'm simply using the index path of the selected item, but I assume you have some other data you'd need to save so the Action Sheet delegate can use it.
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
NSString *str = [NSString stringWithFormat:#"Call number %d", indexPath.row];
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:(id < UIActionSheetDelegate >)self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:str, nil];
[actionSheet showInView:self.view];
}

How to access UIButton in UIAlertView

I'd like to selectively enable the 'save' button on my UIAlertView so that you can't save a file with no name. I can listen to text changed events for the attached UITextView (the style is UIAlertViewStylePlainText), but I can't see how to access the buttons so that I can do the enable/disable. I've tried iterating through [alertView subviews], but there is only the label in there (no buttons). Where do I need to look to directly access the UIButtons attached to a UIAlertView?
Simple, just implement the UIAlertViewDelegate in your class and utilize the alertViewShouldEnableFirstOtherButton: delegate method. You can use this to check the length of the text field, and enable the button accordingly...
- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView
{
return [[[alertView textFieldAtIndex:0] text] length] > 0;
}
Make sure you set your view controller to conform to this delegate in your interface using < UIAlertViewDelegate > and to set this class as the alerts delegate upon instantiation.
Instantiate your alertview:
UIAlertView *av = [UIAlertView alloc] initWithTitle:#"my title" message:#"message" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Save", nil];
});
Then make sure your class implements UIAlertViewDelegate, and look for the button index of the button you want to listen to:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex==1) {
//save here
}
}
0x7fffffff posted right answer, but if someone is looking for answer how to add that textfield and save button to UIAlertView, here is the answer:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Save" message:#"" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Save", nil];
[alert setAlertViewStyle:UIAlertViewStylePlainTextInput];
This delegate method:
- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView {
return [[[alertView textFieldAtIndex:0] text] length] > 0;
}
won't work if you add that save button with addButtonWithTitle: method.
And when save button is clicked, this delegate method is called and here you can read what user wrote to that text field:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if(buttonIndex == 1) {
// Save button clicked
NSLog(#"%#", [[alertView textFieldAtIndex:0] text]);
}
}
You can't access the buttons of an alert view as of iOS 7. Unfortunately they made the subviews completely hidden and there's no way to access them.

Resources