无法覆盖一个简单的方法

时间:2013-01-02 20:23:29

标签: objective-c ios xcode uiviewcontroller

我创建了一个包含两个ViewControllers的新项目,并导入了一个类,该类从右向左推送ViewController而不是LTR,但无法使用它。我可以看到UIRightToLeft.m中的pushViewController没有被调用,我不明白为什么。 我的主要目标是使用RTL aniamtion。

#import "UIRightToLeft.h"

@implementation UIRightToLeft

- (id)initWithRootViewController:(UIViewController *)rootViewController
{
    self = [super initWithRootViewController:rootViewController];
    if (!self)
        return nil;
    return self;
}

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    NSLog(@"pushViewController");
    // Add the viewController and a fake controller without animation. Then pop the fake controller with animation.
    UIViewController *fakeController = [[UIViewController alloc] init] ;
    [super setViewControllers:[[self viewControllers] arrayByAddingObjectsFromArray:[NSArray arrayWithObjects:viewController, fakeController, nil]] animated:NO];
    [super popViewControllerAnimated:animated];
}

- (void)popViewControllerAnimatedStep2:(UIViewController *)viewController
{
    // Push the new top controller with animation
    [super pushViewController:viewController animated:YES];
    // Remove the view that should have been popped
    NSMutableArray *arr = [NSMutableArray arrayWithArray:[self viewControllers]];
    [arr removeObjectAtIndex:[[self viewControllers] count]-2];
    [super setViewControllers:[NSArray arrayWithArray:arr] animated:NO];
}

- (UIViewController *)popViewControllerAnimated:(BOOL)animated
{
    NSLog(@"popViewControllerAnimated");

    if (animated)
    {
        // Save the controller that should be on top after this pop operation
        UIViewController *newTopController = [[self viewControllers] objectAtIndex:[[self viewControllers] count]-2];
        // Remove it from the stack. Leave the view that should be popped on top
        NSMutableArray *arr = [NSMutableArray arrayWithArray:[self viewControllers]];
        [arr removeObjectAtIndex:[[self viewControllers] count]-2];
        [super setViewControllers:[NSArray arrayWithArray:arr] animated:NO];
        // Schedule the next step
        [self performSelector:@selector(popViewControllerAnimatedStep2:) withObject:newTopController afterDelay:0];
        return [arr objectAtIndex:[arr count]-1];
    }
    return [super popViewControllerAnimated:NO];
}

@end

ViewController.m:

#import "ViewController.h"
#import "UIRightToLeft.h"
#import "SecondViewController.h"
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)pressMe:(UIButton *)sender {
    SecondViewController *next = [[SecondViewController alloc]init];
    [self.navigationController pushViewController:next animated:YES];    
}

@end

ViewController.h:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
- (IBAction)pressMe:(UIButton *)sender;

@end

(在我的ViewController中只有一个按钮与推送的第二个ViewController相关联。

1 个答案:

答案 0 :(得分:1)

查看错误日志后,我认为您确实需要将导航控制器子类化为UIRightToLeft。

如果您使用的是故事板,请选择导航控制器并将自定义类设置为UIRightToLeft。