2

我正在尝试将我们的应用程序转换为使用 SWRevealViewController 在应用程序的每一侧为我们提供一个侧边栏。但是,尽管遵循其中一个示例应用程序中的代码,但我收到了一个错误,即前视图的 ViewController 无法正常工作。viewDidLoad 被调用,但一切仍然是黑色的。

有趣的是,如果在我的 viewDidLoad 中,我将视图的背景颜色设置为红色,就会反映出来。但是原始故事板中的界面生成器中的东西不是。

我在 AppDelegate 中使用的代码是:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window = window;

    MainViewController *frontViewController = [[MainViewController alloc] init];
    RearViewController *rearViewController = [[RearViewController alloc] init];

    UINavigationController *frontNavigationController = [[UINavigationController alloc] initWithRootViewController:frontViewController];
    UINavigationController *rearNavigationController = [[UINavigationController alloc] initWithRootViewController:rearViewController];

    SWRevealViewController *revealController = [[SWRevealViewController alloc] initWithRearViewController:rearNavigationController frontViewController:frontNavigationController];
    revealController.delegate = self;

    RightViewController *rightViewController = rightViewController = [[RightViewController alloc] init];
    rightViewController.view.backgroundColor = [UIColor greenColor];

    revealController.rightViewController = rightViewController;

    //revealController.bounceBackOnOverdraw=NO;
    //revealController.stableDragOnOverdraw=YES;

    self.viewController = revealController;

    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    return YES;
}

#pragma mark - SWRevealViewDelegate

- (id <UIViewControllerAnimatedTransitioning>)revealController:(SWRevealViewController *)revealController animationControllerForOperation:(SWRevealControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC
{
    if ( operation != SWRevealControllerOperationReplaceRightController )
        return nil;

    if ( [toVC isKindOfClass:[RightViewController class]] )
    {
        if ( [(RightViewController*)toVC wantsCustomAnimation] )
        {
            id<UIViewControllerAnimatedTransitioning> animationController = [[CustomAnimationController alloc] init];
            return animationController;
        }
    }

    return nil;
}

这是仅用于 MainViewController 的 Main.Storyboard: 在此处输入图像描述

当应用程序加载时,视图只是黑色的。但我可以从左右边缘拖动并按预期查看侧边栏。所以只有 FrontView 会变黑。我将一个 NSLog() 插入到 ViewDidLoad 中,它出现在控制台中,就像 -(void)loadView{} 中的一个一样,它显示视图正在加载。

如果我在 viewDidLoad 中放入一个[self.view setBackgroundColor:[UIColor redColor]]this 生效,这意味着它链接到视图,但它只是情节提要内的视图没有出现。这很奇怪,因为 Storyboard 还包含一个 NavigationController,它确实出现了(我认为 - 除非该导航控制器来自其他地方 - 我很确定它不是)。

关于可能导致这种情况的任何想法?

4

1 回答 1

8

我也发生了同样的事情。我在查看示例代码时修复了它。

  1. 在你的故事板中添加一个 UIViewController
  2. 选择它并在 Identity Inspector 中使用 'SWRevealViewController' 作为类
  3. 添加一个 UITableViewController 到你的故事板
  4. 现在选择您之前添加的 ViewControler 并右键单击并绘制一条线到您的 TableViewController
  5. 选择“显示视图控制器设置控制器”
  6. 单击新添加的 Segue 并在 Attribute Inspector 中将标识符更改为“sw_rear”
  7. 将任何自定义 ViewController(例如“MyViewController”)添加到故事板
  8. 选择它,然后转到 Menu->Editor->Embed In->Navigation Controller
  9. 现在应该出现一个新的导航控制器
  10. 再次右键单击从第一个 ViewController 到您的新 NavigationController 绘制一条线
  11. 再次选择“显示视图控制器集控制器”
  12. 现在将这个新 Segue 的标识符设置为 'sw_front'

现在您有了基本设置,并且在运行您的应用程序时,您应该会看到您的自定义 ViewController。现在必须添加菜单的按钮。

在您的 ViewControllers .m 添加以下内容:

@interface MyViewController ()
@property (nonatomic) IBOutlet UIBarButtonItem* revealButtonItem;
@end

- (void)viewDidLoad {
    [super viewDidLoad];

    [self customSetup];
}

- (void)customSetup
{
    SWRevealViewController *revealViewController = self.revealViewController;
    if ( revealViewController )
    {
        [self.revealButtonItem setTarget: self.revealViewController];
        [self.revealButtonItem setAction: @selector( revealToggle: )];
        [self.navigationController.navigationBar addGestureRecognizer: self.revealViewController.panGestureRecognizer];
    }
}

现在再次切换到故事板。将 UIBarButtonItem 添加到 MyViewController。再次右键单击从 MyViewController 到 NavigationBar 中的这个新项目绘制一条线。选择“显示按钮项目”。

而已!对要添加的每个 ViewController 重复最后的步骤。您只需将它们从 TableView 中通过右键单击绘图连接到您添加的每个 ViewController 的新添加 NavigationController。要推送 ViewControllers,只需选择“显示视图控制器推送控制器”。

希望那有所帮助!

于 2014-12-05T14:53:20.307 回答