How can I deal with UIActionSheet and Three20? - objective-c

How I can deal with UIActionSheet and three20?
I am following this tutorial.
I know about displaying a single photo by clicking on one from the thumbnail viewer is provided to you for free by Three20. In addition, the library also provides all of the native functions such as pinch-to-zoom, swiping to navigate and tapping to hide/show the navigation arrows and back button.
I want to add a button that if clicked by the user, would display a UIActionSheet with options for sharing the photo by mail or MMS and save( using UIImageWriteToSavedPhotosAlbum )and the user would choose one of them.
I see a lot of tutorials about this but I need to do this by my self.

in .h file add
UIActionSheet *actionViewPopup;
in .m file
actionViewPopup = [[UIActionSheet alloc] initWithTitle:nil
delegate:self
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:#"email",#"sms",nil];
[actionViewPopup showInView:self.parentViewController.tabBarController.view];
CODE FOR HANDLING CLICK OF ACTIONSHEET
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
//THIS METHOD SHOWS WHICH BUTTON FROM ACTION SHEET IS PRESSED
if (buttonIndex == 0)
{
NSLog(#"email clicked");
//do code for email (mail composer)
}
if (buttonIndex == 1)
{
NSLog(#"sms clicked");
//do code for sms
}
}

Related

How to show an alert before leaving the table view.?

I am working with a master detail application. In the master section records are listed, and detail section shows each record details. The detail section is a table view where we can edit each record. the problem is that "while editing a record,if i tap a record on the other side, any changes that i have made on the original record are lost and new record details are shown in the table view"...
can anyone please tell me how to show an alert that asks for "save or cancel" , before "showing the new record details"..
any changes that i have made on the original record are lost
A common rule in developing applications is
NEVER loose the user's work
So it would maybe just be the best idea to just save what the user changed.
But let's head to your actual question:
can anyone please tell me how to show an alert
I think you mean those alters looking like push notifications with two buttons.
You create them with UIAlertView.
Then in the alert's delegate you can figure out which button was pressed and you can decide how to go on. Just check out the documentation for UIAlertView - it's pretty simple :)
try this,
-(void)tableView:(UITableView *)tableView1 didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:LString(#"ISCO_FLOW_CALC") message:LString(#"DELETE_MESSAGE") delegate:self cancelButtonTitle:LString(#"CANCEL") otherButtonTitles:LString(#"SAVE"), nil];
alert.tag=11;
[alert show];
[alert release];
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
if(buttonIndex==1 && alertView.tag==11)
{
//your save data action;
}
if(buttonIndex==0 && alertView.tag==11)
{
//your Cancel data action;
}
}
Keep a reference to your DetailViewController in the MasterViewController
eg. in your MasterViewController.h:
DetailViewController *detailVC;
Set this reference to your most recent DetailViewController in didSelectRowAtIndexPath and always check if it is not nil before showing a new detailVC
DetailViewController *controller=[[DetailViewController alloc] init]; //Or similar
if (detail){
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Save or Cancel" message:#"Save or Cancel" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Save",nil];
[alert show];
}
else{
detailVC=controller;
//Show controller
}
Now show the new detailViewController after the user tapped a button of the UIAlertView:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex==0) { //Tapped cancel
//show detailVC
}
else{ //Tapped save
//save
//Show detailVC
}
}
Hope this helps

How can I show UIActionSheet to confirm moving back in Navigation View

