ios 以编程方式使后退按钮转到上一个视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21869480/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Make back button go to the previous view programmatically
提问by Colton Anglin
I have a UIBarButtonItem and would like to programmatically set the action that goes to the previous controller (in my case, my previous view is a UITableViewController).
我有一个 UIBarButtonItem 并且想以编程方式设置转到前一个控制器的操作(在我的例子中,我以前的视图是一个 UITableViewController)。
Below is my code that I am currently using to make the bar button item although the button doesn't go to the previous view yet.
下面是我目前用来制作栏按钮项目的代码,尽管按钮还没有转到上一个视图。
UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithTitle:@"Done"
style:UIBarButtonItemStyleDone target:nil action:nil];
UINavigationItem *item = [[UINavigationItem alloc] initWithTitle:@"Website"];
item.leftBarButtonItem = leftButton;
item.hidesBackButton = YES;
[myBar pushNavigationItem:item animated:NO];
回答by larva
Add following code in your controller, in - (void)viewDidLoad
function:
在您的控制器中添加以下代码,在- (void)viewDidLoad
函数中:
call [self addBackButtonWithTitle:@"back"];
if You want to custom backbutton with title.
[self addBackButtonWithTitle:@"back"];
如果您想自定义带有标题的后退按钮,请致电。
or [self addBackButtonWithImageName:@"back_button_image_name"];
if You want custom backbutton with image.
或者[self addBackButtonWithImageName:@"back_button_image_name"];
如果您想要带有图像的自定义后退按钮。
/**
* @brief set lef bar button with custom title
*/
- (void)addBackButtonWithTitle:(NSString *)title {
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:title style:UIBarButtonItemStylePlain target:self action:@selector(backButtonPressed:)];
self.navigationItem.leftBarButtonItem = backButton;
}
/**
* @brief set left bar button with custom image (or custom view)
*/
- (void)addBackButtonWithImageName:(NSString *)imageName {
// init your custom button, or your custom view
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
backButton.frame = CGRectMake(0, 0, 40, 22); // custom frame
[backButton setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
[backButton addTarget:self action:@selector(backButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
// set left barButtonItem with custom view
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
}
- (void)backButtonPressed:(id)sender {
[self.navigationController popViewControllerAnimated:YES];
}
回答by Mohit
You could write this in your viewDidLoad
method:
你可以在你的viewDidLoad
方法中写这个:
UIBarButtonItem *btn=[[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:self action:@selector(btnClick)];
self.navigationItem.leftBarButtonItem=btn;
and then use this:
然后使用这个:
// call of method
-(void)btnClick
{
[self.navigationController popViewControllerAnimated:YES];
}
回答by Sebastián Lara
[self dismissViewControllerAnimated:YES completion:nil];
[自我解雇ViewControllerAnimated:是完成:nil];