【问题标题】:Can't override a simple method无法覆盖简单的方法
【发布时间】:2013-01-03 22:19:11
【问题描述】:

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

#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 上)

【问题讨论】:

  • 您到底想完成什么?正常的推送动画已经是从右到左了。你是说从左到右吗? ViewController.m 中的 self.navigationController 是否指向您的 UIRightToLeft 类或 UINavigation 控制器?
  • 我正在尝试以苹果提供的另一种方式完成视图控制器之间的移动。 (对于 RTL 语言)。我不知道self.navigationController 指向哪里。这是我所有的代码
  • 那么只需记录 self.navigationController(在您的 pressMe 方法中)并查看它返回的内容。
  • 它崩溃了。我的主要问题是 pushViewController 没有被调用
  • 什么时候崩溃?你有崩溃日志吗?如果在 pressMe 方法中崩溃了,先放日志。

标签: objective-c ios xcode uiviewcontroller


【解决方案1】:

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

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

【讨论】:

  • 如此简单的解决方案!谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多