Using Xcode I have View A that navigates to View B.
Upon pressing the Back UIBarButtonItem, I'm trying present the user with a UIActionSheet to confirm navigation to move back to View A.
What do I need to do in code to stop the view from navigating back and then (depending on user input) move back or stay on the current screen?
add a backbutton programmatically.
eg.
UIButton *backBtn= [[UIButton alloc] initWithFrame:CGRectMake(0,0,54,30)];
[backBtn addTarget:self action:#selector(backButtonPressed:)forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *backBarButton = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
[backBtn release];
[[self navigationItem] setLeftBarButtonItem:backBarButton];
[backBarButton release];
//backButtonPressed is the selector for backBtn
Then present you ActionSheet from that selector and based on user either navigate to previous viewController or dont.
To navigate to previous page, use popViewMethod.
`
You should not present UIActionSheet for every other action.It would be better to use UIAlertView for this purpose. According to Apple UIActionsheet Guidelines :-
Provide alternate ways a task can be completed. An action sheet allows you to provide a range of choices that make sense in the context of the current task, without giving these choices a permanent place in the user interface.
Get confirmation before completing a potentially dangerous task. An action sheet prompts users to think about the potentially dangerous effects of the step they’re about to take and gives them some alternatives. This type of communication is particularly important on iOS-based devices because sometimes users tap controls without meaning to.
for UIAlertView :-
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Alert View"
message:#"Do You want to go back to previous screen?"
delegate:self
cancelButtonTitle:#"NO"
otherButtonTitles:#"YES",nil];
[alertView show];
[alertView release];
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0) {
NSLog(#"THE 'NO' BUTTON WAS PRESSED");
}
if (buttonIndex == 1) {
NSLog(#"THE 'YES' BUTTON WAS PRESSED");
}
}
Implement this on action of back button of UINavigationController.According to the buttons pressed "YES" or "NO" , you can allow navigation.Also conform to UIAlerrtVIewDelegate protocol.

Is there a way to navigate to other views using a UIActionSheet?

Is there is a way to use an Action sheet to navigate to another view?
I have a button - "Go" and an Action sheet that asks "Do you want to proceed?" and in it "Yes" and "Cancel"
Can I can navigate to another view when pressing yes?
Yes, it is possible. To do this, the viewController that represents the UIActionSheet needs to adopt UIActionSheetDelegate. Upon dismissing the action sheet with either Yes or Cancel, - actionSheet:didDismissWithButtonIndex: method gets called, and from there you can navigate to another view or just ignore it.
References:
http://developer.apple.com/library/ios/#documentation/uikit/reference/UIActionSheet_Class/Reference/Reference.html#//apple_ref/occ/cl/UIActionSheet
http://developer.apple.com/library/ios/#documentation/uikit/reference/UIModalViewDelegate_Protocol/UIActionSheetDelegate/UIActionSheetDelegate.html
Edit:
#interface MyViewController : UIViewController <UIActionSheetDelegate>
-(IBAction)showActionSheet:(id)sender;
#end
#implementation MyViewController
-(IBAction)showActionSheet:(id)sender {
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:#"Do you want to procced?" delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:#"YES" otherButtonTitles:nil];
[actionSheet showInView:self.view];
}
-(void) actionSheet: (UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex {
switch (buttonIndex) {
case 0: {
// for illustration
// let's assume (1) you have a navigation controller
// (2) you are using storyboard
// (3) in the storyboard, you have a viewController with identifier MyChildViewControllerIdentifier
MyChildViewController *mcvc = [self.storyboard instantiateViewControllerWithIdentifier:#"MyChildViewControllerIdentifier"];
[self.navigationController pushViewController:mcvc animated:YES];
break;
}
default:
break;
}
}
P.S. I didn't run it, if there is any error, let me know to fix it.
Dismiss the actionsheet and then [self presentmodalviewcontroller:myview animated:yes]

Auto Click on UIAlertView

I have an app which displays a UIAlertView with two choices "Install" and "Cancel"
Currently testers manually click on "Install" button to initiate the install.
What i would like to do is to automate this process... Is there a way i can get the handle /reference to this UIAlertView and automate the clicking of the "Install" button?
PS: The scenario is this. I use TestFlightApp(testflightapp.com) , Now my code clicks on the URL provided by TestFlightApp to install this build , however when i click the link an alertview (possibly displayed by safari) comes up. I want to get handle of this or to dismiss it
You cannot. That UIAlertView is displayed from a MobileSafari page (albeit in a web clip) and you have no control over the UIAlertView or its delegates.
This is intentional, the user should be the only one able to confirm something as significant as an app install.
I have an app which displays a UIAlertView with two choices "Install" and "Cancel" Currently testers manually click on "Install" button to initiate the install. What i would like to do is to automate this process.
The UIAlertView is displayed by a different app
These two are contradictionary. However, if it really is your app that displays the alert view:
Part one:
If you got the handle, you can use this message to dismiss it:
[alertView dismissWithClickedButtonIndex:0 animated:YES];
Part two: how to get the first UIAlertView object in your window
- (UIAlertView *) recuresiveSearchAlertViewInView:(UIView *)mainView
{
if ([mainView isKindOfClass:[UIAlertView class]])
return (UIAlertView *)mainView;
UIView *found = nil;
for (UIView *view in mainView.subviews)
{
if ([view isKindOfClass:[UIAlertView class]])
{
found = view;
break;
}
if (!found)
found = [self recursiveSearchAlertViewInView:view];
}
return found;
}
then call:
UIAlertView *av = [self recursiveSearchAlertViewInView:[[UIApplication sharedApplication] keyWindow];

iPad UIActionSheet - Not displaying the last added button

I'm trying to display a UIActionSheet from my iPad. Here's the code that I'm using:
-(void) presentMenu {
UIActionSheet *popupMenu = [[UIActionSheet alloc] initWithTitle:#"Menu" delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:nil];
for (NSString *option in _menuItems) {
[popupMenu addButtonWithTitle:option];
}
popupMenu.actionSheetStyle = UIActionSheetStyleBlackOpaque;
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
[popupMenu showFromTabBar:_appDelegate.tabBar.tabBar];
}
else if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
[popupMenu showFromBarButtonItem:self.navigationItem.rightBarButtonItem animated:YES];
}
[popupMenu release];
return;
}
The iPhone version of the program displays all the buttons in _menuItems, but the iPad version just ignores the last item from that array. Does anyone know why this might be happening?
Thanks,
Teja.
Found the answer as soon as I typed out this post. Somehow removing the "Cancel" button causes both the buttons to come up. Weird.
EDIT: Although, this is really annoying because all my button indices change between the iPhone and the iPad versions (The iPhone still needs the cancel button). How do I handle this?
I think what iOS is doing is it's expecting the last button to be the cancel button (regardless of whether it is or not) and is removing it, but maybe only for iPads. This is probably because a user can tap outside the action sheet to dismiss it. The problem I have with Apple's design choice is that it may not always be evident that the dialog can or should be dismissed in that way.
For example, I am showing my action sheet by calling [actionSheet showInView:self.view]; This causes the entire view to be grayed with the action sheet displaying in the middle of the device. Users are going to--rightly, in my opinion--assume that they have to choose one of the buttons.
I understand there are other action sheet display mechanisms--like the one that displays it as a bubble attached to a bar button item--where a cancel button is obviously redundant. It would be nice if Apple allowed for more flexibility here. For my app, I am probably going to have to add a dummy button to the end of the array I'm passing into my custom constructor, knowing that iOS will hide it. If the behavior changes in a future release of iOS... well, I'll just have to address it at that time.
In your case, I recommend not using the constructor that takes cancelButtonTitle and destructiveButtonTitle. Instead, subclass UIActionSheet and add buttons manually using the method above. Then, set cancelButtonIndex and destructiveButtonIndex to the desired indices. Remember that you don't have to set those two properties; they default to -1 (no button). Also, remember to abide by the HIG regarding the position of your buttons.
Here's one of my subclass' constructors (edited for brevity), just to give you an idea:
- (instancetype)initWithTitle:(NSString *)title
buttonTitles:(NSArray *)buttonTitles
cancelButtonIndex:(NSInteger)cancelButtonIndex
destructiveButtonIndex:(NSInteger)destructiveButtonIndex
{
self = [super initWithTitle:title delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
if (self)
{
if (buttonTitles)
{
[buttonTitles enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop)
{
[self addButtonWithTitle:obj];
}];
}
self.cancelButtonIndex = cancelButtonIndex;
self.destructiveButtonIndex = destructiveButtonIndex;
if (self.cancelButtonIndex > -1)
{
[self addButtonWithTitle:#""];
}
}
return self;
}