Problem in navigation Controller - iphone

I have a problem I want to control the backBarButtonItem when I click on it Action backAction "works
my problem is that SetAction does not work, I hope there is a solution for the controller backBarButtonItem
- (void)viewDidLoad {
[self.navigationItem.backBarButtonItem setTarget:self];
[self.navigationItem.backBarButtonItem setAction:#selector(backAction)];
[super viewDidLoad];
}
- (void)backAction {
if ((isSaveCarte==NO)&&(isNewCarte)) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Alert" message:#"Voulez vous Enregistrer la Carte" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Save",nil];
[alertView show];
[alertView release];
}
}

Check below, Could be useful for you
How to trap the back button event
http://forums.macrumors.com/showthread.php?t=637266
How to create backBarButtomItem with custom view for a UINavigationController
Assignig a action to backBarButtonItem

back bar button item is meant for navigating back...
why don't you use the leftBarButtonItem instead of backBarButtonItem..
the only difference would be it wont be a left pointed button.. but you are not navigating back anyways right?

Related

Detect if back button is pressed AND wait user action to pop to previous view

I saw multiple questions about how to detect when the user is pressing the "Back" button on a UINavigationBar, but the answers does not solved my problem.
Indeed, I want to display a UIAlertView when user is pressing the UINavigationBar "Back" button, to ask him "Do you want to save the changes ?".
I can display a UIAlertView when the user is pressing the "Back" button (with the following snippet), but the previous view is popped in the same time. And I don't want this behaviour ! I just want that the app WAIT the user answer BEFORE pop the previous view...
-(void) viewWillDisappear:(BOOL)animated
{
if ([self isMovingFromParentViewController])
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Avertissement" message:#"Voulez-vous enregistrer les modifications effectuées ?" delegate:self cancelButtonTitle:#"Retour" otherButtonTitles:#"Oui", #"Non", nil];
[alert show];
}
[super viewWillDisappear:animated];
}
Thanks for you help...
I Suggest you using you own LeftbarButtonItem instead of default Back-button event at viewWillDisappear like this:-
- (void)viewDidLoad
{
UIBarButtonItem *left=[[UIBarButtonItem alloc]initWithTitle:#"Back" style:UIBarButtonItemStyleBordered target:self action:#selector(actionBack:)];
self.navigationItem.leftBarButtonItem = left;
[super viewDidLoad];
}
- (IBAction)actionBack:(id)sender {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Avertissement" message:#"Voulez-vous enregistrer les modifications effectuées ?" delegate:self cancelButtonTitle:#"Retour" otherButtonTitles:#"Oui", #"Non", nil];
[alert show];
}
And use alert Delegate clickedButtonAtIndex
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(alertView.tag == 1)
{
// set your logic
}
}

Don't Pop Navigation Controller in Xcode if AlertView

