how to stop view disappearing when navigation controller left bar button pressed - ios

I am rookie to iOS .In my child view controller i made some modification in data. I inserted a done button to store a data and pop the view controller from navigation controller stack. if i press navigation controller back button it automatically goes to back without saving data. if i made any modification in data and then i pressed back button i need show alert as "modifications are made are sure want to go back". if the user press the cancel button in alert view i need to stop view disappearing and still stand on same view controller.If anybody have the answer please help me.

I think this is a way to do it
- (void)viewDidLoad{
[super viewDidLoad];
[self.navigationItem setLeftBarButtonItem:nil];
[self.navigationItem setHidesBackButton:YES];
UIButton *myButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 44)];
[myButton setImage:[UIImage imageNamed:#"back.png"] forState:UIControlStateNormal];
//back.png = your image name
[myButton setTitle:#"Back" forState:UIControlStateNormal];
[myButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[myButton addTarget:self action:#selector(backButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *customBackButton = [[UIBarButtonItem alloc] initWithCustomView:myButton];
[self.navigationItem setLeftBarButtonItem:customBackButton];
}
- (void)backButtonTapped:(id)sender{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Message" message:#"modifications are made are sure want to go back" delegate:self cancelButtonTitle:#"Go Back" otherButtonTitles:#"Stay Here", nil];
[alert show];
}
- (void)alertView:(UIAlertView *)alertView
clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == [alertView cancelButtonIndex]){
[self.navigationController popViewControllerAnimated:YES];
}else{
//Stay on the page and do something
}
}
Don't forget to add <UIAlertViewDelegate>

Related

View Controller hides after showing when called from ActionSheet manually

I am writing a code, where another view controller is called from Action Sheet. I am using "performSegueWithIdentifier" for this because I want to pass different text depending on which action button is pressed. It was working fine but now as I call the view controller from Action sheet, it appears and then hides suddenly. I checked my code but can't figure out the bug. Here is my code. Note: I am not using navigation controller.
//"selectEditor" is the button action used for calling action sheet.
- (IBAction)selectEditor:(id)sender {
_sheet = [[UIActionSheet alloc] initWithTitle:#"Select type of Editor"
delegate:self
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:#"Button1",#"Button2", nil];
[_sheet showInView:self.view];
}
//Perform segue to vc when action button is pressed. "editorSegue" is segue identifier.
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
//trigger call here
if (buttonIndex==0)
{
EditorFlag=1;
[self performSegueWithIdentifier:#"editorSegue" sender:self];
}
if (buttonIndex==1)
{
EditorFlag=0;
[self performSegueWithIdentifier:#"editorSegue" sender:self];
}
}
#pragma mark - Navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"editorSegue"]) {
//Additional code here. EditorFlag is used for identification to set different text on same view controller when different action buttons are pressed.
if (EditorFlag==1)
{
//EditTextController is the view controller which hides after showing
EditTextController *textEditor = [segue destinationViewController];
textEditor.textstring=#"some text";
}
else
{
EditTextController *textEditor = [segue destinationViewController];
textEditor.textstring=#"some text";
}
}
}
//Here is Editor text code, I am adding navigation bar manually but I also checked it by commenting all code in it.
#implementation EditTextController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[[self navigationController] setNavigationBarHidden:YES animated:YES];
UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0,0, self.view.frame.size.width, 50)];
navBar.barTintColor = [UIColor whiteColor];
[self.view addSubview:navBar];
UIBarButtonItem *doneItem = [[UIBarButtonItem alloc] initWithTitle:#"back" style:UIBarButtonItemStylePlain target:self action:#selector(back:)];
UINavigationItem *navItem = [[UINavigationItem alloc] initWithTitle:_navString];
[navItem setLeftBarButtonItem:doneItem animated:YES];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:#"Cloud_Arrow_Up.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(methodAction:) forControlEvents:UIControlEventTouchUpInside];
[button setFrame:CGRectMake(0, 0, 30, 30)];
navItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
[navBar setItems:[NSArray arrayWithObject:navItem] animated:YES];
[self.textEditor setText:self.textstring];
_textEditor.font = [UIFont fontWithName:#"HelveticaNeue" size:24];
_textEditor.textColor = [UIColor blueColor];
}
-(void)back:(id)sender
{
//[self performSegueWithIdentifier:#"realTimeSegue" sender:sender];
}
- (void)methodAction:(UIButton *)button
{
//[self performSegueWithIdentifier:#"syncSegue" sender:button];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

iOS back button has no padding

When I push a viewcontroller on my navigation stack, the back button appears to be against the left side of the screen. How can I get the regular padding of the back button?
Here is how I present the view controller:
- (void)goToCollection:(UIButton *)btn {
Card *colCard = (Card *)btn.userData;
WViewController *vc = [WViewController new];
NSString *colID = [[colCard.href componentsSeparatedByString:#"/"] lastObject];
vc.collectionID = colID;
[self.navigationController pushViewController:vc animated:YES];
}
Here's my view setup in didFinishLaunchingWithOptions :
//Profile View
ProfileViewController *pv = [ProfileViewController new];
UINavigationController *profNav = [[UINavigationController alloc] initWithRootViewController:pv];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self.window setRootViewController:profNav];
[self.window setTintColor:[UIColor Primary]];
[self.window makeKeyAndVisible];
As per apple documentation, you can not edit or modify back button in any case. Only allowed operation for back button is show and hide .
So if you want something to do with back button, you need to hide it and create a customise left bar button item as back button.
self.navigationItem.hidesBackButton = YES; //hides back button
UIButton *myButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)]; // creates a new button
[myButton setImage:[UIImage backButtonImage]; // sets image for new button
[myButton setContentEdgeInsets:UIEdgeInsetsMake(0, -15, 0, 0)];//content edgeInset to provide required padding
[myButton addTarget:self action:#selector(backToHOmePage) forControlEvents:UIControlEventTouchUpInside];//adding target for new button
UIBarButtonItem *customBackBtn = [[UIBarButtonItem alloc] initWithCustomView:myButton]; //custom bar button item
self.navigationItem.leftBarButtonItem = customBackBtn; //assigning to left bar button item
-(void)backToHOmePage
{
[self.navigationController popViewControllerAnimated:YES];
} // method to be triggered on tapping above button
Put this in your AppDelegate
[[UIBarButtonItem appearance] setBackButtonBackgroundVerticalPositionAdjustment:-3 forBarMetrics:UIBarMetricsDefault];

Keyboard appearing briefly when popping viewcontroller iOS

I'm experimenting an issue with the numeric pad when popping the current UIViewController from the UINavigationController.
In my current UIViewController. I have a few UITextField and a "Save" button in the UINavigationBar. The expected behavior is as follows:
When the user taps "Save", the keyboard must hide and a network operation is performed. In its callback, an UIAlertView is shown. When user dismisses this UIAlertView, a notification raises and the current UIViewController performs
[self.navigationController popViewControllerAnimated:YES];
The thing is, if "Save" is pressed with the keyboard still showing, after performing the popViewControllerAnimated, the keyboard appears briefly and from left to right (as if it was visible in the previous UIViewController).
I've tried
[myTextField resignFirstResponder]
when user taps "Save", when user dismisses the UIAlertView, and even in the
viewWillDisappear
method. Some other answers suggest using
[self.view endEditing:YES];
but it doesn't work either.
If I could use the regular keyboard it would be trivial to override
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
to hide it when user taps "Return", "Done", etc. But being the numeric pad doesn't allow you to show that "finish" button.
I'd appreciate any help, thank you all for your time
Try this:
Set the text field delegate and its return type as Done and pad as numeric pad.
_textField.delegate = self;
_textField.keyboardType = UIKeyboardTypeDecimalPad;
[_textField setReturnKeyType:UIReturnKeyDone];
and now add the buttons to keyboard:
-(void)addButtonsToKeyboard
{
UIToolbar* keyboardDoneButtonView = [[UIToolbar alloc] init];
[keyboardDoneButtonView sizeToFit];
UIBarButtonItem* doneButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(#"Done", nil)
style:UIBarButtonItemStyleDone target:self
action:#selector(kbDoneAction:)];
UIBarButtonItem* seperatorItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
UIBarButtonItem* cancelButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(#"Cancel", nil)
style:UIBarButtonItemStylePlain target:self
action:#selector(kbCancelAction:)];
[keyboardDoneButtonView setItems:[NSArray arrayWithObjects:cancelButton,seperatorItem, doneButton, nil]];
_textField.inputAccessoryView = keyboardDoneButtonView;
}
and to hide the keyboard:
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
and your Done action method is:
-(void)kbDoneAction:(id)sender
{
[_textField resignFirstResponder];
}
and Cancel action method is:
-(void)kbCancelAction:(id)sender
{
[_textField resignFirstResponder];
}
Try using the below code. It works fine for iOS 8 and below version
if (IS_OS_8_OR_LATER) {
UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:title message:msg preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelAction = [UIAlertAction
actionWithTitle:B_title
style:UIAlertActionStyleCancel
handler:^(UIAlertAction *action)
{
[self.navigationController popViewControllerAnimated:YES];
}];
[alertVC addAction:cancelAction];
[self presentViewController:alertVC animated:YES completion:nil];
}
else{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:msg delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
[alert show];
}
}
I had a similar situation. I ended up delaying the popViewControllerAnimated a little longer than the keyboard animation duration (0.333 seconds).

UIImagePickerController inside UIPopoverController doesn't show Cancel button on iOS7

I'm using UIImagePickerController inside a UIPopoverController to select image just from photo albums. When I launch app on device running iOS 8, the Cancel button on the top right of the pop over appeared normally like this:
But when I launch the app on device running iOS 7, the Cancel button disappeared:
The code I used to show the picker:
UIImagePickerController *pickerController = [[UIImagePickerController alloc] init];
[pickerController setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
pickerController.delegate = self;
_popOver = [[UIPopoverController alloc] initWithContentViewController:pickerController];
_popOver.delegate = self;
pickerController.navigationBar.tintColor = [UIColor redColor];//Cancel button text color
[pickerController.navigationBar setTitleTextAttributes:#{NSForegroundColorAttributeName: [UIColor blackColor]}];// title color
if (isImage) {
[pickerController setMediaTypes:#[(NSString*)kUTTypeImage]];
} else
[pickerController setMediaTypes:#[(NSString*)kUTTypeMovie]];
[_popOver presentPopoverFromRect:CGRectMake(1024/2, 768/2, 1, 1) inView:self.view permittedArrowDirections:0 animated:YES];
What can I do to show that Cancel button on iOS7? My app design doesn't allow user to dismiss the popover by tapping anywhere outside the popover view.
Thank you.
#JozoL Write the Below Code this works
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
UINavigationItem *pickerNavBarTopItem;
// add done button to right side of nav bar
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:#"Cancel"
style:UIBarButtonItemStylePlain
target:self
action:#selector(doSomething)];
UINavigationBar *bar = navigationController.navigationBar;
[bar setHidden:NO];
pickerNavBarTopItem = bar.topItem;
pickerNavBarTopItem.rightBarButtonItem = doneButton;
}
-(void)doSomething{
}
For swift2.x you can try below code, Its work for me
pickerController.navigationBar.tintColor = UIColor.blueColor()
In my case, UIImagePickerController was not showing its cancel button
. So I tried adding this line to code:
pickerController.navigationBar.barStyle = UIBarStyleDefault;
And this worked for me.Try setting color of Cancel button like this
pickerController.navigationBar.tintColor = [UIColor blackColor];
Try using this code and let me know if it helps you.

Closing UIActionSheet

I have a UIActionSheet and I have added the following - UIPickerView and 2 BarButtonItems. I am trying to close the action sheet when someone clicks the Cancel Button (it is one of the bar button items).
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles: nil];
[actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
CGRect pickerFrame = CGRectMake(0,40,0,0);
UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:pickerFrame];
pickerView.showsSelectionIndicator = YES;
pickerView.dataSource = self;
pickerView.delegate = self;
[actionSheet addSubview:pickerView];
UIToolbar *tools = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 40)];
tools.barStyle = UIBarStyleBlackOpaque;
[actionSheet addSubview:tools];
UIBarButtonItem *doneButton=[[UIBarButtonItem alloc]initWithTitle:#"Done" style:UIBarButtonItemStyleBordered target:self action:#selector(btnActinDoneClicked)];
doneButton.imageInsets=UIEdgeInsetsMake(200, 6, 50, 25);
UIBarButtonItem *CancelButton=[[UIBarButtonItem alloc]initWithTitle:#"Cancel" style:UIBarButtonItemStyleBordered target:self action:#selector(btnActinCancelClicked)]; //this is where the problem is
NSArray *array = [[NSArray alloc]initWithObjects:CancelButton, doneButton, nil];
[tools setItems:array];
[actionSheet showFromTabBar:self.tabBarController.tabBar];
[actionSheet setBounds:CGRectMake(0, 0, 320, 485)];
I have added another method to dismiss the action sheet -
-(IBAction)btnActinCancelClicked:(id)sender{
[sender dismissWithClickedButtonIndex:0 animated:YES];
}
The problem is that the app crashes when the BarButton is clicked. I think the problem is caused where the CancelButton is declared and it is unable to send a message to the btnActinCancelClicked method, but I can't figure out how to fix it.
Thanks
Your method is this;
-(IBAction)btnActinCancelClicked:(id)sender
So you need to add it like this (because it receives a parameter);
#selector(btnActinCancelClicked:)
So for example, if a method had three parameters you'd call it #selector(myMethod:andX:andY:)
Also, to access your UIActionSheet (not entirely sure this will work);
UIActionSheet *acSheet = sender.superView;
//Check if it is the ActionSheet here then dismiss it
[acSheet dismissWithClickedButtonIndex:0 animated:YES];
It's not the sender you want to dismiss. It's the ActionSheet. You'll need a reference for that and change your code for something like:
- (IBAction)btnActinCancelClicked:(id)sender{
[self.actionSheet dismissWithClickedButtonIndex:0 animated:YES];
}

Resources