Ios 导航控制器中“后退”按钮的设置操作
我试图覆盖导航控制器中后退按钮的默认操作。我在自定义按钮上提供了一个目标操作。奇怪的是,当通过backbutton属性指定它时,它不会注意它们,只会弹出当前视图并返回到根:Ios 导航控制器中“后退”按钮的设置操作,ios,iphone,cocoa-touch,uinavigationcontroller,uibarbuttonitem,Ios,Iphone,Cocoa Touch,Uinavigationcontroller,Uibarbuttonitem,我试图覆盖导航控制器中后退按钮的默认操作。我在自定义按钮上提供了一个目标操作。奇怪的是,当通过backbutton属性指定它时,它不会注意它们,只会弹出当前视图并返回到根: UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle: @"Servers" style:UIBarB
UIBarButtonItem *backButton = [[UIBarButtonItem alloc]
initWithTitle: @"Servers"
style:UIBarButtonItemStylePlain
target:self
action:@selector(home)];
self.navigationItem.backBarButtonItem = backButton;
当我通过navigationItem
上的leftBarButtonItem
设置它时,它会调用我的操作,但是,该按钮看起来像一个普通的圆形按钮,而不是带箭头的背面按钮:
self.navigationItem.leftBarButtonItem = backButton;
如何让它在返回根视图之前调用我的自定义操作?是否有方法覆盖默认的后退操作,或者是否有一种方法在离开视图时总是被调用(
viewDidUnload
不这样做)?通过使用当前离开“nil”的目标和操作变量,您应该能够连接保存对话框,以便在“选中”按钮时调用它们. 小心,这可能会在奇怪的时刻触发
我基本上同意Amagrammer,但我认为定制箭头按钮并不难。我只需重新命名“后退”按钮,拍摄一张屏幕截图,用photoshop处理所需的按钮大小,并将其作为按钮顶部的图像。我认为这是不可能的,很容易做到。我认为唯一能解决这个问题的方法就是制作你自己的后退按钮箭头图像。一开始我很沮丧,但我明白为什么为了一致性,它被忽略了 通过创建常规按钮并隐藏默认的后退按钮,可以接近(不带箭头):
self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"Servers" style:UIBarButtonItemStyleDone target:nil action:nil] autorelease];
self.navigationItem.hidesBackButton = YES;
不像阿格拉默说的,这是可能的。您必须对
导航控制器
进行子类化。我解释了所有内容(包括示例代码)。您可以尝试访问NavigationBars Right Button项并设置其选择器属性…这里有一个参考,如果这不起作用,另一件事是,将导航栏的右按钮项设置为您创建的自定义UIBarButtonItem并设置其选择器…希望这有助于直接执行。有两种选择:
UIBarButtonItem
,如果测试通过,它会在点击时验证并弹出UITextField
委托方法验证表单字段内容,例如,在按下键盘上的Return
或Done
按钮后调用委托方法第二个选项很好,因为您可以在委托方法中返回文本字段,因此您可以将验证逻辑指向发送给委托回调方法的特定文本字段。此技术允许您更改“返回”的文本按钮,而不影响任何视图控制器的标题或在动画期间看到“后退”按钮文本的更改 将其添加到调用视图控制器中的init方法中:
UIBarButtonItem *temporaryBarButtonItem = [[UIBarButtonItem alloc] init];
temporaryBarButtonItem.title = @"Back";
self.navigationItem.backBarButtonItem = temporaryBarButtonItem;
[temporaryBarButtonItem release];
尝试将其放入要检测压力机的视图控制器:
-(void) viewWillDisappear:(BOOL)animated {
if ([self.navigationController.viewControllers indexOfObject:self]==NSNotFound) {
// back button was pressed. We know this is true because self is no longer
// in the navigation stack.
}
[super viewWillDisappear:animated];
}
出于一些线程方面的原因,@HansPinckaers提到的解决方案不适合我,但我找到了一种更简单的方法来点击后退按钮,我想把它固定在这里,以防这可以避免其他人被欺骗数小时。 诀窍非常简单:只需将一个透明的UIButton作为子视图添加到UINavigationBar中,并为他设置选择器,就好像它是真正的按钮一样! 这里有一个使用Monotouch和C#的例子,但是到objective-C的翻译应该不太难找到
public class Test : UIViewController {
public override void ViewDidLoad() {
UIButton b = new UIButton(new RectangleF(0, 0, 60, 44)); //width must be adapted to label contained in button
b.BackgroundColor = UIColor.Clear; //making the background invisible
b.Title = string.Empty; // and no need to write anything
b.TouchDown += delegate {
Console.WriteLine("caught!");
if (true) // check what you want here
NavigationController.PopViewControllerAnimated(true); // and then we pop if we want
};
NavigationController.NavigationBar.AddSubview(button); // insert the button to the nav bar
}
}
有趣的事实:为了测试和为我的假按钮找到好的尺寸,我把它的背景颜色设置为蓝色。。。它会在后退按钮后面显示!无论如何,它仍然捕捉到任何指向原始按钮的触摸。对于需要这样的用户输入的表单,我建议将其作为“模式”调用,而不是作为导航堆栈的一部分。这样,他们必须处理表单上的业务,然后您可以使用自定义按钮对其进行验证和取消。您甚至可以设计一个导航栏,它看起来与应用程序的其他部分相同,但给您更多的控制。要截取后退按钮,只需用透明的UIControl覆盖它并截取触摸
@interface MyViewController : UIViewController
{
UIControl *backCover;
BOOL inhibitBackButtonBOOL;
}
@end
@implementation MyViewController
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
// Cover the back button (cannot do this in viewWillAppear -- too soon)
if ( backCover == nil ) {
backCover = [[UIControl alloc] initWithFrame:CGRectMake( 0, 0, 80, 44)];
#if TARGET_IPHONE_SIMULATOR
// show the cover for testing
backCover.backgroundColor = [UIColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:0.15];
#endif
[backCover addTarget:self action:@selector(backCoverAction) forControlEvents:UIControlEventTouchDown];
UINavigationBar *navBar = self.navigationController.navigationBar;
[navBar addSubview:backCover];
}
}
-(void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[backCover removeFromSuperview];
backCover = nil;
}
- (void)backCoverAction
{
if ( inhibitBackButtonBOOL ) {
NSLog(@"Back button aborted");
// notify the user why...
} else {
[self.navigationController popViewControllerAnimated:YES]; // "Back"
}
}
@end
有一种更简单的方法,只需将
UINavigationBar
的委托方法子类化,并重写ShouldPopItem
方法。我已经实现了扩展。它不需要对任何内容进行子类化,只需将其放入项目中,并覆盖UIViewController
类中的navigationShouldPopOnBackButton
方法:
-(BOOL) navigationShouldPopOnBackButton {
if(needsShowConfirmation) {
// Show confirmation alert
// ...
return NO; // Ignore 'Back' button this time
}
return YES; // Process 'Back' button click and Pop view controler
}
.至少在Xcode 5中,有一个简单且相当好(并非完美)的解决方案。在IB中,从实用程序窗格中拖动一个Bar Button项,并将其放置在导航栏左侧的后退按钮所在位置。将标签设置为“后退”。您将有一个功能正常的按钮,可以连接到iAction并关闭viewController。我正在做一些工作,然后触发一个放松的序列,它工作得非常完美 不理想的是,这个按钮没有<箭头,也没有继承以前的VCs标题,但我认为这是可以管理的。出于我的目的,我将新的“后退”按钮设置为“完成”按钮,以便其目的明确 IB navigator中也有两个后退按钮,但为了清晰起见,标记它很容易
最简单的方法 您可以使用UINavigationController的委托方法。按下VC的后退按钮时,将调用方法
willShowViewController
。按下后退按钮时,您可以执行任何操作
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated;
迅捷的
这是我的快速解决方案。在你的
override func viewWillDisappear(animated: Bool) {
let viewControllers = self.navigationController?.viewControllers!
if indexOfArray(viewControllers!, searchObject: self) == nil {
// do something
}
super.viewWillDisappear(animated)
}
func indexOfArray(array:[AnyObject], searchObject: AnyObject)-> Int? {
for (index, value) in enumerate(array) {
if value as UIViewController == searchObject as UIViewController {
return index
}
}
return nil
}
extension UIViewController {
func navigationShouldPopOnBackButton() -> Bool {
return true
}
}
extension UINavigationController {
func navigationBar(navigationBar: UINavigationBar, shouldPopItem item: UINavigationItem) -> Bool {
if let vc = self.topViewController {
if vc.navigationShouldPopOnBackButton() {
self.popViewControllerAnimated(true)
} else {
for it in navigationBar.subviews {
let view = it as! UIView
if view.alpha < 1.0 {
[UIView .animateWithDuration(0.25, animations: { () -> Void in
view.alpha = 1.0
})]
}
}
return false
}
}
return true
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
UIBarButtonItem* backNavButton = [[UIBarButtonItem alloc] initWithTitle:@"Back"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(backButtonClicked)];
self.navigationItem.leftBarButtonItem = backNavButton;
}
-(void)backButtonClicked
{
// Do something...
AppDelegate* delegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
[delegate.navController popViewControllerAnimated:YES];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
if ((self.isMovingFromParentViewController || self.isBeingDismissed)
&& !self.isPoppingProgrammatically) {
// Do your stuff here
}
}
self.isPoppingProgrammatically = YES;
[self.navigationController popViewControllerAnimated:YES];
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(true)
if self.isMovingFromParentViewController {
// current viewController is removed from parent
// do some work
}
}
- (void)didMoveToParentViewController:(UIViewController *)parent{
if (parent == NULL) {
NSLog(@"Back Pressed");
}
}
override func didMoveToParentViewController(parent: UIViewController?) {
if parent == nil {
println("Back Pressed")
}
}
-(void) overrideBack{
UIButton *transparentButton = [[UIButton alloc] init];
[transparentButton setFrame:CGRectMake(0,0, 50, 40)];
[transparentButton setBackgroundColor:[UIColor clearColor]];
[transparentButton addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
[self.navigationController.navigationBar addSubview:transparentButton];
}
-(void)backAction:(UIBarButtonItem *)sender {
//Your functionality
}
extension MyViewController: NavigationControllerBackButtonDelegate {
func shouldPopOnBackButtonPress() -> Bool {
performSomeActionOnThePressOfABackButton()
return false
}
}
protocol NavigationControllerBackButtonDelegate {
func shouldPopOnBackButtonPress() -> Bool
}
extension UINavigationController {
public func navigationBar(_ navigationBar: UINavigationBar, shouldPop item: UINavigationItem) -> Bool {
// Prevents from a synchronization issue of popping too many navigation items
// and not enough view controllers or viceversa from unusual tapping
if viewControllers.count < navigationBar.items!.count {
return true
}
// Check if we have a view controller that wants to respond to being popped
var shouldPop = true
if let viewController = topViewController as? NavigationControllerBackButtonDelegate {
shouldPop = viewController.shouldPopOnBackButtonPress()
}
if (shouldPop) {
DispatchQueue.main.async {
self.popViewController(animated: true)
}
} else {
// Prevent the back button from staying in an disabled state
for view in navigationBar.subviews {
if view.alpha < 1.0 {
UIView.animate(withDuration: 0.25, animations: {
view.alpha = 1.0
})
}
}
}
return false
}
}
if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)])
{
self.navigationController.interactivePopGestureRecognizer.enabled = NO;
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
if (![self.navigationController.viewControllers containsObject:self])
{
// back button was pressed or the the swipe-to-go-back gesture was
// completed. We know this is true because self is no longer
// in the navigation stack.
}
}
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
if self.navigationController?.topViewController != self {
print("back button tapped")
}
}
protocol RequestsNavigationPopVerification {
var confirmationTitle: String { get }
var confirmationMessage: String { get }
}
extension RequestsNavigationPopVerification where Self: UIViewController {
var confirmationTitle: String {
return "Go back?"
}
var confirmationMessage: String {
return "Are you sure?"
}
}
final class NavigationController: UINavigationController {
func navigationBar(navigationBar: UINavigationBar, shouldPopItem item: UINavigationItem) -> Bool {
guard let requestsPopConfirm = topViewController as? RequestsNavigationPopVerification else {
popViewControllerAnimated(true)
return true
}
let alertController = UIAlertController(title: requestsPopConfirm.confirmationTitle, message: requestsPopConfirm.confirmationMessage, preferredStyle: .Alert)
alertController.addAction(UIAlertAction(title: "Cancel", style: .Cancel) { _ in
dispatch_async(dispatch_get_main_queue(), {
let dimmed = navigationBar.subviews.flatMap { $0.alpha < 1 ? $0 : nil }
UIView.animateWithDuration(0.25) {
dimmed.forEach { $0.alpha = 1 }
}
})
return
})
alertController.addAction(UIAlertAction(title: "Go back", style: .Default) { _ in
dispatch_async(dispatch_get_main_queue(), {
self.popViewControllerAnimated(true)
})
})
presentViewController(alertController, animated: true, completion: nil)
return false
}
}
@objc public protocol BackButtonDelegate {
@objc optional func navigationShouldPopOnBackButton() -> Bool
}
extension UINavigationController: UINavigationBarDelegate {
public func navigationBar(_ navigationBar: UINavigationBar, shouldPop item: UINavigationItem) -> Bool {
if viewControllers.count < (navigationBar.items?.count)! {
return true
}
var shouldPop = true
let vc = self.topViewController
if vc.responds(to: #selector(vc.navigationShouldPopOnBackButton)) {
shouldPop = vc.navigationShouldPopOnBackButton()
}
if shouldPop {
DispatchQueue.main.async {
self.popViewController(animated: true)
}
} else {
for subView in navigationBar.subviews {
if(0 < subView.alpha && subView.alpha < 1) {
UIView.animate(withDuration: 0.25, animations: {
subView.alpha = 1
})
}
}
}
return false
}
}
class BaseVC: UIViewController, BackButtonDelegate {
func navigationShouldPopOnBackButton() -> Bool {
if ... {
return true
} else {
return false
}
}
}
func navigationShouldPopOnBackButton() -> Bool {
let alert = UIAlertController(title: "Warning",
message: "Do you want to quit?",
preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Yes", style: .default, handler: { UIAlertAction in self.yes()}))
alert.addAction(UIAlertAction(title: "No", style: .cancel, handler: { UIAlertAction in self.no()}))
present(alert, animated: true, completion: nil)
return false
}
func yes() {
print("yes")
DispatchQueue.main.async {
_ = self.navigationController?.popViewController(animated: true)
}
}
func no() {
print("no")
}
extension UINavigationController: UINavigationBarDelegate {
public func navigationBar(_ navigationBar: UINavigationBar, shouldPop item: UINavigationItem) -> Bool {
}
}