I have a Navigation Controller and when I press the back button I don't want to pop the view but I want to show an UIAlertView.I just want to pop the view after having make a choice on the UIAlertView!
How can I do?
I tried to catch the event 'back button pressed' but with no results :(
Create a UIBarButtonItem inside viewDidload and added it to the navigation bar
UIBarButtonItem *backButton=[[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStyleBordered target:self action:#selector(backButtonClicked)];
self.navigationItem.leftBarButtonItem=backButton;
create a method which will be called when the backButton is clicked
-(void)backButtonClicked{
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:#"Pop View!" message:#"Are you sure?" delegate:self cancelButtonTitle:#"No" otherButtonTitles:#"Yes", nil];
[alert show];
}
write this UIAlertView delegate method which will be called when the button in the UIAlertView is clicked
-(void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex{
if (buttonIndex==1) {
[self.navigationController popViewControllerAnimated:YES];
}
}
NOTE: Dont forget to add UIAlertView delegate in the header file <UIAlertViewDelegate>
You can check for back-button press in the viewWillDisappear method like this (got it from here):
-(void)viewWillDisappear:(BOOL)animated {
if ([self.navigationController.viewControllers indexOfObject:self]==NSNotFound && self.isBackButtonPressed) {
// back button was pressed. We know this is true because self is no longer
// in the navigation stack.
}
[super viewWillDisappear:animated];
Hope this helps!
Implement pop in UIAlertview delegate method clickedButtonAtIndex:
The back button press need to implement only showing the UIAlertview
refer this
follow this code
in button action.
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Message" message:#"Do you want to open?" delegate:self cancelButtonTitle:#"NO" otherButtonTitles:#"YES", nil];
[alert show];
//Alert View delegate method
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(buttonIndex==0)//NO
{
//your code
}
else if(buttonIndex==1)//YES
{
// your code
}
}

iPhone AlertView Text Field Keyboard not visible

I have an alert view with a uitextfield added as a subview.
In my uitableview controller the keyboard shows fine.
However in a different view I wanted to do the same thing. So instead of using a UITableView Controller I made it a UIViewController so that I could add a toolbar at the bottom of the view.
But when I display the UIAlertView the keyboard is hidden. I'm thinking it's behind the view because the alert view moves up to make room for the keyboard.
Any ideas?
UPDATE:
The reason the keyboard was being hidden was because I was dismissing a modalviewcontroller after showing the alert. For some reason it would dismiss the keyboard also I guess. Just rearranged the order an fit works fine now...
Try implementing the didPresentAlertView: method and inside set the text field to firstResponder like so:
- (IBAction)someActionThatTriggersAnAlertView:(id)sender {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"TITLE" message:#"MESSAGE" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Done", nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
UITextField *someTextField = [alert textFieldAtIndex:0];
someTextField.keyboardType = UIKeyboardTypeAlphabet;
someTextField.keyboardAppearance = UIKeyboardAppearanceAlert;
someTextField.autocorrectionType = UITextAutocorrectionTypeNo;
[alert show];
[alert release];
}
#pragma mark - UIAlertViewDelegate Methods
- (void)didPresentAlertView:(UIAlertView *)alertView {
[[alertView textFieldAtIndex:0] becomeFirstResponder];
}
UIAlertView Documentation
UIAlertViewDelegate Documentation

UIAlertView choice causing resignFirstResponder to fail

I'm having a similar issue to Anthony Chan's question, and after trying every suggested solution, I'm still stuck.
Somehow, only after interacting with my UIAlertView, I'm unable to dismiss the keyboard in another view of my app. It's as though the Alert is breaking my UITextField's ability to resignFirstResponder. Below I instantiate my UIAlertView, which then calls its didDismissWIthButtonIndex method. Then, I call the showInfo method, which loads another UIViewController.
UIAlertView *emailFailAlert = [[UIAlertView alloc] initWithTitle:#"Error"
message:#"error message text."
delegate:self
cancelButtonTitle:#"Not now"
otherButtonTitles:#"Settings", nil];
[emailFailAlert setTag:2];
[emailFailAlert show];
[emailFailAlert release];
Once the 'Settings' option is pressed, I'm calling this method:
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
if ([alertView tag] == 2) {
if (buttonIndex == 1){
[self showInfo:nil];
}
}
}
My showInfo method loads the other ViewController, via the code below:
- (IBAction)showInfo:(id)sender {
FlipsideViewController *fscontroller = [[FlipsideViewController alloc] initWithNibName:#"FlipsideView" bundle:nil];
fscontroller.delegate = self;
fscontroller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:fscontroller animated:YES];
[fscontroller release];
}
Upon clicking any textField in this Flipside VC, I'm unable to dismiss the keyboard as I normally can with - (BOOL)textFieldShouldReturn:(UITextField *)textField, and [textField resignFirstResponder]. I've omitted this code bc this question is getting long, but I'm happy to post if necessary.
The interesting part is that if I comment out the [self showInfo:nil] call made when the button is clicked and call it by clicking a test button (outside the alertView didDismissWithButtonIndex: method), everything works fine. Any idea what's happening here?
Thanks in advance!
When an alert, with more than one dismissal option, is called above a keyboard - the keyboard becomes un-dismissible with resignFirstResponder on the active textfield;
You will need to dismiss the keyboard before showing the alert.
Assuming your UITextField is called myTextField;
[myTextField resignFirstResponder]; //That's the only line I added
UIAlertView *emailFailAlert = [[UIAlertView alloc] initWithTitle:#"Error"
message:#"error message text."
delegate:self
cancelButtonTitle:#"Not now"
otherButtonTitles:#"Settings", nil];
[emailFailAlert setTag:2];
[emailFailAlert show];
[emailFailAlert release];
I hope this helps anyone who had to deal with this oddly obscure issue.
You should not call alertView:didDismissWithButtonIndex: directly. This delegate method will be executed automatically in all cases after the alert has disappeared. Otherwise the code will be run twice!

iPhone, I want to create a popup menu from my tab bar, but what are they call, like copy / paste?

I'd like to create a popup menu, from my tab bar.
I've seen them, but I'm not sure what they called ?
Kind of like a speak bubble.
I think copy / paste is the sort of thing I mean.
You are searching for the UIMenuController.
This question might help you.
That could be the UIActionSheet. You might want to give it a try.
//Your delegate for pressed buttons.
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
//Your Action for pressed button.
[actionSheet release];
}
//Declare a method to show your sheet
-(void)showActionSheet
{
UIActionSheet *menu = [[UIActionSheet alloc]
initWithTitle: #"Action Sheet Title"
delegate:self
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:#"Delete"
otherButtonTitles:#"Other Button1", #"Other Button2", nil];
[menu showInView:self.view];
}
But you said "bubble", so that could also be the UIAlertView.
Try this:
-(void)showAlertView
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Alert!"
message:#"This is the message that pops up in the bubble."
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
}
On the iPad, you'd want UIPopoverController, but I think that on the iPhone the same sort of interactions are meant to be performed by a separate, full screen controller or by a UIActionSheet. There's no speech bubble on the iPhone such that I'm aware.