简体   繁体   English

按下导航控制器的左键按钮时如何停止视图消失

[英]how to stop view disappearing when navigation controller left bar button pressed

I am rookie to iOS .In my child view controller i made some modification in data. 我是iOS的新手。在我的子视图控制器中,我对数据做了一些修改。 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> 不要忘记添加<UIAlertViewDelegate>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 迅速按下导航栏后退按钮时重新加载视图控制器2 - Reloading view controller when navigation bar back button is pressed in swift 2 当导航栏的内置后退按钮被按下时弹出到根视图控制器 - Pop to the root view controller when the navigation bar's built in back button is pressed 在导航控制器中按下“后退”按钮时,是否可能不取消分配视图控制器? - is it possible to not dealloc a view controller when the back button is pressed in a navigation controller? 按下导航后退按钮时,SWRevealViewController关闭左侧视图 - SWRevealViewController close left side view when navigation back button pressed 按下导航控制器后退按钮时如何导航到其他View Controller? - How to navigate to some other View Controller when Navigation Controller back button is pressed? 如何为当前视图控制器编辑导航栏上的后退按钮(不是左按钮)? - How to edit the back button (not left button) on navigation bar for the present view controller? 当按下导航后退按钮时,将值返回到呈现视图控制器 - Return values to presenting view controller when navigation back button pressed 当按下编辑按钮时,如何在导航栏中添加加号按钮? - How to add a plus button in a navigation bar when the edit button is pressed? 标签栏为初始视图控制器时,如何使“返回”按钮出现在导航控制器中 - How to make 'Back' button appear in Navigation Controller when Tab Bar is initial view controller 当导航栏中的“后退”按钮没有标题时,停止将其左移 - stop left bar button from being shifted right when back button has no title in navigation bar
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM