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

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.

Related

Why UIAlertView prevents my modal view controller from being displayed?

Let's say that when the user presses a button a message needs to be shown and then a modal view controller is displayed.
I would write something like that :
- (void)pickImageButtonPressed:(id)button {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Alert"
message:#"Some alert"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[[UIApplication sharedApplication].keyWindow.rootViewController
presentModalViewController:picker animated:YES];
[picker release];
}
With this code, the modal view controller is simply not displayed !
However, if I switch the two blocks of code (show modal first, then show the alert), the view controller is displayed modally and the alert shows up above it, exactly as I want.
My question is: why the alert prevents the modal view controller from being displayed ?
Please note that I do not want to wait for alert to be dismissed before calling presentModalViewController.
Update :
I figured out that my production code was a bit more complicated than what I first put. I updated the example code and then finally found a simple example to reproduce the problem.
Note that in my production code, the popup is displayed from a place where I don't have any reference to a view controller, that's why I use [UIApplication sharedApplication].keyWindow.rootViewController instead of a direct reference to a view controller
I finally found the answer. The issue really was on this line:
[[UIApplication sharedApplication].keyWindow.rootViewController
presentModalViewController:picker animated:YES];
When an alert is displayed, [UIApplication sharedApplication].keyWindow.rootViewController is nil !. That's why the picker was never displayed. And that's why it worked when I switched the two blocks.
Now I need to find a way to get the most relevant view controller to present the picker modally...

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 deal with UIActionSheet and Three20?

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
}
}

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;
}

How to move the buttons in a UIAlertView to make room for an inserted UITextField?

[EDIT] Hmm. Perhaps this question should be titled "what is the default user-input dialog view called in CocoaTouch?" I realize that I can create an entire view that is exactly what I want, and wrap it in a view controller and presentModalView -- but I was sort of hoping that there was a standard, normal user-input "dialog" view that came-with Cocoa-touch. "Enter your name", "enter text to search", etc., are VERY common things!
Anyway... here's the question as I originally asked it:
This code:
UIAlertView* find = [[UIAlertView alloc] init];
[find setDelegate:self];
[find setTitle:#"Find"];
[find addButtonWithTitle:#"Cancel"];
[find addButtonWithTitle:#"Find & Bring"];
[find addButtonWithTitle:#"Find & Go"];
[find addButtonWithTitle:#"Go To Next"];
[find addSubview:_findText];
CGRect frm = find.frame;
int height = frm.size.height + _findText.frame.size.height + 100; // note how even 100 has no effect.
[find setFrame:CGRectMake(frm.origin.x, frm.origin.y, frm.size.width, height)];
[find setNeedsLayout];
[find show];
[find release];
Produces this Alert view:
Find Alert http://www.publicplayground.com/IMGs/Misc/FindAlert.png
(I started with the code from this question by emi1Faber, and it works as advertised; however, as I state in my comment, the cancel button overlays the text field.)
How do I reshuffle everything to make the text field fit properly? [findAlert setNeedsLayout] doesn't seem to do anything, even after I [findAlert setFrame:tallerFrame]. Hints?
Thanks!
The simplest (and most proper way) to move the text view down is to add a message
[find setMessage:#"\n"];
Also, the reason your frame isn't taking effect is that -show sets the frame and creates the view hierarchy before starting the animation. You should also make the text view the first responder so the keyboard pops up.
Full example:
// Create Alert
UIAlertView* av = [UIAlertView new];
av.title = #"Find";
// Add Buttons
[av addButtonWithTitle:#"Cancel"];
[av addButtonWithTitle:#"Find & Bring"];
[av addButtonWithTitle:#"Find & Go"];
[av addButtonWithTitle:#"Go to Next"];
// Make Space for Text View
av.message = #"\n";
// Have Alert View create its view heirarchy, set its frame and begin bounce animation
[av show];
// Adjust the frame
CGRect frame = av.frame;
frame.origin.y -= 100.0f;
av.frame = frame;
// Add Text Field
UITextField* text = [[UITextField alloc] initWithFrame:CGRectMake(20.0, 45.0, 245.0, 25.0)];
text.borderStyle = UITextBorderStyleRoundedRect;
[av addSubview:text];
[text becomeFirstResponder];
Note: You can also modify the subviews of UIAlertView, but since Apple has already changed the UIAlertView layout once you should check their class descriptions and frames against known values before setting new ones. You can even get something like this:
(source: booleanmagic.com)
Even if you can get this working it's not going to be very iPhone-y. The UIAlertView really is not designed for user input like this. If you look in all the Apple apps you'll see that they use a new view that displayed using the presentModalViewController: method of UIViewController.
Edit: This advice is no longer as true as it was when I wrote it. Apple have increasingly used alert views as text entry boxes and iOS5 even includes native support without having to mess around with views (check out the alertViewStyle property).
I think maybe if you need to have four buttons then using a custom UIViewController is probably still the right way to go. But if you just want to enter a password with OK/Cancel buttons then it's fine.
Zoul proposed the best method, to capture user input just do:
a) Add the UITextFieldDelegate protocol to your class.
b) Do something like
UIAlertView *insertScore = [UIAlertView new];
[insertScore setDelegate:self];
[insertScore setTitle:#"New Title!"];
[insertScore addButtonWithTitle:#"Cancel"];
[insertScore addButtonWithTitle:#"Ok"];
insertScore.message = #"\n";
[insertScore addTextFieldWithValue:#"Input" label:#"player"];
[[insertScore textField] setDelegate:self];
[insertScore show];
[insertScore release];
c) The crucial part was to set the delegate of the textField to self, then to access data you can simply:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSLog(#"%#",[[alertView textField] text]);
}
Hope this helps someone, since I had to think a bit to get it right.
Most probably You would want to look into the addTextFieldWithValue method of the UIAlertView? Add the following code somewhere at the top of Your class:
#interface UIAlertView ()
- (void) addTextFieldWithValue: (NSString*) val label: (NSString*) label;
- (UITextField*) textField;
#end
It’s not official, but IMHO it’s not getting You rejected from the App store and it’s much better solution than hacking the textfield into the dialog Yourself.
Explains how to set the number of columns, have not tested it.
http://iloveco.de/uikit-alert-types/
However there is a private method,
setNumberOfRows:(int)n that will allow
you to set a maximum number of rows to
display the buttons in. To use this
method we need to add our own
additions to the UIAlertView class. We
do this by adding an #interface for
UIAlertView in our .m file.
// extend the UIAlertView class to remove the warning caused
// by calling setNumberOfRows.
#interface UIAlertView (extended)
- (void) setNumberOfRows:(int)num;
#end
This will allow us to call the method without the compiler throwing us a warning.
[myAlert setNumberOfRows:2];
Try putting in some (\n)s after the title in the UIAlertView initialization. That will push down the buttons. And I agree with Stephen here. There are chances that Apple might reject an app if it uses controls in a way they shouldn't be. (there's some clause in the Human Interface Guidelines about that!)
This simpler method works for me:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"UIAlertView"
message:#"<Alert message>" delegate:self cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert addTextFieldWithValue:#"" label:#"Text Field"];
Hope that helps. Oh if you needed multiple button rows then it's:
[alert setNumberOfRows:3];
Cheers
https://github.com/TomSwift/TSAlertView
This library actually creates the control from scratch rather than attempting to hack UIAlertView, which is generally a Bad Plan (TM)